Skip to main content

Upcast

Trait Upcast 

Source
pub trait Upcast: Sealed {
    type Pointee;
    type Cast<B>;

    // Required method
    fn upcast<B>(self) -> Self::Cast<B>
       where Self::Pointee: Inherits<B>;
}
Expand description

Converts a raw pointer to a class into a pointer to one of its base classes, keeping it *mut or *const.

A plain cast, valid because of Inherits. It never dereferences, so null and dangling pointers stay null or dangling. Implemented only for raw pointers.

§Examples

A type that isn’t a base doesn’t compile:

ⓘ
use imping_runtime::Upcast;

#[repr(C)]
struct Entity { health: i32 }
#[repr(C)]
struct Item { count: i32 }

let item: *mut Item = core::ptr::null_mut();
let entity: *mut Entity = item.upcast(); // `Item` does not inherit `Entity`

Required Associated Types§

Source

type Pointee

The type the pointer points to.

Source

type Cast<B>

The same kind of pointer (*mut or *const) to a base class B.

Required Methods§

Source

fn upcast<B>(self) -> Self::Cast<B>
where Self::Pointee: Inherits<B>,

Returns this pointer as a pointer to its base class B.

B is usually inferred from where the result goes; name it with upcast::<B>() otherwise.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> Upcast for *const T

Source§

type Pointee = T

Source§

type Cast<B> = *const B

Source§

fn upcast<B>(self) -> *const B
where T: Inherits<B>,

Source§

impl<T> Upcast for *mut T

Source§

type Pointee = T

Source§

type Cast<B> = *mut B

Source§

fn upcast<B>(self) -> *mut B
where T: Inherits<B>,

Implementors§