Trait naga::back::spv::recyclable::Recyclable

source ·
pub trait Recyclable {
    // Required method
    fn recycle(self) -> Self;
}
Expand description

A value that can be reset to its initial state, retaining its current allocations.

Naga attempts to lower the cost of SPIR-V generation by allowing clients to reuse the same Writer for multiple Module translations. Reusing a Writer means that the Vecs, HashMaps, and other heap-allocated structures the Writer uses internally begin the translation with heap-allocated buffers ready to use.

But this approach introduces the risk of Writer state leaking from one module to the next. When a developer adds fields to Writer or its internal types, they must remember to reset their contents between modules.

One trick to ensure that every field has been accounted for is to use Rust’s struct literal syntax to construct a new, reset value. If a developer adds a field, but neglects to update the reset code, the compiler will complain that a field is missing from the literal. This trait’s recycle method takes self by value, and returns Self by value, encouraging the use of struct literal expressions in its implementation.

Required Methods§

source

fn recycle(self) -> Self

Clear self, retaining its current memory allocations.

Shrink the buffer if it’s currently much larger than was actually used. This prevents a module with exceptionally large allocations from causing the Writer to retain more memory than it needs indefinitely.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<K, S: Clone> Recyclable for IndexSet<K, S>

source§

fn recycle(self) -> Self

source§

impl<K, S: Clone> Recyclable for HashSet<K, S>

source§

fn recycle(self) -> Self

source§

impl<K, V, S: Clone> Recyclable for HashMap<K, V, S>

source§

fn recycle(self) -> Self

source§

impl<K: Ord, V> Recyclable for BTreeMap<K, V>

source§

fn recycle(self) -> Self

source§

impl<T> Recyclable for Vec<T>

source§

fn recycle(self) -> Self

Implementors§