https://github.com/milesgranger/baggie
Container for mixed / heterogeneous values, in the form of a HashMap
https://github.com/milesgranger/baggie
bag collection collections hashmap rust
Last synced: 7 months ago
JSON representation
Container for mixed / heterogeneous values, in the form of a HashMap
- Host: GitHub
- URL: https://github.com/milesgranger/baggie
- Owner: milesgranger
- License: mit
- Created: 2018-12-23T11:11:59.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-04-29T19:40:04.000Z (about 5 years ago)
- Last Synced: 2024-04-24T20:28:27.668Z (about 2 years ago)
- Topics: bag, collection, collections, hashmap, rust
- Language: Rust
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# baggie
---
[](https://travis-ci.com/milesgranger/baggie)
[](https://crates.io/crates/baggie)
[](https://coveralls.io/github/milesgranger/baggie?branch=master)
`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 `Any`s 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. :)
```rust
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::("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"]);
```