Skip to content

Commit

Permalink
add into function for PrefixIterator (paritytech#10614)
Browse files Browse the repository at this point in the history
* add into function for PrefixIterator

* update with comments

* update with comments
  • Loading branch information
Mr-Leshiy authored and grishasobol committed Mar 28, 2022
1 parent 0763831 commit f20299b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
13 changes: 13 additions & 0 deletions frame/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,19 @@ pub struct PrefixIterator<T, OnRemoval = ()> {
phantom: core::marker::PhantomData<OnRemoval>,
}

impl<T, OnRemoval1> PrefixIterator<T, OnRemoval1> {
/// Converts to the same iterator but with the different 'OnRemoval' type
pub fn convert_on_removal<OnRemoval2>(self) -> PrefixIterator<T, OnRemoval2> {
PrefixIterator::<T, OnRemoval2> {
prefix: self.prefix,
previous_key: self.previous_key,
drain: self.drain,
closure: self.closure,
phantom: Default::default(),
}
}
}

/// Trait for specialising on removal logic of [`PrefixIterator`].
pub trait PrefixIteratorOnRemoval {
/// This function is called whenever a key/value is removed.
Expand Down
36 changes: 4 additions & 32 deletions frame/support/src/storage/types/counted_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,7 @@ where
///
/// NOTE: If a value failed to decode because storage is corrupted then it is skipped.
pub fn iter_values() -> crate::storage::PrefixIterator<Value, OnRemovalCounterUpdate<Prefix>> {
let map_iterator = <Self as MapWrapper>::Map::iter_values();
crate::storage::PrefixIterator {
prefix: map_iterator.prefix,
previous_key: map_iterator.previous_key,
drain: map_iterator.drain,
closure: map_iterator.closure,
phantom: Default::default(),
}
<Self as MapWrapper>::Map::iter_values().convert_on_removal()
}

/// Translate the values of all elements by a function `f`, in the map in no particular order.
Expand Down Expand Up @@ -374,28 +367,14 @@ where
///
/// If you alter the map while doing this, you'll get undefined results.
pub fn iter() -> crate::storage::PrefixIterator<(Key, Value), OnRemovalCounterUpdate<Prefix>> {
let map_iterator = <Self as MapWrapper>::Map::iter();
crate::storage::PrefixIterator {
prefix: map_iterator.prefix,
previous_key: map_iterator.previous_key,
drain: map_iterator.drain,
closure: map_iterator.closure,
phantom: Default::default(),
}
<Self as MapWrapper>::Map::iter().convert_on_removal()
}

/// Remove all elements from the map and iterate through them in no particular order.
///
/// If you add elements to the map while doing this, you'll get undefined results.
pub fn drain() -> crate::storage::PrefixIterator<(Key, Value), OnRemovalCounterUpdate<Prefix>> {
let map_iterator = <Self as MapWrapper>::Map::drain();
crate::storage::PrefixIterator {
prefix: map_iterator.prefix,
previous_key: map_iterator.previous_key,
drain: map_iterator.drain,
closure: map_iterator.closure,
phantom: Default::default(),
}
<Self as MapWrapper>::Map::drain().convert_on_removal()
}

/// Translate the values of all elements by a function `f`, in the map in no particular order.
Expand All @@ -420,14 +399,7 @@ where
pub fn iter_from(
starting_raw_key: Vec<u8>,
) -> crate::storage::PrefixIterator<(Key, Value), OnRemovalCounterUpdate<Prefix>> {
let map_iterator = <Self as MapWrapper>::Map::iter_from(starting_raw_key);
crate::storage::PrefixIterator {
prefix: map_iterator.prefix,
previous_key: map_iterator.previous_key,
drain: map_iterator.drain,
closure: map_iterator.closure,
phantom: Default::default(),
}
<Self as MapWrapper>::Map::iter_from(starting_raw_key).convert_on_removal()
}
}

Expand Down

0 comments on commit f20299b

Please sign in to comment.