[resolved] Trying to store an iterator: "cannot infer an appropriate lifetime for autoref due to conflicting requirements"


#1

Hi,

i am trying to wrap an iterator in another iterator in order to apply some filtering.
The original iterator is returned in a Box, because the function that returns it is defined in a trait and the concrete iterator struct is not known.

I am struggling to resolve the error:

https://play.rust-lang.org/?gist=a481f7f4cd38e84622b3722a624337ef&version=stable&backtrace=0


#2

Box<Trait> is actually shorthand for Box<Trait + 'static>, so the FilteredIterator type requires that the inner iterator not have any lifetime restrictions, which is demonstrably not true, since you parameterised on an arbitrary lifetime 'a.

The solution is to parameterise FilteredIterator in the same way:

struct FilteredIterator<'a> {
    // Some filter criteria.
    // [...]

    // The original iterator.
    iterator: Box<Iterator<Item=u32> + 'a>,
}

impl<'a> Iterator for FilteredIterator<'a> {

// ... the rest is the same ...

#3

That makes sense, thank you.