Derive Macro Object
#[derive(Object)]
{
// Attributes available to this derive:
#[object]
#[reflect]
}
Expand description
Bind an object type that another library, usually C++, registers: derive
ObjectCore for a #[repr(C)] object struct whose
first field is its parent, so that a pointer to the object is one to its
parent. Define a new type in Rust with rs_object
instead.
Without register or type_index, type_index() takes the index the
defining library registered for the key, and panics if that library is not
loaded yet, so a binding never registers a key with the wrong child slots.
Such a panic is final: type_index() of the type, and of every type under
it, keeps panicking after the library loads, so call it only once the
library is loaded. It also panics unless the registered key has the type’s
parent and depth and, when the type table records a size, a size no
smaller than the type’s.
A binding that declares only part of its object must not be constructed: keep its fields private.
§Options
#[object(...)] takes a comma-separated list, as in
#[object(type_key = "ffi.String", unsafe type_index = TypeIndex::kTVMFFIStr)]:
type_key = "..."- The type key; required.unsafe type_index = expr- A static index instead of the registered one; theunsafemarks the caller’s promise that the runtime gives that index to this type.final- The type has no subtypes, as C++_type_final.immutable- ImplementImmutableObject, as for a C++ type not registered as_type_mutable; every subtype must be immutable too.rs_parent- ImplementShallowCopyandUnsafeInit, so anrs_objectcan derive from the type. Each field other than the parent must beCloneand implementAnyCompatible, be anAnyor have no drop glue.register- The type is defined here: register its key under its parent when the library loads, asrs_objectdoes.child_slots = N,child_slots_can_overflow = bool- Type indices kept for subtypes, as C++_type_child_slotsand_type_child_slots_can_overflow; 0 andtrueby default. Needsregister.s_eq_hash_kind = "TreeNode"- TheSEqHashKindof structural equality; by default the parent’s.reflection = path- Afn(&mut ObjectDef<Self>)that registers the type’s methods.
s_eq_hash_kind, reflection and field #[reflect(...)], as
rs_object takes it, need register and no
type_index.