I have a function that returns an iterator trait object, i.e. something like
fn foo<'a>(...) -> Result<Box<Iterator<Item = SomeType> + 'a + Send>>
I'm trying to use the it to return a stream in a gRPC service. I'm using grpc-rust. To do this, I have to construct a StreamingResponse.
However, the struct and methods are written using 'static lifetimes for the iterator. For example, the iter method has the following signature:
pub fn iter<I>(iter: I) -> StreamingResponse<T>
where I : Iterator<Item=T> + Send + 'static
Is there a way to reconcile the lifetimes so that I can can pass the result of foo() to StreamingResponse::iter? Do I have to change foo to return a concrete iterator, instead of a trait object?
It should work as is. The signature of foo says the caller gets to decide what 'a is, so the caller can pick 'a = 'static and get back a Box<Iterator<Item = SomeType> + 'static + Send>. Playground
So it seems my problem is actually that the iterator comes from a vector stored in a BTreeMap in my struct, and this is causing trouble with the lifetimes of the references to self... trying to fix that...
This actually works... the problem I had was that I had used &'a self in the foo signature to try to ensure the returned iterator items would survive long enough... but I think that didn't work because I can't return references to something protected by a mutex, so I added the clone() call but the 'a remained. Simply removing it fixed my issue.