Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Add map and try_map methods to BoundedBTreeMap (#11869)
Browse files Browse the repository at this point in the history
* Add map and try_map methods to BoundedBTreeMap

* Undo changes to basic_authorship.rs

* Remove unwrap and use unchecked_from instead

* Add iter_mut() method

* Remove map functions and add docs to iter_mut

* fmt

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
  • Loading branch information
benluelo and shawntabrizi authored Aug 11, 2022
1 parent dc45cc2 commit 469189b
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion primitives/runtime/src/bounded/bounded_btree_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ where
{
self.0.remove_entry(key)
}

/// Gets a mutable iterator over the entries of the map, sorted by key.
///
/// See [`BTreeMap::iter_mut`] for more information.
pub fn iter_mut(&mut self) -> sp_std::collections::btree_map::IterMut<K, V> {
self.0.iter_mut()
}
}

impl<K, V, S> Default for BoundedBTreeMap<K, V, S>
Expand Down Expand Up @@ -508,7 +515,7 @@ pub mod test {
b1.iter().map(|(k, v)| (k + 1, *v)).take(2).try_collect().unwrap();
assert_eq!(b2.into_iter().map(|(k, _)| k).collect::<Vec<_>>(), vec![2, 3]);

// but these worn't work
// but these won't work
let b2: Result<BoundedBTreeMap<u32, (), ConstU32<3>>, _> =
b1.iter().map(|(k, v)| (k + 1, *v)).try_collect();
assert!(b2.is_err());
Expand All @@ -517,4 +524,17 @@ pub mod test {
b1.iter().map(|(k, v)| (k + 1, *v)).skip(2).try_collect();
assert!(b2.is_err());
}

#[test]
fn test_iter_mut() {
let mut b1: BoundedBTreeMap<u8, u8, ConstU32<7>> =
[1, 2, 3, 4].into_iter().map(|k| (k, k)).try_collect().unwrap();

let b2: BoundedBTreeMap<u8, u8, ConstU32<7>> =
[1, 2, 3, 4].into_iter().map(|k| (k, k * 2)).try_collect().unwrap();

b1.iter_mut().for_each(|(_, v)| *v *= 2);

assert_eq!(b1, b2);
}
}

0 comments on commit 469189b

Please sign in to comment.