Skip to main content

ZstCache

Struct ZstCache 

Source
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,

Source

pub fn new(mc: &Mutation<'gc>) -> Self

Source

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.

Source

pub fn is_cached<T: ?Sized>(&self, p: Gc<'gc, T>) -> bool

Returns true if the given pointer is cached by this ZstCache.

Source

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.

Source

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.

Source

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> Clone for ZstCache<'gc, MAX_ALIGN>

Source§

fn clone(&self) -> ZstCache<'gc, MAX_ALIGN>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'gc, const MAX_ALIGN: usize> Collect<'gc> for ZstCache<'gc, MAX_ALIGN>

Source§

const NEEDS_TRACE: bool = true

As an optimization, if this type can never hold a 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)

Must call 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
Source§

impl<'gc, const MAX_ALIGN: usize> Copy for ZstCache<'gc, MAX_ALIGN>

Auto Trait Implementations§

§

impl<'gc, const MAX_ALIGN: usize> !RefUnwindSafe for ZstCache<'gc, MAX_ALIGN>

§

impl<'gc, const MAX_ALIGN: usize> !Send for ZstCache<'gc, MAX_ALIGN>

§

impl<'gc, const MAX_ALIGN: usize> !Sync for ZstCache<'gc, MAX_ALIGN>

§

impl<'gc, const MAX_ALIGN: usize> Freeze for ZstCache<'gc, MAX_ALIGN>

§

impl<'gc, const MAX_ALIGN: usize> Unpin for ZstCache<'gc, MAX_ALIGN>

§

impl<'gc, const MAX_ALIGN: usize> UnsafeUnpin for ZstCache<'gc, MAX_ALIGN>

§

impl<'gc, const MAX_ALIGN: usize> UnwindSafe for ZstCache<'gc, MAX_ALIGN>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<'gc, T> DynCollect<'gc> for T
where T: Collect<'gc>,

Source§

fn dyn_trace(&self, cc: &mut dyn Trace<'gc>)

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.