pub unsafe trait VTable: Clone {
type Bounds: IsBound;
type Id: PartialEq + Eq;
// Required methods
fn descriptor(&self) -> &Descriptor<Self::Id>;
fn bound_impl(&self) -> &BoundsImpl<Self::Bounds>;
}Expand description
Virtual method table and type descriptor.
Rust automatically generates vtables for trait objects. However, its not currently possible to access these vtables directly on stable. Instead, this trait provides a unified interface for custom vtables. Custom vtables can be generated thanks to Rust’s ability to static promote constants to give them stable addresses.
A vtable is created from a type Descriptor and a BoundsImpl. A Descriptor
describes a type, and includes things like the type’s size and name. The
Descriptor::is_type() method can be used to check if a vtable is for a particular type. A
BoundsImpl contains the implementations of the virtual methods for the type.
Currently, only Drop, Clone, and
Debug are included in the virtual methods.
All dungeon_cell types use this trait as the basis for their type erasure.
Use VTableOf or ConstVTableOf to construct a instance of a generic vtable for a type.
Additionally, use VTableSubset to reduce the number of traits bound by Self::Bounds.
§Safety
- The type described by
Self::descriptor()must be the same thatSelf::bound_implwas created for. This means callingDescriptor::new::<T>()andBoundsImpl::new::<T>()with the sameT. - The type
Self::Idmust be well behaved. This means that when performing equality between twoSelf::Idif the result istruethen the typeTthatSelf::descriptor()describes is the same type. Additionally,Inspect::<Self::Id>::inspect::<T>()must return theSelf::Idfor the typeTfollowing this equality. - The type the vtable describes must implement the traits given by
Self::Bounds.
Required Associated Types§
Required Methods§
Sourcefn descriptor(&self) -> &Descriptor<Self::Id>
fn descriptor(&self) -> &Descriptor<Self::Id>
Get the type descriptor.
This is the descriptor for the type the vtable was made for.
Sourcefn bound_impl(&self) -> &BoundsImpl<Self::Bounds>
fn bound_impl(&self) -> &BoundsImpl<Self::Bounds>
Get the bound implementation.
This is the method implementation for the type the vtable was made for.
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.