Crate typemap_meta

source ·
Expand description

A simple compile-time derive macro to create type-to-value maps.

This approach in contrast to crates such as typemap or type-map that perform run-time lookup. The static typing brings compile-time safety and faster execution at the expense of using a derive macro and generics.

The crate is no_std compatible.

Example

#[derive(Typemap)]
struct Test(i32, f32);

let t = Test(1, 2.0);
assert_eq!(*get!(t, i32), 1);
assert_eq!(*get!(t, f32), 2.0);

To get mutable references, add the #[typemap_mut] attribute on your struct, and use get_mut! instead of get!:

#[derive(Typemap)]
#[typemap_mut]
struct Test(i32, f32);

let mut t = Test(1, 2.0);
assert_eq!(*get!(t, i32), 1);
assert_eq!(*get!(t, f32), 2.0);

*get_mut!(t, i32) = 3;
*get_mut!(t, f32) = 4.0;

assert_eq!(*get!(t, i32), 3);
assert_eq!(*get!(t, f32), 4.0);

Macros

  • Convenience macro to get a specific type $t from a tuple struct $s containing disjoint heterogeneous types
  • Convenience macro to mutably get a specific type $t from a tuple struct $s containing disjoint heterogeneous types

Traits

  • Helper trait to get a specific type T from a tuple struct containing disjoint heterogeneous types
  • Helper trait to mutably get a specific type T from a tuple struct containing disjoint heterogeneous types

Derive Macros

  • Add static type-to-value getters to a tuple struct containing disjoint heterogeneous types