Expand description

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)?;
    } // byte_chunk lifetime ends


This crate delegates most of the 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 or empty it

“Most of unsafe” because we still implement Send/Sync on VecStorageForReuse regardless of the inner type. This is safe because any Vec stored in this structure is always empty.


Stores a Vec and prevents it from being accessed in any other ways than through reinterpreting its type to reuse the allocation. This is useful to make it clear by typing that it’s its only intended purpose.

Implements DerefMut<Target = Vec<T>>, and puts the allocation back in place in the source Vec<S> once dropped