Skip to content

Commit

Permalink
Implement more IntoIter traits for bounded types (paritytech#11616)
Browse files Browse the repository at this point in the history
  • Loading branch information
KiChjang authored and godcodehunter committed Jun 22, 2022
1 parent ec758f1 commit d2e763a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
18 changes: 18 additions & 0 deletions frame/support/src/storage/bounded_btree_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,24 @@ impl<K, V, S> IntoIterator for BoundedBTreeMap<K, V, S> {
}
}

impl<'a, K, V, S> IntoIterator for &'a BoundedBTreeMap<K, V, S> {
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<K, V, S> {
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<K, V, S> MaxEncodedLen for BoundedBTreeMap<K, V, S>
where
K: MaxEncodedLen,
Expand Down
9 changes: 9 additions & 0 deletions frame/support/src/storage/bounded_btree_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ impl<T, S> IntoIterator for BoundedBTreeSet<T, S> {
}
}

impl<'a, T, S> IntoIterator for &'a BoundedBTreeSet<T, S> {
type Item = &'a T;
type IntoIter = sp_std::collections::btree_set::Iter<'a, T>;

fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}

impl<T, S> MaxEncodedLen for BoundedBTreeSet<T, S>
where
T: MaxEncodedLen,
Expand Down
24 changes: 24 additions & 0 deletions frame/support/src/storage/bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ impl<'a, T, S> From<BoundedSlice<'a, T, S>> 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<T: Decode, S: Get<u32>> Decode for BoundedVec<T, S> {
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
let inner = Vec::<T>::decode(input)?;
Expand Down Expand Up @@ -605,6 +613,22 @@ impl<T, S> sp_std::iter::IntoIterator for BoundedVec<T, S> {
}
}

impl<'a, T, S> sp_std::iter::IntoIterator for &'a BoundedVec<T, S> {
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<T, S> {
type Item = &'a mut T;
type IntoIter = sp_std::slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter_mut()
}
}

impl<T, S> codec::DecodeLength for BoundedVec<T, S> {
fn len(self_encoded: &[u8]) -> Result<usize, codec::Error> {
// `BoundedVec<T, _>` stored just a `Vec<T>`, thus the length is at the beginning in
Expand Down
16 changes: 16 additions & 0 deletions frame/support/src/storage/weak_bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,22 @@ impl<T, S> sp_std::iter::IntoIterator for WeakBoundedVec<T, S> {
}
}

impl<'a, T, S> sp_std::iter::IntoIterator for &'a WeakBoundedVec<T, S> {
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<T, S> {
type Item = &'a mut T;
type IntoIter = sp_std::slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter_mut()
}
}

impl<T, S> codec::DecodeLength for WeakBoundedVec<T, S> {
fn len(self_encoded: &[u8]) -> Result<usize, codec::Error> {
// `WeakBoundedVec<T, _>` stored just a `Vec<T>`, thus the length is at the beginning in
Expand Down

0 comments on commit d2e763a

Please sign in to comment.