extern crate total_order_multi_map as tomm;
use std::fmt::{self, Display};
use tomm::TotalOrderMultiMap;
type Key = &'static str;
type Value = Box<Display>;
fn main() {
let mut map = TotalOrderMultiMap::<Key, Value>::new();
map.add("key1", mk_box_str("val1"));
map.add("key1", mk_my_thingy("val2"));
map.add("key2", mk_box_str("val3"));
map.add("key1", mk_my_thingy("val4"));
map.add("key0", mk_box_str("val5"));
let stringed = map
.iter()
.map(|(key, val)| format!("{}:{}", key, val))
.collect::<Vec<_>>();
assert_eq!(stringed, &[
"key1:val1".to_owned(),
"key1:val2".to_owned(),
"key2:val3".to_owned(),
"key1:val4".to_owned(),
"key0:val5".to_owned()
]);
{
let values = map.get("key1");
let stringed = values
.map(|val| format!("{}", val))
.collect::<Vec<_>>();
assert_eq!(stringed, &[ "val1", "val2", "val4" ]);
}
let (key, val) = map.pop().unwrap();
assert_eq!(format!("{}:{}", key, val), "key0:val5");
map.remove_all("key1");
assert_eq!(0, map.get("key1").len(), "expected no values for key1");
println!("DONE (I only contain asserts)")
}
fn mk_box_str(inp: &str) -> Box<Display> {
Box::new(inp.to_owned())
}
#[derive(Debug)]
struct MyThingy { val: String }
impl Display for MyThingy {
fn fmt(&self, fter: &mut fmt::Formatter) -> fmt::Result {
write!(fter, "{}", self.val)
}
}
fn mk_my_thingy(inp: &str) -> Box<Display> {
Box::new(MyThingy { val: inp.to_owned() })
}