use std::cmp::Ordering;
use std::fmt;
use std::collections::BTreeMap;
use ordered::{ArbitraryOrd, Ordered};
fn main() {
b_tree_map();
derive_and_access();
}
fn b_tree_map() {
let mut map = BTreeMap::new();
let a = Foo::Space(50);
let b = Foo::Time(50);
map.insert(Ordered(a), "some interesting value");
map.insert(Ordered(b), "some other interesting value");
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Foo {
Space(u32),
Time(u32)
}
impl ArbitraryOrd for Foo {
fn arbitrary_cmp(&self, other: &Self) -> Ordering {
use Foo::*;
match (self, other) {
(Space(_), Time(_)) => Ordering::Less,
(Time(_), Space(_)) => Ordering::Greater,
(Space(this), Space(that)) => this.cmp(that),
(Time(this), Time(that)) => this.cmp(that),
}
}
}
impl fmt::Display for Foo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Foo::Space(x) => write!(f, "Space {}", x),
Foo::Time(x) => write!(f, "Time {}", x),
}
}
}
fn derive_and_access() {
let adt = Adt { x: 42, p: Foo::Space(50).into() };
println!("We can explicitly deref: {}", *adt.p);
println!("Or use deref coercion: {}", adt.p);
println!("Or we can use borrow: {}", &adt.p);
println!("Explicitly get a reference: {}", adt.p.as_inner());
println!("Or the inner type: {}", adt.p.into_inner());
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Adt {
x: u32,
p: Ordered<Foo>,
}