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

Merge MultiPeek and PeekNth #933

Open
Philippe-Cholet opened this issue May 9, 2024 · 1 comment
Open

Merge MultiPeek and PeekNth #933

Philippe-Cholet opened this issue May 9, 2024 · 1 comment

Comments

@Philippe-Cholet
Copy link
Member

Those two adaptors are quite similar, except one handles an additional index.
While specializing nth/last/count for them, I nearly wrote the same code.
Before going further, I think they should be aliases of a more general struct.

Merge those would reduce duplication and additionally provide more methods for MultiPeek for "free".

I note that most of methods' documentation would be merged. We would need to be "careful" on this.

Draft

#[derive(Debug, Clone)]
/*kept private*/ struct MultiPeekGeneral<I: Iterator, Idx> {
    iter: Fuse<I>,
    buf: VecDeque<I::Item>,
    index: Idx,
}
pub type MultiPeek<I> = MultiPeekGeneral<I, usize>;
pub type PeekNth<I> = MultiPeekGeneral<I, ()>;

/*kept private*/ trait PeekIndex { ... } // fn reset_index probably. Maybe more.
impl PeekIndex for () { ... }
impl PeekIndex for usize { ... }

impl<I: Iterator, Idx: PeekIndex> MultiPeekGeneral<I, Idx> {
    // public functions: peek peek_mut peek_nth peek_nth_mut next_if next_if_eq nth_if nth_if_eq
}
impl<I: Iterator> MultiPeek<I> { // not for PeekNth
    // public functions: reset_peek
}

impl<I: Iterator, Idx> Iterator for MultiPeekGeneral<I, Idx> { ... }
impl<I: ExactSizeIterator, Idx> ExactSizeIterator for MultiPeekGeneral<I, Idx> {}

impl<I: Iterator, Idx> PeekingNext for MultiPeekGeneral<I, Idx> { ... }
@Owen-CH-Leung
Copy link
Contributor

I'll take a look and create a PR soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants