pub unsafe trait Superclass: Sized {
// Required method
fn vmt_rva() -> Rva;
// Provided methods
fn vmt_va() -> Va { ... }
fn vmt(&self) -> Va { ... }
fn is_subclass<T: Subclass<Self>>(&self) -> bool { ... }
fn as_subclass<T: Subclass<Self>>(&self) -> Option<&T> { ... }
fn as_subclass_mut<T: Subclass<Self>>(&mut self) -> Option<&mut T> { ... }
}Expand description
A trait for C++ types that have multiple different subclasses. Implementing this for a superclass and Subclass for its subclasses makes it possible to safely check for the object’s actual type based on its vtable.
§Safety
In order to implement this for a struct, you must guarantee:
- The struct uses C-style layout.
- The first element of the struct is a pointer to a C++-style vtable.
- There’s a 1-to-1 correspondence between vtable address and C++ class.
Required Methods§
Provided Methods§
Sourcefn is_subclass<T: Subclass<Self>>(&self) -> bool
fn is_subclass<T: Subclass<Self>>(&self) -> bool
Returns whether this is an instance of T.
Note: Because this just checks the address of the virtual method
table, it will return false for subclasses of T even though C++
considers them to be of type T.
Sourcefn as_subclass<T: Subclass<Self>>(&self) -> Option<&T>
fn as_subclass<T: Subclass<Self>>(&self) -> Option<&T>
Returns this as a T if it is one. Otherwise, returns None.
Note: Because this just checks the address of the virtual method
table, it will return None for subclasses of T even though C++
considers them to be of type T.
Sourcefn as_subclass_mut<T: Subclass<Self>>(&mut self) -> Option<&mut T>
fn as_subclass_mut<T: Subclass<Self>>(&mut self) -> Option<&mut T>
Returns this as a mutable T if it is one. Otherwise, returns None.
Note: Because this just checks the address of the virtual method
table, it will return None for subclasses of T even though C++
considers them to be of type T.
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.