I want to reuse a container implementation but tweak the interface, like BTreeSet is an adaptation of BTreeMap. Take this silly vector pretending to be storing pairs (playground).
I left out most methods because they're "easy", except drain_filter, the only method that returns a closure stored in a struct. Since there's no way to write the type of any closure, and you need to specify the return value type in the adapted interface, I guess there's no way around boxing the closure, even though it has only one implementation. Or is there?
Another way is fn drain_filter<'a, F>(...) -> impl Iterator<Item = T> + 'a. This puts "can't name the type" problem onto the user's shoulders. If they need to name it, they'd have to box it, but for something like drain_filter you usually don't have to name it. Well, unless you are writing a wrapper over a DrainFilter
Turns out that a better way would be for the bulk of the std::vec::DrainFilter data and methods to in a separate struct, e.g. DrainFilterImpl, without containing the predicate, but passing the predicate as argument to DrainFilterImpl::next(). std::vec::DrainFilter or whatever similar actual iterator must be written out reusing that struct and simply delegating to the methods.
Similar code for BTreeMap and BTreeSet sharing a DrainFilter implementation is currently a pull request