#[doc(hidden)]pub unsafe trait HasField<Field, const VARIANT_ID: i128, const FIELD_ID: i128>: HasTag {
type Type: ?Sized;
// Required methods
fn only_derive_is_allowed_to_implement_this_trait()
where Self: Sized;
fn project(slf: PtrInner<'_, Self>) -> *mut Self::Type;
}Expand description
Projects a given field from Self.
All implementations of HasField for a particular field f in Self
should use the same Field type; this ensures that Field is inferable
given an explicit VARIANT_ID and FIELD_ID.
§Safety
A field f is HasField for Self if and only if:
- If
Selfhas the layout of a struct or union type, thenVARIANT_IDisSTRUCT_VARIANT_IDorUNION_VARIANT_IDrespectively; otherwise, ifSelfhas the layout of an enum type,VARIANT_IDis the numerical index of the enum variant in whichfappears. Note thatSelfdoes not need to actually be such a type – it just needs to have the same layout as such a type. For example, a#[repr(transparent)]wrapper around an enum has the same layout as that enum. - If
fhas namen,FIELD_IDiszerocopy::ident_id!(n); otherwise, iffis at indexi,FIELD_IDiszerocopy::ident_id!(i). Fieldis a type with the same visibility asf.Typehas the same type asf.
The caller must not assume that a pointer’s referent being aligned
implies that calling project on that pointer will result in a pointer to
an aligned referent. For example, HasField may be implemented for
#[repr(packed)] structs.
The implementation of project must satisfy its safety post-condition.
Required Associated Types§
Required Methods§
fn only_derive_is_allowed_to_implement_this_trait()where
Self: Sized,
Sourcefn project(slf: PtrInner<'_, Self>) -> *mut Self::Type
fn project(slf: PtrInner<'_, Self>) -> *mut Self::Type
Projects from slf to the field.
Users should generally not call project directly, and instead should
use high-level APIs like [PtrInner::project] or [Ptr::project].
§Safety
The returned pointer refers to a non-strict subset of the bytes of
slf’s referent, and has the same provenance as slf.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.