map_overview/
map_overview.rs

1use singletons::SingletonMap;
2
3fn main() {
4    // Create a map that stores String descriptions for different types
5    let mut type_descriptions: SingletonMap<String> = SingletonMap::new();
6
7    // Insert descriptions for various types
8    type_descriptions.insert::<u8>("An unsigned 8-bit integer (0 to 255)".to_string());
9    type_descriptions.insert::<i8>("A signed 8-bit integer (-128 to 127)".to_string());
10    type_descriptions.insert::<u16>("An unsigned 16-bit integer".to_string());
11    type_descriptions.insert::<String>("A heap-allocated string".to_string());
12    type_descriptions.insert::<Vec<u32>>("A vector of 32-bit integers".to_string());
13
14    // Retrieve descriptions
15    println!("u8: {}", type_descriptions.get::<u8>().unwrap());
16    println!("i8: {}", type_descriptions.get::<i8>().unwrap());
17    println!("String: {}", type_descriptions.get::<String>().unwrap());
18
19    // Modify a description
20    if let Some(desc) = type_descriptions.get_mut::<u16>() {
21        desc.push_str(" (0 to 65,535)");
22    }
23    println!("u16: {}", type_descriptions.get::<u16>().unwrap());
24
25    // Use entry API
26    type_descriptions
27        .entry::<f64>()
28        .or_insert("A 64-bit floating-point number".to_string());
29
30    // Update existing entry
31    type_descriptions
32        .entry::<u8>()
33        .and_modify(|desc| desc.push_str(" - commonly used for bytes"))
34        .or_insert("Should not be inserted".to_string());
35
36    println!("\nAll type descriptions:");
37    for (type_key, description) in type_descriptions.iter() {
38        println!("  {}: {}", type_key.as_name(), description);
39    }
40
41    println!("\nTotal types documented: {}", type_descriptions.len());
42
43    // Example with default values
44    let mut counters: SingletonMap<i32> = SingletonMap::new();
45    *counters.get_or_insert_default::<u8>() += 5;
46    *counters.get_or_insert_default::<i8>() -= 3;
47
48    println!("\nCounters:");
49    println!("  u8 counter: {}", counters.get::<u8>().unwrap());
50    println!("  i8 counter: {}", counters.get::<i8>().unwrap());
51}