map_ext/lib.rs
1#![cfg_attr( features = "unstable"
2 , feature(augmented_assignments, op_assign_traits) )]
3
4//! Extensions to `std::collections::HashMap` and `std::collections::BTreeMap`.
5
6/// Trait adding an `update` method to maps, allowing their entries to be
7/// updated.
8pub trait Update<K, V> {
9 /// Update the entry for the given key by applying a function to the value.
10 ///
11 /// If there is an entry in the map corresponding to `key`, updates the
12 /// value by applying the function `f` to it. Otherwise, if there is
13 /// no entry, does nothing.
14 ///
15 /// # Arguments
16 /// - `key`: the key to the entry to update
17 /// - `f`: a function taking `&mut V` to update the entry's value
18 ///
19 fn update<F>(&mut self, key: &K, f: F)
20 where F: FnOnce(&mut V);
21}
22
23/// Trait adding an `update_or` method to maps, allowing their entries to be
24/// updated with a default value.
25pub trait UpdateOr<K, V> {
26 /// Update the entry for given key, or insert a default.
27 ///
28 /// If there is an entry in the map corresponding to `key`, updates the
29 /// value by applying the function `f` to it. Otherwise, if there is
30 /// no entry, inserts the default value into the map for that key.
31 ///
32 /// # Arguments
33 /// - `key`: the key to the entry to update
34 /// - `f`: a function taking `&mut V` to update the entry's value
35 /// - `default`: a default value to insert if there is no existing value
36 ///
37 fn update_or<F>(&mut self, key: &K, f: F, default: V)
38 where F: FnOnce(&mut V);
39}
40
41pub mod btree_map;
42pub use self::btree_map::*;
43
44pub mod hash_map;
45pub use self::hash_map::*;
46
47#[cfg(test)]
48mod tests {
49 #[test]
50 fn it_works() {
51 }
52}