If I've understood correctly, you want to implement new methods on anything that implements Iterator. The way to do that is via the extension trait pattern:
If you want to build that logic as a chain of the standard adapters, you can replace MyIteratorWrapper<T> with whatever complicated type the chain returns. Unfortunately, closure types can't be named like this, so you'll need to use regular functions instead.
If you're on the nightly compiler, the type_alias_impl_trait feature makes this a bit cleaner:
#![feature(type_alias_impl_trait)]
type OutType = ...;
type InType = ...;
trait IteratorExt : Iterator {
type FooIter: Iterator<Item = OutType>;
fn foo(self)->Self::FooIter;
}
impl<T : Iterator<Item = InType>> IteratorExt for T {
type FooIter = impl Iterator<Item = OutType>;
fn foo(self)->Self::FooIter {
self.filter_map(|x| ...)
}
}
For reusing other iterator chains with closures you can also use Box<dyn Iterator> on stable, or use a free-standing function (not trait method) that returns impl Iterator.
This is basically good and seems to be the standard approach of std::iter
Also, it has ability to be flexible and composable
I can impl a lot of these things like LoggerToNet, LoggerToSysLog, DummyRunner, RemoveErr
and combine them
So long as I can add my OWN logics into next method of impl Iterator in new struct
But one concern I have is:
In next method:
I can only write procedural code and lose ability to call declarative adapters on inner_iter, even though they are all standard iterators
Would'n this bring some troubles and make things more complex?