//! Utilities for internal use and other cool stuff use std::collections::HashMap; use std::hash::Hash; /// drain_filter() implemented for HashMap. It returns the removed items as a Vec pub fn map_drain_filter( map: &mut HashMap, filter: impl Fn(&K, &V) -> bool, ) -> Vec<(K, V)> { let mut removed = vec![]; let mut retain = vec![]; for (k, v) in map.drain() { if filter(&k, &v) { removed.push((k, v)); } else { retain.push((k, v)); } } map.extend(retain); removed } /// Get the first or second item from a Vec of (Key, Value) pairs. /// Use when only one part of the pair is needed pub trait KVVecToKeysOrValues { /// Get the first item of each tuple fn keys(self) -> Vec; /// Get the second item of each tuple fn values(self) -> Vec; } impl KVVecToKeysOrValues for Vec<(K, V)> { fn keys(self) -> Vec { self.into_iter().map(|(k, _v)| k).collect() } fn values(self) -> Vec { self.into_iter().map(|(_k, v)| v).collect() } } pub(crate) trait IsNoneOrElse: Sized { //noinspection RsSelfConvention fn is_none_or_else(&self, test: impl FnOnce(&T) -> bool) -> bool; } impl IsNoneOrElse for Option { fn is_none_or_else(&self, test: impl FnOnce(&T) -> bool) -> bool { match self { None => true, Some(value) => test(value), } } }