-
Notifications
You must be signed in to change notification settings - Fork 308
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
Merge multipeek peeknth #940
base: master
Are you sure you want to change the base?
Merge multipeek peeknth #940
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #940 +/- ##
==========================================
- Coverage 94.38% 94.32% -0.07%
==========================================
Files 48 48
Lines 6665 7013 +348
==========================================
+ Hits 6291 6615 +324
- Misses 374 398 +24 ☔ View full report in Codecov by Sentry. |
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.
I reviewed but I'm not sure we should continue this, as merge implementations imply to merge method documentations, while the index
behavior is different.
@phimuemue What do you think? Was I overzealous here?
src/lib.rs
Outdated
#[cfg(feature = "use_alloc")] | ||
pub use crate::multipeek_impl::MultiPeek; | ||
pub use crate::pad_tail::PadUsing; | ||
pub use crate::multipeek_general::MultiPeek; | ||
#[cfg(feature = "use_alloc")] | ||
pub use crate::peek_nth::PeekNth; | ||
pub use crate::multipeek_general::PeekNth; |
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.
You can merge those imports.
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.
Yup - merged.
src/multipeek_general.rs
Outdated
impl<I: Iterator, Idx: PeekIndex> PeekingNext for MultiPeekGeneral<I, Idx> | ||
where | ||
I: Iterator, |
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: Iterator
bound twice, move Idx
bound below.
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.
Thanks. Moved Idx
bound below
src/multipeek_general.rs
Outdated
where | ||
I: Iterator, | ||
{ | ||
fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item> | ||
where | ||
F: FnOnce(&Self::Item) -> bool, | ||
{ | ||
self.peek().filter(|item| accept(item))?; | ||
self.peek_mut().filter(|item| accept(item))?; |
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.
I don't see why it would now need peek_mut
.
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.
Thanks. Reverted to peek
src/multipeek_general.rs
Outdated
pub fn peek(&mut self) -> Option<&I::Item> { | ||
let ret = if self.index < self.buf.len() { | ||
Some(&self.buf[self.index]) | ||
} else { | ||
match self.iter.next() { | ||
Some(x) => { | ||
self.buf.push_back(x); | ||
Some(&self.buf[self.index]) | ||
} | ||
None => return None, | ||
} | ||
}; | ||
|
||
self.index += 1; | ||
ret | ||
} | ||
} | ||
|
||
impl<I: Iterator> PeekNth<I> { | ||
/// Works exactly like the `peek` method in [`std::iter::Peekable`]. | ||
pub fn peek(&mut self) -> Option<&I::Item> { | ||
self.peek_nth(0) | ||
} |
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.
I think it could be merged into MultiPeekGeneral::peek
? With a PeekIndex::increment_index
.
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.
Yes - I've merged peek
into MultiPeekGeneral
and removed the impl block
src/multipeek_general.rs
Outdated
/// [`IntoIterator`] enabled version of [`crate::Itertools::multipeek`]. | ||
pub fn multipeek<I>(iterable: I) -> MultiPeek<I::IntoIter> |
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.
To avoid crate::
to be user-visible in doc, we usually do:
[`Itertools::multipeek`](crate::Itertools::multipeek)
which I usually prefer- (OR
#[cfg(doc)] use crate::Itertools;
at the top of the file to be able to only write[`Itertools::multipeek`]
in the doc).
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.
Thanks. I've revised to use
[`Itertools::multipeek`](crate::Itertools::multipeek)
src/multipeek_general.rs
Outdated
#![allow(private_interfaces)] | ||
#![allow(private_bounds)] |
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.
I discover those lints, or rather I probably usually fix what those lints tell me rather than allow them.
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.
You are right. Turns out it passes the clippy even the lints are removed. I've removed them
src/free.rs
Outdated
#[cfg(feature = "use_alloc")] | ||
pub use crate::multipeek_impl::multipeek; | ||
pub use crate::multipeek_general::multipeek; | ||
#[cfg(feature = "use_alloc")] | ||
pub use crate::peek_nth::peek_nth; | ||
pub use crate::multipeek_general::peek_nth; |
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.
You can merge those lines.
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.
Merged
src/multipeek_general.rs
Outdated
pub iter: Fuse<I>, | ||
pub buf: VecDeque<I::Item>, | ||
pub index: Idx, |
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.
Almost missed this. The fields should remain private! If there is a need to access them from another module, you can pub(crate)
the fields.
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.
Thanks - I've changed them to private fields
src/multipeek_general.rs
Outdated
impl PeekIndex for () { | ||
fn reset_index(&mut self) {} | ||
} |
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.
I mention elsewhere the need to add a fn increment_index(&mut self);
but we probably need a more general fn add(&mut self, n: usize) { *self += n; }
instead to update the index with the nth
methods (and create tests about it).
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.
Sure. I've revised increment_index
to a generic add
instead.
@@ -137,15 +165,64 @@ where | |||
{ | |||
self.next_if(|next| next == expected) | |||
} | |||
|
|||
/// Works exactly like `next_if`, but for the `nth` value without advancing the iterator. | |||
pub fn nth_if(&mut self, n: usize, func: impl FnOnce(&I::Item) -> bool) -> Option<&I::Item> { |
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.
What should we expect from MultiPeek::nth_if
concerning the index? To advance only if func
returned true
I guess. But I would expect the doc of the method to decribe this but we can't make different docs for MultiPeek
/PeekNth
without making different methods.
⚠ Maybe I was too optimistic about this merge. ⚠
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.
I believe with the merge we'd need to completely revise the doc to describe the API behaviour under MultiPeek
and PeekNth
. I can help to re-write them if we decide to go for the direction to merge both.
My suggestion: Deprecate Rationale: And imho we should even un-fuse it to really be in line with |
According to https://github.com/search?q=itertools+multipeek+reset_peek+lang%3ARust&type=code there is some real use of Maybe the status quo is not so bad. I'm not opposed to unfuse it but it's a breaking change, I'd prefer to not break things for a while. |
Yes, we should not deprecate at a whim. But honestly I really like to know if deprecating wasn't for the better in the long run. My rationale (and the linked examples appear to confirm this to some extent):
On top of that, I do not see a problem in keeping a deprecated |
As per #933 , this PR merges
MultiPeek
andPeekNth
into a general private interfaceMultiPeekGeneral
, with public type alias to define the compatibleMultiPeek
andPeekNth
struct.