Crate recycle_vec

source ·
Expand description

This crate provides a recycle extension method for Vec. It’s intended to change the type of the Vec while “recycling” the underlying allocation. This is a trick that is useful especially when storing data with short lifetimes in Vec:

let mut objects: Vec<Object<'static>> = Vec::new();    // Any lifetime goes here

while let Some(byte_chunk) = stream.next() {           // `byte_chunk` lifetime starts
    let mut temp: Vec<Object<'_>> = objects.recycle(); // `temp` lifetime starts

    // Zero-copy parsing; deserialized `Object`s have references to `byte_chunk`
    deserialize(byte_chunk, &mut temp)?;
    process(&temp)?;

    objects = temp.recycle();                          // `temp` lifetime ends
}                                                      // `byte_chunk` lifetime ends

§Notes about safety

This crate uses internally unsafe to achieve it’s functionality. However, it provides a safe interface. To achieve safety, it does the following precautions:

  1. It truncates the Vec to zero length, dropping all the values. This ensures that no values of arbitrary types are transmuted accidentally.
  2. It checks that the sizes and alignments of the source and target types match. This ensures that the underlying block of memory backing Vec is compatible layout-wise. The sizes and alignments are checked statically, so if the compile will fail in case of a mismatch.
  3. It creates a new Vec value using from_raw_parts, instead of transmuting, an operation whose soundness would be questionable.

Traits§

  • A trait that provides an API for recycling Vec’s internal buffers