I'm trying to create a trait that contains functions that return Future
with the goal of being able to implement different versions that use different Future
implementations such as Ready
for synchronous behaviors. What I've gotten so far is this:
pub trait Cache {
fn get(&self, key: String) -> dyn Future<Output=Option<String>>;
fn set(&self, key: String, value: String) -> dyn Future<Output=()>;
}
struct NullCache;
impl Cache for NullCache {
fn get(&self, key: String) -> Ready<Option<String>> {
ready(None)
}
fn set(&self, key: String, value: String) -> Ready<()> {
ready(())
}
}
this results in:
error[E0053]: method `get` has an incompatible type for trait
--> src/cache.rs:26:35
|
26 | fn get(&self, key: String) -> Ready<Option<String>> {
| ^^^^^^^^^^^^^^^^^^^^^
| |
| expected trait object `dyn futures_util::Future`, found struct `std::future::Ready`
| help: change the output type to match the trait: `(dyn futures_util::Future<Output = std::option::Option<std::string::String>> + 'static)`
|
note: type in trait
--> src/cache.rs:19:35
|
19 | fn get(&self, key: String) -> dyn Future<Output=Option<String>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected fn pointer `fn(&NullCache, std::string::String) -> (dyn futures_util::Future<Output = std::option::Option<std::string::String>> + 'static)`
found fn pointer `fn(&NullCache, std::string::String) -> std::future::Ready<std::option::Option<std::string::String>>`
How should I define the Future
return types in the trait so that Ready<>
and other future implementations are matching output types?