Skip to main content

Inherits

Trait Inherits 

Source
pub unsafe trait Inherits<T: GarbageCollected + 'static>: GarbageCollected + 'static { }
Expand description

Indicates that Self is a CppGC type that structurally inherits from T.

When implemented for a type, it declares that Self contains T as its first field (at offset 0) in a #[repr(C)] layout. This enables the runtime to safely reinterpret a pointer to the derived type as a pointer to the base type, allowing base-class methods to operate on derived instances.

This trait is transitive: if C: Inherits<B> and B: Inherits<A>, then C: Inherits<A> is also implemented automatically by the derive macro.

§Safety

The implementor must guarantee that:

  1. Self is #[repr(C)].
  2. The first field of Self is of type T and is at offset 0.
  3. T is a valid CppGC type (implements GarbageCollected).

These invariants ensure that a CppGcObject<Self> pointer can be safely cast to CppGcObject<T>, because the tag field and the base type’s fields are at the same memory offsets in both layouts.

Use #[derive(CppgcInherits)] with #[cppgc_inherits_from(BaseType)] instead of implementing this trait manually. The derive macro validates the layout requirements at compile time.

Implementors§