#[object]Expand description
Add inheritance capabilities to your struct.
§Syntax
#[oors::object]
struct T { /* ... */ }
#[oors::object(parent = T)]
struct T2 { /* ... */ } // First field will be _base: T§How it works
This macro will generate and implement various traits + a macro to allow structs to be safely converted to other compatible structs:
-
IsA</* immediate parent */> for T: Allows at compile time forTto have access to parent structs’ methods and fields, and to be safely converted to parent structs -
__*Accessors: Methods to have direct and easy access to field ofTand all its parents -
__*Builders: Methods to easily initialize the struct using the builder pattern -
__oors_recursive_impl_*!(/* type */): Generated macro to implementIsA</* all parents of T */>toTand its children -
Object: Allows at runtime to know what is the actual type + the parents ofTwhen inside aTyped<T>
Notes:
- This macro will also cause the object to have a C layout (
#[repr(C)]) to ensure predictability - (Unless the feature
nightlyis enabled) If you inherit from an object (C) from a foreign module/crate (mod1), this macro requires importing the module and all modules containing parent ofCusing the wildcard syntax:
// prefer `pub use` so other modules doesn't have to `use` `mod1` or `parent_of_c`
// to make a child struct of `D`
pub use mod1::*;
pub use crate_b::parent_of_c::*;
// Will use __oors_recursive_impl_C!(...) which
// will use __oors_recursive_impl_<parent of C>!()
// etc.
#[oors::object(parent = C)]
struct D;(if nightly is enabled, the macro will generate __oors_recursive_impl_*!() as hygienic pub macro
removing the need of importing anything other than the __oors_recursive_impl_</* immediate parent */> itself)