Crate mezzaluna_type_registry

source ·
Expand description

A registry for “type spec” values that uses types as keys.

This data structure is used for associating data type metadata with a Rust type which can be used to ensure the lifetime of the associated metadata.

The registry resembles an append-only HashMap.

The registry stores values behind a Box pointer to ensure pointers to the interior of the spec, like CString fields, are not invalidated as the underlying storage reallocates.

§Example

use mezzaluna_type_registry::Registry;

let mut reg: Registry<&'static str> = Registry::with_capacity(10);
reg.insert::<i32>(Box::new("Numeric"));
reg.insert::<Vec<u8>>(Box::new("String"));

assert_eq!(reg.get::<i32>(), Some(&"Numeric"));
assert_eq!(reg.get::<Vec<u8>>(), Some(&"String"));
assert_eq!(reg.get::<f64>(), None);

§Motivating use case: mrb_data_type

In the mruby C API, custom data types define a mrb_data_type struct which contains the custom data type’s module name and free function. The C API requires that this struct live at least as long as the mrb_state. Typically, the mrb_data_type is static.

static const struct mrb_data_type mrb_time_type = { "Time", mrb_free };

Structs§

  • A registry for “type spec” values that uses types as keys.
  • An iterator of all type specs stored in the Registry.