rust-itertools / itertools

Extra iterator adaptors, iterator methods, free functions, and macros.
https://docs.rs/itertools/
Apache License 2.0
2.72k stars 309 forks source link

Draining peek buf of MultiPeek #750

Closed ratmice closed 1 year ago

ratmice commented 1 year ago

I need a way to discard peeked elements of a MultiPeek, it seemed the easiest way was to expose the VeqDeque impl of Drain as some kind of drain_peeked function. Subtracting the range.len() from the current buf index. I'm not sure if it is an elegant solution (i.e. the range of the peek buf differs from the range of the self (i.e. if we implemented drain for MultiPeek). In using such a drain mechanism it might also be helpful to have a function for getting the size_hint or a Range for the peek buf rather than having to call the number of times peek has been called.

Before I went and made a pull-req I was curious if you feel this warrants inclusion in itertools MultiPeek, or if I should just add an augmented implementation of it directly to my project -- or perhaps I missed an iterator adapter in itertools that can be used this way. Thanks.

Philippe-Cholet commented 1 year ago

First, I'm no maintainer here. That seems a bit special case to me. If you drain the buffer, it means you advance the iterator. So if you had a buf_len(&self) -> usize method (which might have a broader use case), you could then do something like it.skip(it.buf_len()) that could mimic the behavior you want, and if performance is important and if you massively peek, then maybe Multipeek could have a specialized Iterator::nth method (since skip relies on it).

PS: buf_len or any other name, a specialized nth would internally drain needed elements.

ratmice commented 1 year ago

Thanks I had indeed missed that skip should work, if performance is a problem (which is unfortunately likely, the thing i'm implementing even specifically mentions its :fire:) I'm perhaps better off trying to cut out the buf: VeqDeque entirely by manually incrementing indexes. As such I'll close this.