baggie 0.2.1

Container for storing mixed / heterogeneous values in a common structure
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 9.27 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.44 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • milesgranger/baggie
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • milesgranger

baggie


Build Status crates.io Coverage Status

Baggie is simple interface for storing any type of element in a HashMap. The crate has no dependencies, and is really just a helper around storing and fetching Anys from a HashMap. It has no unsafe code and free of any unwraps or similar misgivings.

The Baggie implements a subset of methods found in HashMap.

The downside of this crate is you must know the type of what you stored later on. Typically this shouldn't be a problem, as you could keep some metadata structure describing what types belong to what keys you've stored.

Sometimes you might need a tool like this, but most times you should be using an enum. :)

use baggie::Baggie;

let mut bag = Baggie::new();

// Insert any value type you wish...
bag.insert("key1", "Value1".to_owned());
bag.insert("key2", vec!["value", "2"]);
bag.insert("key3", 3);

// Get a reference
let val3 = bag.get::<i32, _>("key3");
assert_eq!(Some(&3), val3);

// Get a mutable reference
let val2: Option<&mut Vec<&str>> = bag.get_mut("key2");
match val2 {
    Some(v) => *v = vec!["new", "value", "2"],
    None => panic!()
}
let val2: &mut Vec<&str> = bag.get_mut("key2").unwrap();
assert_eq!(val2, &mut vec!["new", "value", "2"]);