gen_map 0.3.0

A generational map with configurable key size, key layout and storage.
Documentation
use crate::{GenMap, GenMapConfig, GenSlotItem, InsertError, MapConfig, Split};
use std::string::{String, ToString};
use std::vec::Vec;

struct Byte;

impl MapConfig for Byte {
    type KeyConfig = Split<u8, u8>;
}

impl<S: GenSlotItem> GenMapConfig<S> for Byte {
    type Storage = Vec<S>;
}

#[test]
fn try_insert_works_like_insert_while_there_is_room() {
    let mut map = GenMap::new();
    let a = map.try_insert(1).unwrap();
    let b = map.try_insert(2).unwrap();
    assert_eq!(map[a], 1);
    assert_eq!(map[b], 2);
    assert_eq!(map.len(), 2);
}

#[test]
fn try_insert_hands_the_value_back_when_the_index_runs_out() {
    let mut map = GenMap::<String, Byte>::new_with_config();
    // No slot gets the index `u8::MAX`, so the map holds 255 values.
    for i in 0..255 {
        map.try_insert(i.to_string()).unwrap();
    }
    match map.try_insert("late".to_string()) {
        Err(InsertError::IndexExhausted(value)) => assert_eq!(value, "late"),
        other => panic!("expected IndexExhausted, got {other:?}"),
    }
    assert_eq!(map.len(), 255);
    assert_eq!(map.slots_len(), 255);
}

#[test]
fn into_inner_takes_the_value_back() {
    assert_eq!(InsertError::<_, ()>::IndexExhausted(5).into_inner(), 5);
    assert_eq!(InsertError::StorageFull("x", ()).into_inner(), "x");
}

#[test]
fn the_error_does_not_need_debug_from_the_value() {
    struct Opaque;
    let text = std::format!("{:?}", InsertError::<_, ()>::IndexExhausted(Opaque));
    assert_eq!(text, "IndexExhausted(..)");
    let text = std::format!("{:?}", InsertError::StorageFull(Opaque, "why"));
    assert_eq!(text, "StorageFull(.., \"why\")");
    let text = std::format!("{}", InsertError::StorageFull(Opaque, "why"));
    assert!(text.contains("storage") && text.contains("why"));
}

#[test]
#[should_panic(expected = "cannot address more than 255 slots")]
fn insert_panics_when_the_index_runs_out() {
    let mut map = GenMap::<i32, Byte>::new_with_config();
    for i in 0..256 {
        map.insert(i);
    }
}