|
| 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