pub struct ZstCache<'gc, const MAX_ALIGN: usize> { /* private fields */ }Expand description
Provides an optimization for allocating Gc pointers to Zero Sized Types.
This type stores a single dummy allocation of the requested alignment. When an alloc method is called, if the given value is a ZST whose alignment is less than or equal to the given one, then the dummy allocation is cast to the correct type and that is returned instead.
All ZSTs allocated this way can share a single allocated pointer. This obviously breaks pointer
uniqueness, so if pointer uniqueness is required, Gc::new should be used instead.
Implementations§
Source§impl<'gc, const MAX_ALIGN: usize> ZstCache<'gc, MAX_ALIGN>where
Alignment<MAX_ALIGN>: ValidAlignment,
impl<'gc, const MAX_ALIGN: usize> ZstCache<'gc, MAX_ALIGN>where
Alignment<MAX_ALIGN>: ValidAlignment,
pub fn new(mc: &Mutation<'gc>) -> Self
Sourcepub fn cached_ptr(&self) -> Gc<'gc, ()>
pub fn cached_ptr(&self) -> Gc<'gc, ()>
Returns the internally held pointer used as a ZST ptr cache.
It will always be aligned to MAX_ALIGN.
Sourcepub fn is_cached<T: ?Sized>(&self, p: Gc<'gc, T>) -> bool
pub fn is_cached<T: ?Sized>(&self, p: Gc<'gc, T>) -> bool
Returns true if the given pointer is cached by this ZstCache.
Sourcepub fn alloc_zst<T: 'gc>(&self) -> Option<Gc<'gc, T>>
pub fn alloc_zst<T: 'gc>(&self) -> Option<Gc<'gc, T>>
Return the cached pointer as a pointer to T, if possible.
If the given type T is not a ZST or has an alignment which is greater than MAX_ALIGN,
returns None.
This method never performs any actual allocation.
Sourcepub fn alloc<T: Collect<'gc>>(&self, mc: &Mutation<'gc>, t: T) -> Gc<'gc, T>
pub fn alloc<T: Collect<'gc>>(&self, mc: &Mutation<'gc>, t: T) -> Gc<'gc, T>
Like Gc::new, but returns the cached pointer if possible.
Sourcepub fn alloc_static<T: 'static>(&self, mc: &Mutation<'gc>, t: T) -> Gc<'gc, T>
pub fn alloc_static<T: 'static>(&self, mc: &Mutation<'gc>, t: T) -> Gc<'gc, T>
Like Gc::new_static, but returns the cached pointer if possible.
Trait Implementations§
Source§impl<'gc, const MAX_ALIGN: usize> Collect<'gc> for ZstCache<'gc, MAX_ALIGN>
impl<'gc, const MAX_ALIGN: usize> Collect<'gc> for ZstCache<'gc, MAX_ALIGN>
Source§const NEEDS_TRACE: bool = true
const NEEDS_TRACE: bool = true
Gc pointer and trace is unnecessary
to call, you may set this to false. The default value is true, signaling that
Collect::trace must be called.Source§fn trace<T: Trace<'gc>>(&self, cc: &mut T)
fn trace<T: Trace<'gc>>(&self, cc: &mut T)
Trace::trace_gc (resp. Trace::trace_gc_weak) on all directly owned
Gc (resp. GcWeak) pointers. If this type holds inner types that implement Collect,
a valid implementation would simply call Trace::trace on all the held values to ensure
this. Read more