register

Attribute Macro register 

Source
#[register]
Expand description

Implements the Registered trait for a type and registers the type with the given type-registry. Can be applied to structs, enums and unions.

use std::fmt::Debug;
use type_registry::Registry;
use type_registry_register_macro::register;

struct MyTypeInfo {
    very_important_info: usize
}

impl MyTypeInfo {
    // The macro assumes that the type-info type has a const, no-argument `new` fn like this,
    // which is generic on the type being registered (the generic parameter can have bounds,
    // like the implicit `Sized` bound here.
    pub const fn new<T>() -> Self {
        Self {
            very_important_info: std::mem::size_of::<T>()
        }
    }

    // If this is not the case, or custom initialisation is needed for some other reason, the
    // initialisation can be overridden.
    pub const fn new_unsized(number: usize) -> Self {
        Self {
            very_important_info: number
        }
    }
}

struct MyRegistry;

impl Registry for MyRegistry {
    type TypeInfo = MyTypeInfo;

    fn name() -> &'static str {
        "My Registry"
    }
}

#[register(MyRegistry, MyTypeInfo::new_unsized(42))]
struct MyStruct([u8]);

#[register(MyRegistry)]
enum MyEnum {}

pub mod reexport {
    pub use type_registry as type_registry_reexported;
}

#[register(MyRegistry)]
// If the macro has been exported from a non-standard path, use this attribute to customise it
#[type_registry(crate = reexport::type_registry_reexported)]
union MyUnion { u8: u8, u16: u16 }