Skip to content

Commit

Permalink
chore: cleanup more
Browse files Browse the repository at this point in the history
  • Loading branch information
clintval committed Oct 15, 2023
1 parent cd8e21b commit f311980
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use std::iter::FusedIterator;
#[derive(Clone, Debug)]
pub struct FullyPeekableIterator<I: Iterator> {
iter: I,
queue: VecDeque<I::Item>
queue: VecDeque<I::Item>,
}

/// Create a new fully-peekable iterator from an existing iterator.
impl<I: Iterator> FullyPeekableIterator<I> {
fn new(iter: I) -> FullyPeekableIterator<I> {
FullyPeekableIterator {
iter,
queue: VecDeque::new()
queue: VecDeque::new(),
}
}
}
Expand All @@ -29,24 +29,17 @@ impl<I: Iterator> Iterator for FullyPeekableIterator<I> {
self.queue.pop_front().or_else(|| self.iter.next())
}

/// Returns the bounds on the remaining length of the iterator.
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
todo!()
}

#[inline]
fn count(self) -> usize {
todo!()
}

#[inline]
fn last(mut self) -> Option<I::Item> {
todo!()
}

#[inline]
fn nth(&mut self, n: usize) -> Option<I::Item> {
todo!()
let peek_len = self.queue.len();
let (lo, hi) = self.iter.size_hint();
let lo = lo.saturating_add(peek_len);
let hi = match hi {
Some(x) => x.checked_add(peek_len),
None => None,
};
(lo, hi)
}
}

Expand All @@ -57,7 +50,6 @@ impl<I: ExactSizeIterator> ExactSizeIterator for FullyPeekableIterator<I> {}
impl<I: FusedIterator> FusedIterator for FullyPeekableIterator<I> {}

impl<I: Iterator> FullyPeekableIterator<I> {

/// Test if the iterator has another element to yield. May advance the underlying iterator.
#[inline]
pub fn has_next(&mut self) -> bool {
Expand All @@ -70,9 +62,9 @@ impl<I: Iterator> FullyPeekableIterator<I> {
while self.queue.len() <= index + 1 {
match self.iter.next() {
Some(item) => self.queue.push_back(item),
None => break
None => break,
}
};
}
self.queue.get(index)
}

Expand All @@ -82,9 +74,9 @@ impl<I: Iterator> FullyPeekableIterator<I> {
while self.queue.len() <= index + 1 {
match self.iter.next() {
Some(item) => self.queue.push_back(item),
None => break
None => break,
}
};
}
self.queue.get_mut(index)
}

Expand All @@ -109,16 +101,16 @@ impl<I: Iterator> FullyPeekableIterator<I> {
self.queue.push_front(other);
None
}
other => None
None => None,
}
}

/// Consume and return the next item if it is equal to `expected`.
#[inline]
pub fn next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item>
where
T: ?Sized,
I::Item: PartialEq<T>,
where
T: ?Sized,
I::Item: PartialEq<T>,
{
self.next_if(|next| next == expected)
}
Expand Down Expand Up @@ -170,6 +162,19 @@ mod tests {
assert_eq!(peekable.has_next(), false);
}

#[test]
fn it_can_estimate_its_size_using_size_hint() {
let iter = vec![1, 2].into_iter();
let mut peekable = FullyPeekableIterator::new(iter);
assert_eq!(peekable.size_hint(), (2, Some(2)));
assert_eq!(peekable.next(), Some(1));
assert_eq!(peekable.size_hint(), (1, Some(1)));
assert_eq!(peekable.next(), Some(2));
assert_eq!(peekable.size_hint(), (0, Some(0)));
assert_eq!(peekable.next(), None);
assert_eq!(peekable.size_hint(), (0, Some(0)));
}

#[test]
fn it_can_lift_elements_without_advancing() {
let iter = vec![1, 2].into_iter();
Expand Down

0 comments on commit f311980

Please sign in to comment.