Skip to content
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

Add const generics for customized arrayvec size #63

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 0 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,3 @@ default = []
version = "1.5.1"
optional = true
default-features = false

[dev-dependencies]
criterion = "0.3"

[[bench]]
name = "peek_bench"
harness = false
37 changes: 0 additions & 37 deletions benches/peek_bench.rs

This file was deleted.

41 changes: 18 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,32 +120,27 @@ use smallvec::SmallVec;
///
/// [`next`]: https://doc.rust-lang.org/core/iter/trait.Iterator.html#tymethod.next
/// [`Iterator`]: https://doc.rust-lang.org/core/iter/trait.Iterator.html
pub trait PeekMore: Iterator + Sized {
pub trait PeekMore<const S: usize = 16>: Iterator + Sized {
/// Create a multi-peek iterator where we can peek forward multiple times from an existing iterator.
fn peekmore(self) -> PeekMoreIterator<Self>;
fn peekmore(self) -> PeekMoreIterator<Self, S>;
}

impl<I: Iterator> PeekMore for I {
fn peekmore(self) -> PeekMoreIterator<I> {
impl<I: Iterator, const S: usize> PeekMore<S> for I {
fn peekmore(self) -> PeekMoreIterator<I, S> {
PeekMoreIterator {
iterator: self,

#[cfg(not(feature = "smallvec"))]
queue: Vec::new(),

#[cfg(feature = "smallvec")]
queue: SmallVec::new(),
queue: SmallVec::<[Self; S]>::new(),

cursor: 0usize,
}
}
}

/// Default stack size for SmallVec.
/// Admittedly the current size is chosen quite arbitrarily.
#[cfg(feature = "smallvec")]
const DEFAULT_STACK_SIZE: usize = 8;

/// This iterator makes it possible to peek multiple times without consuming a value.
/// In reality the underlying iterator will be consumed, but the values will be stored in a queue.
/// This queue allows us to peek at unconsumed elements (as far as the multi-peek iterator is concerned).
Expand All @@ -154,7 +149,7 @@ const DEFAULT_STACK_SIZE: usize = 8;
///
/// [consumes]: https://doc.rust-lang.org/core/iter/trait.Iterator.html#tymethod.next
#[derive(Clone, Debug)]
pub struct PeekMoreIterator<I: Iterator> {
pub struct PeekMoreIterator<I: Iterator, const S: usize> {
/// The underlying iterator. Consumption of this inner iterator does not represent consumption of the
/// `PeekMoreIterator`.
iterator: I,
Expand All @@ -165,7 +160,7 @@ pub struct PeekMoreIterator<I: Iterator> {
#[cfg(not(feature = "smallvec"))]
queue: Vec<Option<I::Item>>,
#[cfg(feature = "smallvec")]
queue: SmallVec<[Option<I::Item>; DEFAULT_STACK_SIZE]>,
queue: SmallVec<[Option<I::Item>; S]>,

/// The cursor points to the element we are currently peeking at.
///
Expand All @@ -177,7 +172,7 @@ pub struct PeekMoreIterator<I: Iterator> {
cursor: usize,
}

impl<I: Iterator> PeekMoreIterator<I> {
impl<I: Iterator, const S: usize> PeekMoreIterator<I, S> {
/// Get a reference to the element where the cursor currently points to. If no such element exists,
/// return `None` will be returned.
///
Expand Down Expand Up @@ -456,7 +451,7 @@ impl<I: Iterator> PeekMoreIterator<I> {
///
/// [`next()`]: struct.PeekMoreIterator.html#impl-Iterator
#[inline]
pub fn advance_cursor(&mut self) -> &mut PeekMoreIterator<I> {
pub fn advance_cursor(&mut self) -> &mut PeekMoreIterator<I, S> {
self.increment_cursor();
self
}
Expand All @@ -467,7 +462,7 @@ impl<I: Iterator> PeekMoreIterator<I> {
///
/// [`next()`]: struct.PeekMoreIterator.html#impl-Iterator
#[inline]
pub fn advance_cursor_by(&mut self, n: usize) -> &mut PeekMoreIterator<I> {
pub fn advance_cursor_by(&mut self, n: usize) -> &mut PeekMoreIterator<I, S> {
self.cursor += n;
self
}
Expand All @@ -477,7 +472,7 @@ impl<I: Iterator> PeekMoreIterator<I> {
pub fn advance_cursor_while<P: Fn(Option<&I::Item>) -> bool>(
&mut self,
predicate: P,
) -> &mut PeekMoreIterator<I> {
) -> &mut PeekMoreIterator<I, S> {
let view = self.peek();

if predicate(view) {
Expand All @@ -498,7 +493,7 @@ impl<I: Iterator> PeekMoreIterator<I> {
///
/// [`PeekMoreError::ElementHasBeenConsumed`]: enum.PeekMoreError.html#variant.ElementHasBeenConsumed
#[inline]
pub fn move_cursor_back(&mut self) -> Result<&mut PeekMoreIterator<I>, PeekMoreError> {
pub fn move_cursor_back(&mut self) -> Result<&mut PeekMoreIterator<I, S>, PeekMoreError> {
if self.cursor >= 1 {
self.decrement_cursor();
Ok(self)
Expand All @@ -520,7 +515,7 @@ impl<I: Iterator> PeekMoreIterator<I> {
pub fn move_cursor_back_by(
&mut self,
n: usize,
) -> Result<&mut PeekMoreIterator<I>, PeekMoreError> {
) -> Result<&mut PeekMoreIterator<I, S>, PeekMoreError> {
if self.cursor < n {
Err(PeekMoreError::ElementHasBeenConsumed)
} else {
Expand All @@ -533,7 +528,7 @@ impl<I: Iterator> PeekMoreIterator<I> {
/// The latter happens when the cursor position is smaller than the elements it has to move
/// backwards by.
#[inline]
pub fn move_cursor_back_or_reset(&mut self, n: usize) -> &mut PeekMoreIterator<I> {
pub fn move_cursor_back_or_reset(&mut self, n: usize) -> &mut PeekMoreIterator<I, S> {
if self.cursor < n {
self.reset_cursor();
} else {
Expand All @@ -545,7 +540,7 @@ impl<I: Iterator> PeekMoreIterator<I> {

/// Move the cursor to the n-th element of the queue.
#[inline]
pub fn move_nth(&mut self, n: usize) -> &mut PeekMoreIterator<I> {
pub fn move_nth(&mut self, n: usize) -> &mut PeekMoreIterator<I, S> {
self.cursor = n;
self
}
Expand Down Expand Up @@ -746,7 +741,7 @@ impl<I: Iterator> PeekMoreIterator<I> {
}
}

impl<'a, I: Iterator> Iterator for PeekMoreIterator<I> {
impl<'a, I: Iterator, const S: usize> Iterator for PeekMoreIterator<I, S> {
type Item = I::Item;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -765,12 +760,12 @@ impl<'a, I: Iterator> Iterator for PeekMoreIterator<I> {
/// Uses [`ExactSizeIterator`] default implementation.
///
/// [`ExactSizeIterator`]: https://doc.rust-lang.org/core/iter/trait.ExactSizeIterator.html
impl<I: ExactSizeIterator> ExactSizeIterator for PeekMoreIterator<I> {}
impl<I: ExactSizeIterator, const S: usize> ExactSizeIterator for PeekMoreIterator<I, S> {}

/// Uses [`FusedIterator`] default implementation.
///
/// [`FusedIterator`]: https://doc.rust-lang.org/core/iter/trait.FusedIterator.html
impl<I: FusedIterator> FusedIterator for PeekMoreIterator<I> {}
impl<I: FusedIterator, const S: usize> FusedIterator for PeekMoreIterator<I, S> {}

/// This enumeration provides errors which represent lack of success of the [`PeekMoreIterator`].
///
Expand Down