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:
RwLock<HashMap<K, Entry<V, Pin<Box<dyn Future<Output = V>>>>>>
= note: expected struct `Shared<Pin<Box<(dyn futures::Future<Output = V> + 'static)>>>`
found struct `Shared<futures::future::Inspect<Fut, [closure@src/lib.rs:92:26: 97:18]>>`
code is here. Sorry for the length. most of it is type declerations.
- How did the compiler infer a
Shared<Pin<Box<_>>>
? - Should I change the type of the loader to be
F: Fn(&K) -> Fut
andFut: Box<dyn Future<Output = V>>
? Should this create other problems? - Is there a shorter or more idiomatic way to write this?