typemap-meta 0.2.0

A simple compile-time macro to create type-to-value maps
Documentation
  • Coverage
  • 71.43%
    5 out of 7 items documented1 out of 5 items with examples
  • Size
  • Source code size: 28.95 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 858.69 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • enlightware/typemap-meta
    1 2 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • stephanemagnenat

typemap-meta

Crates.io Docs.rs Build Status

An Enlightware® software.

Overview

A simple compile-time (hence the meta) derive macro to create type-to-value maps (hence the typemap). 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.

Usage

To use this crate, first add this to your Cargo.toml:

[dependencies]
typemap-meta = "0.2"

Then, you can create a tuple struct containing disjoint heterogeneous types, and derive Typemap, and then use the get! macro (a syntactic sugar around Typemap::Get):

#[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);

A mutable version is also available:

#[derive(Typemap)]
#[typemap_mut]
struct Test(i32, f32);
let mut t = Test(1, 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);

Crate structure

As currently procedural macros must be defined in their own crate, we have two crates typemap-meta and typemap-meta-derive, the former re-exporting the macro from the later. Only the former needs to be imported in your project.

License

Licensed under either of

at your option.