1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
use std::collections::BTreeMap; use std::collections::btree_map::Entry::{ Occupied, Vacant }; use super::{ Update, UpdateOr }; impl<K, V> Update<K, V> for BTreeMap<K, V> where K: Ord , K: Copy { fn update<F>(&mut self, key: &K, f: F) where F: FnOnce(&mut V) { match self.entry(*key) { Vacant(_) => { } , Occupied(mut entry) => { f(entry.get_mut()); } } } } impl<K, V> UpdateOr<K, V> for BTreeMap<K, V> where K: Ord , K: Copy { fn update_or<F>(&mut self, key: &K, f: F, default: V) where F: FnOnce(&mut V) { match self.entry(*key) { Vacant(entry) => { entry.insert(default); } , Occupied(mut entry) => { f(entry.get_mut()); } } } }