LateInstance

Struct LateInstance 

Source
pub struct LateInstance<S: LateStruct> { /* private fields */ }
Expand description

An instance of a LateStruct.

You can define a late-initialized structure with the late_struct! macro and add fields to it with the late_field! macro. You can then fetch these fields using the get and get_mut methods.

These methods only allow users to borrow one field mutably at a time. If this is too restrictive, you can use the get_two method, which obtains two fields mutably at the same time. If that is still too restrictive, consider using the dynamic method, which returns a LateInstanceDyn view of this structure providing a RefCell-like runtime-borrow-checked API for accessing fields. If that is still too restrictive, all fields in the structure exhibit UnsafeCell-like semantics and thus can be borrowed manually using the get_ptr method.

§Reflection Operations

Both LateInstance::fields and LateStruct::descriptor expose the set of fields in a given structure as a list of LateFieldDescriptors. These can be used alongside the get_erased and get_erased_mut to obtain references to the S::EraseTo types to which all fields are required to upcast.

§Structural Traits

Trait implementations for this type are largely structural. If S::EraseTo implements Send, for instance, this type will implement Send. Here is a full table of all of the structural trait implementations:

The DynEq, DynClone, and DynHash traits exist because Eq, Clone, and Hash, are not dyn compatible and thus cannot directly be used in the bounds of a dyn Trait object.

We do not have a structural implementation for Ord since the order of fields inside this structure is not defined.

By default, S::EraseTo is set to dyn 'static + fmt::Debug. This implies that the LateInstance instantiating that S will implement Debug but will not implement, e.g., Send or Sync. See this section of the crate level documentation for information on how to change these bounds.

Implementations§

Source§

impl<S: LateStruct> LateInstance<S>

Regular operations.

Source

pub fn new() -> Self

Instantiate a new structure where each field is initialized using its implementation of the Default trait.

This is equivalent to the LateInstance’s implementation of Default.

Source

pub fn get<F: LateField<S>>(&self) -> &F::Value

Fetches an immutable reference to a field by its LateField marker type.

Source

pub fn get_mut<F: LateField<S>>(&mut self) -> &mut F::Value

Fetches an mutable reference to a field by its LateField marker type.

If you want to fetch more than one field at a time mutably, consider using the get_two or dynamic methods.

Source

pub fn get_ptr<F: LateField<S>>(&self) -> NonNull<F::Value>

Fetches a raw pointer to the structure’s field by its LateField marker type. These fields act as if they were wrapped inside an UnsafeCell and thus can be mutably dereferenced.

Source

pub fn get_two<F, G>(&mut self) -> (&mut F::Value, &mut G::Value)
where F: LateField<S>, G: LateField<S>,

Fetches a pair of mutable references to distinct fields identified by their LateField marker types. This method panics if F and G are the same type.

Source

pub fn dynamic(&mut self) -> &mut LateInstanceDyn<S>

Fetches a LateInstanceDyn view into the structure which exposes a RefCell-like API for dynamically borrowing multiple fields mutably at the same time.

Source§

impl<S: LateStruct> LateInstance<S>

Reflection operations.

Source

pub unsafe fn new_custom( init: impl FnMut(&'static LateFieldDescriptor<S>, *mut u8), ) -> Self

Construct a new LateInstance using the init closure to initialize each field in the order they appear in the LateStructDescriptor::fields list.

§Safety

The pointee of each of the *mut u8 pointers provided to init must be initialized to an instance of the corresponding field’s value type.

Source

pub unsafe fn try_new_custom<E>( init: impl FnMut(&'static LateFieldDescriptor<S>, *mut u8) -> Result<(), E>, ) -> Result<Self, E>

Construct a new LateInstance using the init closure to initialize each field in the order they appear in the LateStructDescriptor::fields list. If init returns an Err, the construction of the structure is aborted and the error is forwarded to the caller.

§Safety

The pointee of each of the *mut u8 pointers provided to init must be initialized to an instance of the corresponding field’s value type.

Source

pub fn init_token(&self) -> LateLayoutInitToken

Fetches a LateLayoutInitToken attesting to the fact that all late-initialized structure layouts and field offsets have been resolved.

Source

pub fn fields(&self) -> &'static [&'static LateFieldDescriptor<S>]

Fetches the set of fields comprising the structure. This is equivalent to calling the fields method on the LateStructDescriptor instance returned by S::descriptor().

Source

pub fn base_ptr(&self) -> NonNull<u8>

Fetches the pointer to the base of the heap allocation containing the structure’s values.

Source

pub fn get_erased(&self, field: &LateFieldDescriptor<S>) -> &S::EraseTo

Fetches an immutable reference to a field by its LateFieldDescriptor instance.

Source

pub fn get_erased_mut( &mut self, field: &LateFieldDescriptor<S>, ) -> &mut S::EraseTo

Fetches a mutable reference to a field by its LateFieldDescriptor instance.

If you want to fetch more than one field at a time mutably, consider using the get_two or dynamic methods.

Source

pub fn get_erased_ptr( &self, field: &LateFieldDescriptor<S>, ) -> NonNull<S::EraseTo>

Fetches a raw pointer to the structure’s field identified by its LateFieldDescriptor instance. These fields act as if they were wrapped inside an UnsafeCell and thus can be mutably dereferenced.

Trait Implementations§

Source§

impl<S: LateStruct> Clone for LateInstance<S>
where S::EraseTo: DynClone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<S: LateStruct> Debug for LateInstance<S>
where S::EraseTo: Debug,

Source§

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

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

impl<S: LateStruct> Default for LateInstance<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S: LateStruct> Drop for LateInstance<S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<S: LateStruct> Hash for LateInstance<S>
where S::EraseTo: DynHash,

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<S: LateStruct> PartialEq for LateInstance<S>
where S::EraseTo: DynEq,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<S: LateStruct> Eq for LateInstance<S>
where S::EraseTo: DynEq,

Source§

impl<S: LateStruct> Send for LateInstance<S>
where S::EraseTo: Send,

Source§

impl<S: LateStruct> Sync for LateInstance<S>
where S::EraseTo: Sync,

Auto Trait Implementations§

§

impl<S> Freeze for LateInstance<S>

§

impl<S> !RefUnwindSafe for LateInstance<S>

§

impl<S> Unpin for LateInstance<S>

§

impl<S> UnwindSafe for LateInstance<S>

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

Source§

unsafe fn dyn_clone_into(&self, other: *mut u8)

Source§

impl<T> DynHash for T
where T: Hash,

Source§

fn dyn_hash(&self, hasher: &mut dyn Hasher)

Source§

impl<T> DynPartialEq for T
where T: 'static + PartialEq,

Source§

fn dyn_eq(&self, other: &(dyn Any + 'static)) -> bool

Source§

fn as_any(&self) -> &(dyn Any + 'static)

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.
Source§

impl<T> DynEq for T
where T: 'static + Eq,