object

Attribute Macro object 

Source
#[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 for T to 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 of T and all its parents

  • __*Builders: Methods to easily initialize the struct using the builder pattern

  • __oors_recursive_impl_*!(/* type */): Generated macro to implement IsA</* all parents of T */> to T and its children

  • Object: Allows at runtime to know what is the actual type + the parents of T when inside a Typed<T>

Notes:

  • This macro will also cause the object to have a C layout (#[repr(C)]) to ensure predictability
  • (Unless the feature nightly is 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 of C using 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)