tyght-map 0.1.0-alpha.0

A static type map implementation.
Documentation

The tyght-map crate provides a static type map implementation.

The map, [TyghtMap], enjoys the following properties:

  • The size of the map will match the size of its items.
  • No heap allocations, this crate is !#[no_std].
  • All methods on the map are infallible.

Example

#![feature(generic_const_exprs)]
# use tyght_map::*;
// Insert some different integer types into the map and check the size
let map = TyghtMap::new()
.insert(3u32)
.insert(4i32)
.insert(3f32);
assert_eq!(std::mem::size_of_val(&map), 12);

// Retrieve the `u32` from the map
let item: &u32 = map.get();
assert_eq!(*item, 3);

// Insert a `String` into the map, then mutate it
let mut map = map.insert("Hello".to_string());
*map.get_mut::<String>() += ", world!";

// Remove the `String` from the map
let (item, _map) = map.remove::<String>();
println!("{item}");

Traits

For each operation on [TyghtMap] there is an associated trait:

This means that placing constraints on the S of TyghtMap<S> acts as a constraint on the items it contains.

For example, the following function cannot be called using a map which does not contain a String and a u32.

# use tyght_map::*;
fn print_string<S>(map: &TyghtMap<S>)
where
S: Contains<String>,
S: Contains<u32>
{
let string: &String = map.get();
let int: &u32 = map.get();
println!("{string} {int}");
}

Known Limitations

Currently, the map can only store 32 items. This limit can be raised if compile-times are known to be reasonable.

Future improvements to rustcs type system may remove the need for a limit all together.

Nightly

In contrast to other attempts, this implementation does not rely on specialization. It does however rely on a variety of nightly features:

These can be expected to be stabilized, in some form, before specialization.