Crates.io | vec_storage_reuse |
lib.rs | vec_storage_reuse |
version | 0.1.0 |
source | src |
created_at | 2022-09-04 23:15:29.697922 |
updated_at | 2022-09-04 23:15:29.697922 |
description | Provides an API to reuse a `Vec`'s allocation |
homepage | |
repository | https://github.com/Ten0/vec_storage_reuse |
max_upload_size | |
id | 658486 |
size | 14,882 |
Provides an API to reuse a Vec
's allocation.
This is useful to achieve zero-alloc when storing data with short lifetimes in a Vec
:
let mut objects_storage: VecStorageForReuse<Object<'static>> = VecStorageForReuse::new();
while let Some(byte_chunk) = stream.next() { // byte_chunk only lives this scope
let mut objects: &mut Vec<Object<'_>> = &mut *objects_storage.reuse_allocation();
// Zero-copy parsing; Object has references to chunk
deserialize(byte_chunk, &mut objects)?;
process(&objects)?;
} // byte_chunk lifetime ends
This crate delegates the actual unsafe functionality to the recycle_vec
crate, and just provides
an interface that abstracts the swapping with the container through Drop
, so that one can never
forget to swap back the temporary object with the storage