From 2bda6abae791ef642292299d94654c9b5b16d37b Mon Sep 17 00:00:00 2001 From: benluelo <57334811+benluelo@users.noreply.github.com> Date: Thu, 11 Aug 2022 14:55:30 -0400 Subject: [PATCH] Add map and try_map methods to BoundedBTreeMap (#11869) * 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> --- .../runtime/src/bounded/bounded_btree_map.rs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/substrate/primitives/runtime/src/bounded/bounded_btree_map.rs b/substrate/primitives/runtime/src/bounded/bounded_btree_map.rs index aefd168632a..725864b4626 100644 --- a/substrate/primitives/runtime/src/bounded/bounded_btree_map.rs +++ b/substrate/primitives/runtime/src/bounded/bounded_btree_map.rs @@ -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> @@ -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()); @@ -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); + } } -- GitLab