Struct ReusableVec

Source
pub struct ReusableVec<T: 'static> { /* private fields */ }
Expand description

A wrapper around Vec that allows for reusing its allocation.

In performance-sensitive applications, frequent creation and destruction of vectors can lead to significant overhead from memory allocations. ReusableVec mitigates this by providing a mechanism to recycle a vector’s allocation.

The recycle method returns a ReusableVecGuard, which provides temporary, exclusive access to the underlying vector. When the guard is dropped, the vector is cleared, but its underlying memory allocation is preserved for subsequent use.

§Type Parameters

  • T: The type of elements in the Vec. This type must be 'static because the ReusableVec itself holds onto the allocation indefinitely.

§Safety

This struct uses an UnsafeCell to hold the Vec, which allows for mutating its contents through a shared reference. The safety of this pattern is guaranteed by the recycle method, which requires a mutable reference (&mut self). This ensures that only one ReusableVecGuard can exist at a time for a given ReusableVec, thereby preventing data races.

§Examples

§Basic Reuse

use triple_r::ReusableVec;

let mut reusable_vec = ReusableVec::<i32>::default();

// First use: populate the vector
{
    let mut vec_guard = reusable_vec.recycle();
    vec_guard.push(10);
    vec_guard.push(20);
    assert_eq!(*vec_guard, vec![10, 20]);
} // Guard is dropped, vector is cleared, but allocation is kept.

// Second use: the vector is empty but has retained its capacity.
{
    let mut vec_guard = reusable_vec.recycle();
    assert!(vec_guard.is_empty());
    assert!(vec_guard.capacity() >= 2);
    vec_guard.push(30);
    assert_eq!(*vec_guard, vec![30]);
}

Implementations§

Source§

impl<T1> ReusableVec<T1>
where T1: 'static,

Source

pub fn recycle<'parent, T2>( &'parent mut self, ) -> ReusableVecGuard<'parent, T1, T2>
where T1: ReuseCastInto<T2>,

Reuses the Vec’s allocation, returning a guard for temporary access.

This method allows the Vec’s element type to be “cast” to a new type T2, as long as the original type T1 implements ReuseCastInto<T2>.

The &mut self requirement is a key safety feature, as it ensures that only one guard can be active at any given time.

Trait Implementations§

Source§

impl<T: Debug + 'static> Debug for ReusableVec<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: 'static> Default for ReusableVec<T>

Source§

fn default() -> Self

Creates a new, empty ReusableVec with no allocation.

Source§

impl<T: Send> Send for ReusableVec<T>

Source§

impl<T: Send> Sync for ReusableVec<T>

Auto Trait Implementations§

§

impl<T> !Freeze for ReusableVec<T>

§

impl<T> !RefUnwindSafe for ReusableVec<T>

§

impl<T> Unpin for ReusableVec<T>
where T: Unpin,

§

impl<T> UnwindSafe for ReusableVec<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.