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:
'staticandDefault, although since all fields are always required to implement these two traits, so too willLateInstance.Send,Sync, andDebugare all perfectly structural w.r.tS::EraseTo.Eqis implemented ifS::EraseToimplementsDynEq.PartialEqis implemented ifS::EraseToimplementsDynPartialEq.Cloneis implemented ifS::EraseToimplementsDynClone.Hashis implemented ifS::EraseToimplementsDynHash.
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.
impl<S: LateStruct> LateInstance<S>
Regular operations.
Sourcepub fn new() -> Self
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.
Sourcepub fn get<F: LateField<S>>(&self) -> &F::Value
pub fn get<F: LateField<S>>(&self) -> &F::Value
Fetches an immutable reference to a field by its LateField marker type.
Sourcepub fn get_ptr<F: LateField<S>>(&self) -> NonNull<F::Value>
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.
Sourcepub fn get_two<F, G>(&mut self) -> (&mut F::Value, &mut G::Value)
pub fn get_two<F, G>(&mut self) -> (&mut F::Value, &mut G::Value)
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.
Sourcepub fn dynamic(&mut self) -> &mut LateInstanceDyn<S>
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.
impl<S: LateStruct> LateInstance<S>
Reflection operations.
Sourcepub unsafe fn new_custom(
init: impl FnMut(&'static LateFieldDescriptor<S>, *mut u8),
) -> Self
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.
Sourcepub unsafe fn try_new_custom<E>(
init: impl FnMut(&'static LateFieldDescriptor<S>, *mut u8) -> Result<(), E>,
) -> Result<Self, E>
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.
Sourcepub fn init_token(&self) -> LateLayoutInitToken
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.
Sourcepub fn fields(&self) -> &'static [&'static LateFieldDescriptor<S>]
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().
Sourcepub fn base_ptr(&self) -> NonNull<u8>
pub fn base_ptr(&self) -> NonNull<u8>
Fetches the pointer to the base of the heap allocation containing the structure’s values.
Sourcepub fn get_erased(&self, field: &LateFieldDescriptor<S>) -> &S::EraseTo
pub fn get_erased(&self, field: &LateFieldDescriptor<S>) -> &S::EraseTo
Fetches an immutable reference to a field by its LateFieldDescriptor instance.
Sourcepub fn get_erased_mut(
&mut self,
field: &LateFieldDescriptor<S>,
) -> &mut S::EraseTo
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.
Sourcepub fn get_erased_ptr(
&self,
field: &LateFieldDescriptor<S>,
) -> NonNull<S::EraseTo>
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.