From 5119479ec01f59ce6de1e8c123b31919ad40ed65 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Tue, 7 Jun 2022 14:14:24 +0100 Subject: [PATCH] Implement more IntoIter traits for bounded types --- .../support/src/storage/bounded_btree_map.rs | 18 ++++++++++++++ .../support/src/storage/bounded_btree_set.rs | 9 +++++++ frame/support/src/storage/bounded_vec.rs | 24 +++++++++++++++++++ frame/support/src/storage/weak_bounded_vec.rs | 16 +++++++++++++ 4 files changed, 67 insertions(+) diff --git a/frame/support/src/storage/bounded_btree_map.rs b/frame/support/src/storage/bounded_btree_map.rs index 390330457b9b9..fd086f1fb23a1 100644 --- a/frame/support/src/storage/bounded_btree_map.rs +++ b/frame/support/src/storage/bounded_btree_map.rs @@ -252,6 +252,24 @@ impl IntoIterator for BoundedBTreeMap { } } +impl<'a, K, V, S> IntoIterator for &'a BoundedBTreeMap { + type Item = (&'a K, &'a V); + type IntoIter = sp_std::collections::btree_map::Iter<'a, K, V>; + + fn into_iter(self) -> Self::IntoIter { + self.0.iter() + } +} + +impl<'a, K, V, S> IntoIterator for &'a mut BoundedBTreeMap { + type Item = (&'a K, &'a mut V); + type IntoIter = sp_std::collections::btree_map::IterMut<'a, K, V>; + + fn into_iter(self) -> Self::IntoIter { + self.0.iter_mut() + } +} + impl MaxEncodedLen for BoundedBTreeMap where K: MaxEncodedLen, diff --git a/frame/support/src/storage/bounded_btree_set.rs b/frame/support/src/storage/bounded_btree_set.rs index 275bb07c6275c..77e1c6f1c9625 100644 --- a/frame/support/src/storage/bounded_btree_set.rs +++ b/frame/support/src/storage/bounded_btree_set.rs @@ -230,6 +230,15 @@ impl IntoIterator for BoundedBTreeSet { } } +impl<'a, T, S> IntoIterator for &'a BoundedBTreeSet { + type Item = &'a T; + type IntoIter = sp_std::collections::btree_set::Iter<'a, T>; + + fn into_iter(self) -> Self::IntoIter { + self.0.iter() + } +} + impl MaxEncodedLen for BoundedBTreeSet where T: MaxEncodedLen, diff --git a/frame/support/src/storage/bounded_vec.rs b/frame/support/src/storage/bounded_vec.rs index 232f9f97fc6f8..82ae36a82bf9f 100644 --- a/frame/support/src/storage/bounded_vec.rs +++ b/frame/support/src/storage/bounded_vec.rs @@ -149,6 +149,14 @@ impl<'a, T, S> From> for &'a [T] { } } +impl<'a, T, S> sp_std::iter::IntoIterator for BoundedSlice<'a, T, S> { + type Item = &'a T; + type IntoIter = sp_std::slice::Iter<'a, T>; + fn into_iter(self) -> Self::IntoIter { + self.0.iter() + } +} + impl> Decode for BoundedVec { fn decode(input: &mut I) -> Result { let inner = Vec::::decode(input)?; @@ -605,6 +613,22 @@ impl sp_std::iter::IntoIterator for BoundedVec { } } +impl<'a, T, S> sp_std::iter::IntoIterator for &'a BoundedVec { + type Item = &'a T; + type IntoIter = sp_std::slice::Iter<'a, T>; + fn into_iter(self) -> Self::IntoIter { + self.0.iter() + } +} + +impl<'a, T, S> sp_std::iter::IntoIterator for &'a mut BoundedVec { + type Item = &'a mut T; + type IntoIter = sp_std::slice::IterMut<'a, T>; + fn into_iter(self) -> Self::IntoIter { + self.0.iter_mut() + } +} + impl codec::DecodeLength for BoundedVec { fn len(self_encoded: &[u8]) -> Result { // `BoundedVec` stored just a `Vec`, thus the length is at the beginning in diff --git a/frame/support/src/storage/weak_bounded_vec.rs b/frame/support/src/storage/weak_bounded_vec.rs index 8d1d38374d8a7..bf9b9a1017221 100644 --- a/frame/support/src/storage/weak_bounded_vec.rs +++ b/frame/support/src/storage/weak_bounded_vec.rs @@ -262,6 +262,22 @@ impl sp_std::iter::IntoIterator for WeakBoundedVec { } } +impl<'a, T, S> sp_std::iter::IntoIterator for &'a WeakBoundedVec { + type Item = &'a T; + type IntoIter = sp_std::slice::Iter<'a, T>; + fn into_iter(self) -> Self::IntoIter { + self.0.iter() + } +} + +impl<'a, T, S> sp_std::iter::IntoIterator for &'a mut WeakBoundedVec { + type Item = &'a mut T; + type IntoIter = sp_std::slice::IterMut<'a, T>; + fn into_iter(self) -> Self::IntoIter { + self.0.iter_mut() + } +} + impl codec::DecodeLength for WeakBoundedVec { fn len(self_encoded: &[u8]) -> Result { // `WeakBoundedVec` stored just a `Vec`, thus the length is at the beginning in