How to design a composable iterator adapter/consumer API?

You can look at existing adapters in itertools or the standard library. Peekable is a simple stateful one. Flatten is one that alters state between driving a source iterator and then getting items from the inner iterator.

However, if it is not sum and count, but some complex iterator adaptors/consumers provided by other crates

The issue here is that summing and counting are reducers/folders, not consumers or iterators. Those crates are simply providing the wrong API for what you want to do. Composable reduction could be done when provided as a simple binary operation.

Previous discussion: Adding Iterator::extrema, argmin, argmax - #4 by dlight

4 Likes