If I already have a function which could generate an iterator and that function is a blackbox for us.
fn iter<'a>(&'a context) -> impl Iterator+'a
Now we if we have another function, in which we have a local variable context
.
fn get_iter() -> impl Iterator {
context=...;
// iter(&context)
}
Obviously we cannot return the iter(&context)
, because the iterator cannot outlive the context
, which will not outlive current function.
The only way is to return the iterator with context together.
I reckon that the returned type should be like:
pub struct Context<B, I: Iterator> {
context: B,
iter: I,
}
The Context implement Iterator
and just forward every associated functions like next()
to iter
.
In that schema, the question is transformed to how to construct Context
.
More specifically, how to implement this function:
fn iter_with_context<B, F, I>(context: B, f: F) -> Context<B, I>
where
I: Iterator,
F: FnOnce(&B) -> I,
{
unimplemented!()
}
Then several more problem emerge:
F: FnOnce(&B) -> I
in the constrains acctually doesn't make sense, because returned type I
is dependent on lifetime of &B
. How to change it?
There are self referencing in the Context
, how can we construct it?