[][src]Trait cpp_core::StaticDowncast

pub trait StaticDowncast<T>: Sized {
    unsafe fn static_downcast(ptr: Ptr<Self>) -> Ptr<T>;

Converts a class pointer to a base class pointer without a runtime check.

A null pointer is always converted to a null pointer. Otherwise, the provided pointer must be valid and point to the object of the specified type.

It's generally safer to use DynamicCast instead because it performs a checked conversion. This trait should only be used if the type of the object is known.

It's recommended to perform the conversion by calling static_downcast method on pointer types (CppBox, Ptr, Ref) instead of importing the trait directly.

Provides access to C++ static_cast conversion from base class to derived class. The conversion in opposite direction can be done with StaticUpcast.

If T1 class is derived (in C++) from T2 class (directly or indirectly), StaticDowncast<T1> is implemented for T2. The implementation is generated by ritual automatically.

Note that Rust functions associated with this trait have runtime overhead. In C++, static_cast is usually a no-op if there is no multiple inheritance, and multiple inheritance requires pointer adjustment. However, Rust compiler and ritual do not have any information about these implementation details, so all calls of static_cast are wrapper in FFI functions.

Required methods

unsafe fn static_downcast(ptr: Ptr<Self>) -> Ptr<T>

Convert type of a const pointer.


This operation is safe as long as ptr is either null or points to an object of the T class or a class inherited by T.

Loading content...


Loading content...