copiablebuf

Crates.iocopiablebuf
lib.rscopiablebuf
version0.0.7
created_at2025-05-14 19:10:07.870455+00
updated_at2025-05-18 18:18:15.597164+00
descriptionCopiable buffer, a tinier `Vec`, uses a fixed-size array to store a variable number of items.
homepage
repositoryhttps://github.com/0xAA55-rs/copiablebuf
max_upload_size
id1673852
size43,119
0xaa55 (0xAA55)

documentation

README

Copiable buffer

The copiable buffer is a tinier Vec, which uses a fixed-size array to store a variable number of items.

Overview

Prototypes

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],
}

Usage

let buf = CopiableBuffer::<i32, 64>::new();

Then you can use the buf just like using a fixed capacity Vec.

Stack memory usage

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.

Commit count: 33

cargo fmt