1
0
Fork
You've already forked prealloc_ref_vec
0
Create temporary Vecs of references without allocating/deallocating (useful for realtime code)
Rust 100%
2025年10月05日 16:48:55 -05:00
src add crate-level docs 2025年10月05日 16:48:55 -05:00
.gitignore initial implementation 2025年10月05日 13:01:18 -05:00
Cargo.toml add crate-level docs 2025年10月05日 16:48:55 -05:00
LICENSE Initial commit 2025年10月05日 18:59:37 +02:00
README.md fix link to license 2025年10月05日 16:44:54 -05:00

prealloc_ref_vec

Documentation Crates.io License

Create temporary Vecs of references without allocating/deallocating (useful for realtime code). This crate is implemented without any unsafe code, and can be used without the standard library (#![no_std]).

Note, if the maximum capacity is known at compile time and is small enough to fit on the stack, then the same effect can be more easily achieved using arrayvec instead.

Example

useprealloc_ref_vec::PreallocRefVec;// Contruct the object to hold the allocation. Here we specify
// we want the data type to be slices of `f32` values.
//
// This object can be stored as a field in a struct for later use.
letcapacity=100;letmutv_alloc: PreallocRefVec<[f32]>=PreallocRefVec::new(capacity);// ...
letslice_1: [f32;4]=[0.0;4];letslice_2: [f32;4]=[0.0;4];// Temporarily use the allocation to construct a Vec<&T>
// (Vec<&[f32]> in this case)
letmuttmp_vec=v_alloc.get_tmp();// The temporary vec is always cleared when created.
assert_eq!(tmp_vec.len(),0);// The temporary vec uses the internal allocation.
assert!(tmp_vec.capacity()>=capacity);tmp_vec.push(&slice_1);tmp_vec.push(&slice_2);// Dropping the temporary vec returns the allocation to be used again.
drop(tmp_vec);// ...
// Alternatively, a closure can be used to construct a temporary vec:
v_alloc.with_tmp(|tmp_vec|{assert_eq!(tmp_vec.len(),0);assert!(tmp_vec.capacity()>=capacity);tmp_vec.push(&slice_1);tmp_vec.push(&slice_2);});