HasField

Trait HasField 

Source
#[doc(hidden)] pub unsafe trait HasField<Field, const VARIANT_ID: i128, const FIELD_ID: i128> {
    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 Self is a struct or union type, then VARIANT_ID is STRUCT_VARIANT_ID or UNION_VARIANT_ID respectively; otherwise, if Self is an enum type, VARIANT_ID is the numerical index of the enum variant in which f appears.
  • If f has name n, FIELD_ID is zerocopy::ident_id!(n); otherwise, if f is at index i, FIELD_ID is zerocopy::ident_id!(i).
  • Field is a type with the same visibility as f.
  • Type has the same type as f.

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§

Source

type Type: ?Sized

The type of the field.

Required Methods§

Source

fn only_derive_is_allowed_to_implement_this_trait()
where Self: Sized,

Source

fn project(slf: PtrInner<'_, Self>) -> *mut Self::Type

Projects from slf to the field.

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

Implementors§