mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-20 13:24:44 +00:00
Remove code duplicating stdlib (#1239)
* Get rid of superfluous ReverseBlockRootIterator * Get rid of superfluous ReverseStateRootIterator and ReverseChainIterator * cargo fmt
This commit is contained in:
@@ -294,52 +294,6 @@ fn slot_of_prev_restore_point<E: EthSpec>(current_slot: Slot) -> Slot {
|
||||
(current_slot - 1) / slots_per_historical_root * slots_per_historical_root
|
||||
}
|
||||
|
||||
pub type ReverseBlockRootIterator<'a, E, S> =
|
||||
ReverseHashAndSlotIterator<BlockRootsIterator<'a, E, S>>;
|
||||
pub type ReverseStateRootIterator<'a, E, S> =
|
||||
ReverseHashAndSlotIterator<StateRootsIterator<'a, E, S>>;
|
||||
|
||||
pub type ReverseHashAndSlotIterator<I> = ReverseChainIterator<(Hash256, Slot), I>;
|
||||
|
||||
/// Provides a wrapper for an iterator that returns a given `T` before it starts returning results of
|
||||
/// the `Iterator`.
|
||||
pub struct ReverseChainIterator<T, I> {
|
||||
first_value_used: bool,
|
||||
first_value: T,
|
||||
iter: I,
|
||||
}
|
||||
|
||||
impl<T, I> ReverseChainIterator<T, I>
|
||||
where
|
||||
T: Sized,
|
||||
I: Iterator<Item = T> + Sized,
|
||||
{
|
||||
pub fn new(first_value: T, iter: I) -> Self {
|
||||
Self {
|
||||
first_value_used: false,
|
||||
first_value,
|
||||
iter,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, I> Iterator for ReverseChainIterator<T, I>
|
||||
where
|
||||
T: Clone,
|
||||
I: Iterator<Item = T>,
|
||||
{
|
||||
type Item = T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.first_value_used {
|
||||
self.iter.next()
|
||||
} else {
|
||||
self.first_value_used = true;
|
||||
Some(self.first_value.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user