Wrapping iterators that consume variable number of elements

I have a need for an iterator that wraps another iterator and in response to each next() call on the outer iterator calls next() on the inner iterator a variable number of times. My question is open ended: are there any idioms or popular designs around how to enable this? Or a reason there isn't?

The reason I ask is that typically the closures you pass into methods that construct Iterators e.g. .map, .filter, .take_while, etc. pass the closure the elements one at a time. In this instance I want to invert the control -- I want the closure to drive how many elements of the inner iterator are consumed for each next() call on the outer iterator. In fact what I've been considering is .my_outer_iterator(|inner_iterator| ...). But I haven't seen this pattern before so I'm not sure if there's a reason it isn't more widely used?

Something like Itertools::batching?

2 Likes

Yes that pattern exactly :grinning: I guess if it's in a popular crate like itertools it's probably considered not terrible style?