Skip to main content

Base

Trait Base 

Source
pub unsafe trait Base: GarbageCollected + 'static {
    // Provided method
    fn inheriting_types() -> &'static [TypeId] { ... }
}
Expand description

Marks a CppGC type as a base class that other types may inherit from.

Types implementing Base can be used with [try_unwrap_cppgc_base_object], which accepts both the base type itself and any type that implements Inherits<Self>. This is what powers polymorphic method dispatch on CppGC objects: methods defined on a base class can be called on instances of any derived class.

The trait provides inheriting_types, which returns the TypeIds of all types that transitively inherit from Self. This list is computed once (lazily) using the inheritance graph built by the CppgcInherits derive macro and cached for the lifetime of the program.

§Safety

The implementor must guarantee that:

  1. Self is #[repr(C)] and non-zero-sized.
  2. Any type D for which D: Inherits<Self> holds has Self embedded at offset 0, so that a CppGcObject<D> can be safely reinterpreted as CppGcObject<Self>.

Use #[derive(CppgcBase)] instead of implementing this trait manually.

Provided Methods§

Source

fn inheriting_types() -> &'static [TypeId]

Returns the TypeIds of all types that transitively inherit from this type.

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§