Trait cpp_utils::UnsafeStaticCast
source · pub trait UnsafeStaticCast<T> {
unsafe fn static_cast(&self) -> &T;
unsafe fn static_cast_mut(&mut self) -> &mut T;
}
Expand description
Provides access to C++ static_cast
conversion from base class to derived class.
This trait is automatically implemented by cpp_to_rust
.
If T1
class is derived (in C++) from T2
class,
UnsafeStaticCast<T1>
is implemented for T2
.
UnsafeStaticCast
allows to convert a reference to a class into
a reference to a derived class without runtime check of the type.
Casting from base class type to a derived class type which is
not the actual type of the object will result in an invalid reference.
unsafe_static_cast
and unsafe_static_cast_mut
free functions can be used
to convert pointer types.
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 cpp_to_rust
do not have any information about these implementation details,
so all calls of static_cast
are wrapper in FFI functions.
Still, static_cast
is faster than casts with runtime checks on C++ side
because runtime overhead of Rust wrapper functions is the same for all cast types.
Required Methods
sourceunsafe fn static_cast(&self) -> &T
unsafe fn static_cast(&self) -> &T
Convert type of a const reference.
sourceunsafe fn static_cast_mut(&mut self) -> &mut T
unsafe fn static_cast_mut(&mut self) -> &mut T
Convert type of a mutable reference.