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

add into function for PrefixIterator #10614

Merged
merged 3 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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