| Crates.io | copiablebuf |
| lib.rs | copiablebuf |
| version | 0.0.7 |
| created_at | 2025-05-14 19:10:07.870455+00 |
| updated_at | 2025-05-18 18:18:15.597164+00 |
| description | Copiable buffer, a tinier `Vec`, uses a fixed-size array to store a variable number of items. |
| homepage | |
| repository | https://github.com/0xAA55-rs/copiablebuf |
| max_upload_size | |
| id | 1673852 |
| size | 43,119 |
The copiable buffer is a tinier Vec, which uses a fixed-size array to store a variable number of items.
pub trait CopiableItem: Default + Clone + Copy + Debug + Sized + PartialEq {}
impl<T> CopiableItem for T where T: Default + Clone + Copy + Debug + Sized + PartialEq {}
#[derive(Clone, Copy, Eq)]
pub struct CopiableBuffer<T, const N: usize>
where
T: CopiableItem,
{
buf_used: usize,
buffer: [T; N],
}
let buf = CopiableBuffer::<i32, 64>::new();
Then you can use the buf just like using a fixed capacity Vec.
If you are using this directly in your struct, beware if you then just use your struct in your main() function or test functions, this thing stores data on the stack.
Using too much of CopiableBuffer on the stack will cause stack overflow even if you are not using recursive calls in your program.
This is a convenient tool for you to create data structures with a clear max size and less memory allocation (Rust only needs to do one memory allocation to your struct, then all of the CopiableBuffer in your struct were allocated with a clear capacity). If your struct is on the heap, all of the CopiableBuffer are on the heap too, thus you don't need to worry about stack overflow.