Skip to content

Commit ea1fc85

Browse files
committed
Initial commit of functionality
While still a long way from a release, this commit adds the basics of the fixedvec architecture. The struct FixedVec has been created, and supports len(), capacity(), and push() functions (naively). This commit also adds a README and configuration file for TravisCI. Signed-off-by: Nick Stevens <[email protected]>
1 parent be1c9f4 commit ea1fc85

File tree

5 files changed

+246
-2
lines changed

5 files changed

+246
-2
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010
*.exe
1111

1212
# Generated by Cargo
13+
Cargo.lock
1314
/target/
14-
15-

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: rust
2+
rust:
3+
- nightly
4+
5+
script:
6+
- cargo build --verbose
7+
- cargo test --verbose
8+
- cargo doc
9+
10+
env:
11+
global:
12+
secure: "Bce9DCZMa5jAfvn3QKsOsWrTcM/qI+x2Q32rn2DKcy8rDNilZmoR8DXUisiqLoFB1xuc88Sj9P9Zu9oXoTwU7BDECBxMzayZgdjxHUS8m9hQd4RmuN8nRvLkGKjc74Fk4L881t1nkuwfbKOlpw2ev68Vn84luPlJToaDrAIc9KdjSr9Qyhar+vhQKy+hv1x/hp/1QVF7oYr+R7slXMX4HihXc2RlXuk89lslWRqd4SY7N93xywEv4kn+fE4AKS0qo8WQCKcpjMKTqvof7T2O2Cbpa0aLeHlS9WZ+/yM82s/cg2rvXWHOWclAgwuIS//RUJy8LPa3MXkMX2rz/L1NeO9Ff347SWwdw57MUnCNnWO/Tdk6qnzRLkjYlrMwFeDQtaRw8NTMKgjCvuT+bvQFXLsPXi6vrUNCab1zA2p/xiwPtCc4Vg55TnSj15Ryc81kokP+zlHNC5NHZe/fKmORaLjuIKx4n7gCGZiuCZYypLdG0MS/lQO7afK4XfjHVtEKqJpwxdY5lClWVZuie/dsDeYANQS7/ClMWcqLPZfYul2BF7furNn1g408iZsB/eFimS0pc8/HT3o0ju5Fgh5Ny1k5Xt/xCwp5LK1L6ZhSazTeHElecq6MNSe78HI/apFkc3k+QLiEJudwF/cnvSHT8KJkEBy8Z0rdJdQ07psZyvE="
13+
14+
# Publish documentation on successful master build using the
15+
# ghp-import python package
16+
after_success: ! '[ $TRAVIS_BRANCH = master ] && [ $TRAVIS_PULL_REQUEST = false ]
17+
&& echo "<meta http-equiv=refresh content=0;url=fixedvec/index.html>" > target/doc/index.html
18+
&& sudo pip install ghp-import
19+
&& ghp-import -n target/doc
20+
&& git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages'
21+

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "fixedvec"
3+
version = "0.1.0"
4+
authors = ["Nick Stevens <[email protected]>"]
5+
license = "MIT"
6+
repository = "https://github.com/nastevens/fixedvec-rs"
7+
homepage = "https://github.com/nastevens/fixedvec-rs"
8+
documentation = "http://nastevens.github.io/fixedvec-rs/"
9+
description = """
10+
A heapless implementation of the Rust vector.
11+
"""

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
fixedvec-rs
2+
===========
3+
4+
[![Build Status] (https://img.shields.io/travis/nastevens/fixedvec-rs.svg)](https://travis-ci.org/nastevens/fixedvec-rs)
5+
6+
- [API Documentation](http://nastevens.github.io/fixedvec-rs/)
7+
8+
`fixedvec-rs` is a Rust library/crate providing a heapless version of the Rust
9+
vector type. Although more limited than the libstd version, fixedvec-rs
10+
provides a much-needed "managed" array type for embedded systems or other
11+
projects that cannot rely on the heap.
12+
13+
TODO
14+
----
15+
16+
`fixedvec-rs` as it exists now is not ready for any sort of general use, and is
17+
extremely limited at this point. This is a work in progress. Once a reasonable
18+
level of functionality has been achieved, this warning will be removed and the
19+
crate will be uploaded to crates.io.
20+
21+
License
22+
-------
23+
24+
```
25+
Copyright (c) 2015, Nick Stevens <[email protected]>
26+
27+
The MIT License (MIT)
28+
29+
Permission is hereby granted, free of charge, to any person obtaining a copy of
30+
this software and associated documentation files (the "Software"), to deal in
31+
the Software without restriction, including without limitation the rights to
32+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
33+
of the Software, and to permit persons to whom the Software is furnished to do
34+
so, subject to the following conditions:
35+
36+
The above copyright notice and this permission notice shall be included in all
37+
copies or substantial portions of the Software.
38+
39+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
44+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
45+
SOFTWARE.
46+
```

src/lib.rs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2015 Nick Stevens <[email protected]>
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a
6+
// copy of this software and associated documentation files (the "Software"),
7+
// to deal in the Software without restriction, including without limitation
8+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
// and/or sell copies of the Software, and to permit persons to whom the
10+
// Software is furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in
13+
// all copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
// DEALINGS IN THE SOFTWARE.
22+
23+
#![crate_type = "lib"]
24+
#![crate_name = "fixedvec"]
25+
26+
#![feature(core)]
27+
28+
///! Heapless Vec implementation using only libcore
29+
///!
30+
///! When developing for certain types of systems, especially embedded systems,
31+
///! it is desireable to avoid the non-determinism that can be introduced by
32+
///! using a heap. A commonly used data structure is a "buffer" - a
33+
///! pre-allocated chunk of memory, either in static memory or on the stack.
34+
///!
35+
///! Thanks to the extensibility of Rust, it is possible to have a datatype
36+
///! that performs _almost_ like the libstd `Vec` type, without requiring a
37+
///! heap and while only using libcore.
38+
///!
39+
///! # Examples
40+
///!
41+
///! Typical usage looks like the following:
42+
///!
43+
///! ```rust
44+
///! #![feature(core)]
45+
///! extern crate core;
46+
///!
47+
///! #[macro_use] extern crate fixedvec;
48+
///!
49+
///! use fixedvec::FixedVec;
50+
///!
51+
///! #[derive(Debug, Default, Copy, Clone)]
52+
///! struct MyStruct {
53+
///! a: i32,
54+
///! b: i32
55+
///! }
56+
///!
57+
///! fn main() {
58+
///! let mut preallocated_space = alloc_stack!([MyStruct; 16]);
59+
///! let vec = FixedVec::new(&mut preallocated_space);
60+
///! }
61+
///! ```
62+
63+
extern crate core;
64+
65+
#[macro_export]
66+
macro_rules! alloc_stack {
67+
([$item_type:ty; $len:expr]) => ({
68+
let space: [$item_type; $len] = [ Default::default() ; $len ];
69+
space
70+
})
71+
}
72+
73+
#[derive(Debug)]
74+
pub struct FixedVec<'a, T: 'a> {
75+
memory: &'a mut [T],
76+
used: usize,
77+
}
78+
79+
impl <'a, T: 'a> FixedVec<'a, T> {
80+
/// Create a new `FixedVec` from the provided memory
81+
///
82+
/// # Example
83+
///
84+
/// ```rust
85+
/// #[macro_use] extern crate fixedvec;
86+
/// use fixedvec::FixedVec;
87+
///
88+
/// fn main() {
89+
/// let mut space = alloc_stack!([u8; 16]);
90+
/// let vec = FixedVec::new(&mut space);
91+
/// assert!(vec.capacity() == 16);
92+
/// assert!(vec.len() == 0);
93+
/// }
94+
/// ```
95+
///
96+
pub fn new(memory: &'a mut [T]) -> Self {
97+
FixedVec {
98+
memory: memory,
99+
used: 0,
100+
}
101+
}
102+
103+
/// Returns the capacity of the `FixedVec`
104+
///
105+
/// # Example
106+
///
107+
/// ```rust
108+
/// #[macro_use] extern crate fixedvec;
109+
/// use fixedvec::FixedVec;
110+
///
111+
/// fn main() {
112+
/// let mut space = alloc_stack!([u8; 16]);
113+
/// let vec = FixedVec::new(&mut space);
114+
/// assert_eq!(vec.capacity(), 16);
115+
/// }
116+
/// ```
117+
pub fn capacity(&self) -> usize {
118+
self.memory.len()
119+
}
120+
121+
/// Returns the number of elements in the `FixedVec`. This will always be
122+
/// less than or equal to the `capacity()`.
123+
///
124+
/// # Example
125+
///
126+
/// ```rust
127+
/// #[macro_use] extern crate fixedvec;
128+
/// use fixedvec::FixedVec;
129+
///
130+
/// fn main() {
131+
/// let mut space = alloc_stack!([u8; 16]);
132+
/// let mut vec = FixedVec::new(&mut space);
133+
/// vec.push(1);
134+
/// vec.push(2);
135+
/// assert_eq!(vec.len(), 2);
136+
/// }
137+
/// ```
138+
pub fn len(&self) -> usize {
139+
self.used
140+
}
141+
142+
/// Appends an element to the back of the `FixedVec`.
143+
///
144+
/// # Panics
145+
///
146+
/// Panics if the number of elements in the collection exceeds its
147+
/// capacity.
148+
///
149+
/// # Example
150+
///
151+
/// ```rust
152+
/// #[macro_use] extern crate fixedvec;
153+
/// use fixedvec::FixedVec;
154+
///
155+
/// fn main() {
156+
/// let mut space = alloc_stack!([u8; 16]);
157+
/// let mut vec = FixedVec::new(&mut space);
158+
/// vec.push(1);
159+
/// vec.push(2);
160+
/// assert_eq!(vec.len(), 2);
161+
/// }
162+
/// ```
163+
pub fn push(&mut self, value: T) {
164+
self.memory[self.used] = value;
165+
self.used += 1;
166+
}
167+
}

0 commit comments

Comments
 (0)