baggie/lib.rs
1#![warn(missing_docs)]
2//! [`Baggie`] is simple interface for storing any type of element in a [`HashMap`]. The crate
3//! has no dependencies, and is really just a helper around storing and fetching [`Any`]s from
4//! a [`HashMap`]. It has no `unsafe` code and free of any `unwrap`s or similar misgivings.
5//!
6//! The [`Baggie`] implements a subset of methods found in [`HashMap`].
7//!
8//! The downside of this crate is you _must_ know the type of what you stored later on. Typically
9//! this shouldn't be a problem, as you could keep some _metadata_ structure describing what types
10//! belong to what keys you've stored.
11//!
12//! _Sometimes_ you might need a tool like this, but _most times_ you should be using an [`enum`]. :)
13//!
14//! ## Example
15//! ```
16//! use baggie::Baggie;
17//!
18//! let mut bag = Baggie::new();
19//!
20//! // Insert any value type you wish...
21//! bag.insert("key1", "Value1".to_owned());
22//! bag.insert("key2", vec!["value", "2"]);
23//! bag.insert("key3", 3);
24//!
25//! // Get a reference
26//! let val3 = bag.get::<i32, _>("key3");
27//! assert_eq!(Some(&3), val3);
28//!
29//! // Get a mutable reference
30//! let val2: Option<&mut Vec<&str>> = bag.get_mut("key2");
31//! match val2 {
32//! Some(v) => *v = vec!["new", "value", "2"],
33//! None => panic!()
34//! }
35//! let val2: &mut Vec<&str> = bag.get_mut("key2").unwrap();
36//! assert_eq!(val2, &mut vec!["new", "value", "2"]);
37//!
38//! ```
39//! [`enum`]: https://doc.rust-lang.org/book/enums.html
40//! [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html
41//! [`Any`]: https://doc.rust-lang.org/std/any/trait.Any.html
42//! [`Baggie`]: struct.Baggie.html
43
44
45mod baggie;
46pub use self::baggie::Baggie;