use collections::BTreeMap; use spin::Mutex; use sync::WaitCondition; #[derive(Debug)] pub struct WaitMap { inner: Mutex>, condition: WaitCondition } impl WaitMap where K: Ord { pub fn new() -> WaitMap { WaitMap { inner: Mutex::new(BTreeMap::new()), condition: WaitCondition::new() } } pub fn send(&self, key: K, value: V) { self.inner.lock().insert(key, value); self.condition.notify(); } pub fn receive(&self, key: &K) -> V { loop { if let Some(value) = self.inner.lock().remove(key) { return value; } self.condition.wait(); } } }