Hey all,
I am trying to implement an async cache that is designed to store future values that multiple readers can poll. For that, the cache holds an internal map of future values. For polling by multiple users, I wanted to use Shared futures. The loader given to the cache is a function it uses to load missing/expired entries.
Sometimes the cache should chain some stuff on the futures it holds (1 example is to report statistics of the time it took to hold the value). This changes the stored type and throws a cryptic error.
By this point, I have already learned that an indirection using a trait object is required here. So the internal map is:
This is using the boxed() combinator from FutureExt. This will fail about errors talking about Send, but you can fix them by adding + Send to your boxed futures.
@alice you are a lifesaver.
The compiler just sent me to throw Send on a few places. and then, after uncommenting the code for the statistics the borrow checker now complains about the borrowing of self. By this point, throwing a Static lifetime everywhere looks wrong to me.
Updated code in the playground.
It is because the reference to self, which is stored inside shared_fut, ends up not only in the return value, but also in the map argument. My expectation is that you will have a lot of trouble getting that to work with an ordinary, and that you should figure out a way to only put owned data into the inspect future.