#[cfg(feature = "map_first_last")] pub trait BTreeMapExt { fn first_key(&self) -> Option<&K>; fn last_key(&self) -> Option<&K>; fn pop_first_value(&mut self) -> Option; fn pop_last_value(&mut self) -> Option; } #[cfg(feature = "map_first_last")] impl BTreeMapExt for std::collections::BTreeMap { fn first_key(&self) -> Option<&K> { self.keys().next() } fn last_key(&self) -> Option<&K> { self.keys().next_back() } fn pop_first_value(&mut self) -> Option { self.first_entry().map(|x| x.remove()) } fn pop_last_value(&mut self) -> Option { self.last_entry().map(|x| x.remove()) } } #[cfg(not(feature = "map_first_last"))] pub trait BTreeMapExt { fn first_key(&self) -> Option<&K>; fn last_key(&self) -> Option<&K>; fn pop_first_key(&mut self) -> Option; fn pop_last_key(&mut self) -> Option; fn pop_first_value(&mut self) -> Option; fn pop_last_value(&mut self) -> Option; } #[cfg(not(feature = "map_first_last"))] impl BTreeMapExt for std::collections::BTreeMap { fn first_key(&self) -> Option<&K> { self.keys().next() } fn last_key(&self) -> Option<&K> { self.keys().next_back() } fn pop_first_key(&mut self) -> Option { let key = self.first_key()?; let key = key.clone(); // ownership hack Some(self.remove_entry(&key).unwrap().0) } fn pop_last_key(&mut self) -> Option { let key = self.last_key()?; let key = key.clone(); // ownership hack Some(self.remove_entry(&key).unwrap().0) } fn pop_first_value(&mut self) -> Option { let key = self.first_key()?; let key = key.clone(); // ownership hack Some(self.remove_entry(&key).unwrap().1) } fn pop_last_value(&mut self) -> Option { let key = self.last_key()?; let key = key.clone(); // ownership hack Some(self.remove_entry(&key).unwrap().1) } } #[cfg(feature = "map_first_last")] pub trait BTreeSetExt {} #[cfg(not(feature = "map_first_last"))] pub trait BTreeSetExt { fn last(&self) -> Option<&K>; fn pop_last(&mut self) -> Option; } #[cfg(not(feature = "map_first_last"))] impl BTreeSetExt for std::collections::BTreeSet { fn last(&self) -> Option<&K> { self.iter().next_back() } fn pop_last(&mut self) -> Option { #[allow(unstable_name_collisions)] let key = self.last()?; let key = key.clone(); // ownership hack self.take(&key) } }