-
Notifications
You must be signed in to change notification settings - Fork 25
refactor: partially refactor iter types into submodules #1531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright (C) 2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE.md for licensing terms. | ||
|
|
||
| use crate::v2::api::{KeyType, KeyValuePair}; | ||
|
|
||
| pub trait FilteredKeyRangeExt: Iterator<Item: KeyValuePair> + Sized { | ||
| /// Returns a new iterator that will emit key-value pairs up to and | ||
| /// including `last_key`. | ||
| fn stop_after_key<K: KeyType>(self, last_key: Option<K>) -> FilteredKeyRangeIter<Self, K> { | ||
| FilteredKeyRangeIter::new(self, last_key) | ||
| } | ||
| } | ||
|
|
||
| impl<I: Iterator<Item: KeyValuePair>> FilteredKeyRangeExt for I {} | ||
|
|
||
| /// An iterator over key-value pairs that stops after a specified final key. | ||
| #[derive(Debug)] | ||
| #[must_use = "iterators are lazy and do nothing unless consumed"] | ||
| pub enum FilteredKeyRangeIter<I, K> { | ||
| Unfiltered { iter: I }, | ||
| Filtered { iter: I, last_key: K }, | ||
| Exhausted, | ||
| } | ||
|
|
||
| impl<I: Iterator<Item = T>, T: KeyValuePair, K: KeyType> FilteredKeyRangeIter<I, K> { | ||
| /// Creates a new [`FilteredKeyRangeIter`] that will iterate over `iter` | ||
| /// stopping early if `last_key` is `Some` and a key greater than it is | ||
| /// encountered. | ||
| pub fn new(iter: I, last_key: Option<K>) -> Self { | ||
| match last_key { | ||
| Some(k) => FilteredKeyRangeIter::Filtered { iter, last_key: k }, | ||
| None => FilteredKeyRangeIter::Unfiltered { iter }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<I: Iterator<Item = T>, T: KeyValuePair, K: KeyType> Iterator for FilteredKeyRangeIter<I, K> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should also Probably should document that it will fuse it as well. |
||
| type Item = Result<(T::Key, T::Value), T::Error>; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| match self { | ||
| FilteredKeyRangeIter::Unfiltered { iter } => iter.next().map(T::try_into_tuple), | ||
| FilteredKeyRangeIter::Filtered { iter, last_key } => { | ||
| match iter.next().map(T::try_into_tuple) { | ||
| Some(Ok((key, value))) if key.as_ref() <= last_key.as_ref() => { | ||
| Some(Ok((key, value))) | ||
| } | ||
| Some(Err(e)) => Some(Err(e)), | ||
| _ => { | ||
| *self = FilteredKeyRangeIter::Exhausted; | ||
| None | ||
| } | ||
| } | ||
| } | ||
| FilteredKeyRangeIter::Exhausted => None, | ||
| } | ||
| } | ||
|
|
||
| fn size_hint(&self) -> (usize, Option<usize>) { | ||
| match self { | ||
| FilteredKeyRangeIter::Unfiltered { iter } => iter.size_hint(), | ||
| FilteredKeyRangeIter::Filtered { iter, .. } => { | ||
| let (_, upper) = iter.size_hint(); | ||
| (0, upper) | ||
| } | ||
| FilteredKeyRangeIter::Exhausted => (0, Some(0)), | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright (C) 2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE.md for licensing terms. | ||
|
|
||
| pub(crate) trait ReturnableIteratorExt: Iterator + Sized { | ||
| /// Wraps this iterator in a [`ReturnableIterator`]. | ||
| fn returnable(self) -> ReturnableIterator<Self> { | ||
| ReturnableIterator::new(self) | ||
| } | ||
| } | ||
|
|
||
| impl<I: Iterator> ReturnableIteratorExt for I {} | ||
|
|
||
| /// Similar to a peekable iterator. In addition to being able to peek at the | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I'm following what this does that Peekable's blanket implementation does not do., except it's a little less flexible. Peekable keeps a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is similar to using |
||
| /// next item without consuming it, it also allows "returning" an item back to | ||
| /// the iterator to be yielded on the next call to [`next()`]. | ||
| /// | ||
| /// [`next()`]: Iterator::next | ||
| pub(crate) struct ReturnableIterator<I: Iterator> { | ||
| iter: I, | ||
| next: Option<I::Item>, | ||
| } | ||
|
|
||
| impl<I: Iterator> ReturnableIterator<I> { | ||
| pub(crate) const fn new(iter: I) -> Self { | ||
| Self { iter, next: None } | ||
| } | ||
|
|
||
| /// Peeks at the next item without consuming it. The next call to | ||
| /// [`next()`] will still return this item. | ||
| /// | ||
| /// [`next()`]: Iterator::next | ||
| pub(crate) fn peek(&mut self) -> Option<&mut I::Item> { | ||
| if self.next.is_none() { | ||
| self.next = self.iter.next(); | ||
| } | ||
|
|
||
| self.next.as_mut() | ||
| } | ||
|
|
||
| /// Puts an item back to be returned on the next call to [`next()`]. This | ||
| /// makes it easy to "un-read" a single item from the iterator without | ||
| /// needing to implement complex buffering logic. | ||
| /// | ||
| /// NOTE: This will replace and return any item that was already in the | ||
| /// return slot. | ||
| /// | ||
| /// [`next()`]: Iterator::next | ||
| pub(crate) const fn return_item(&mut self, head: I::Item) -> Option<I::Item> { | ||
| self.next.replace(head) | ||
| } | ||
| } | ||
|
|
||
| impl<I: Iterator> Iterator for ReturnableIterator<I> { | ||
| type Item = I::Item; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| self.next.take().or_else(|| self.iter.next()) | ||
| } | ||
|
|
||
| fn size_hint(&self) -> (usize, Option<usize>) { | ||
| let (lower, upper) = self.iter.size_hint(); | ||
| let head_count = usize::from(self.next.is_some()); | ||
| ( | ||
| lower.saturating_add(head_count), | ||
| upper.and_then(|u| u.checked_add(head_count)), | ||
| ) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: naming
FilteredKeyRangeIter::Unfilteredseems odd. PerhapsMaybeFilteredKeyRangeIteris better?