primitives/utils/futures.rs
1use std::future::Future;
2
3/// A convenience for futures that return `Result` values that includes
4/// a variety of adapters tailored to such futures. The original futures::TryFuture
5/// trait does not require the future output to be a `Result` with corresponding
6/// types, which makes it less useful in many cases.
7///
8/// Additionally, we bind this future to be `Send` and `Sync` to ensure it can be used
9/// in a multi-threaded context.
10pub trait TryFuture: Future<Output = Result<Self::Ok, Self::Error>> + Send {
11 /// The type of successful values yielded by this future
12 type Ok;
13
14 /// The type of failures yielded by this future
15 type Error;
16}
17
18impl<F, T, E> TryFuture for F
19where
20 F: ?Sized + Future<Output = Result<T, E>> + Send,
21{
22 type Ok = T;
23 type Error = E;
24}
25
26/// A helper trait to designate a future that is `Send` and `'static`, meaning it does not depend on
27/// any other lifetime parameters that might otherwise be automatically captured by the compiler.
28pub trait StaticFuture<Output>: Future<Output = Output> + Send + 'static {}
29
30impl<T: Future + Send + 'static> StaticFuture<T::Output> for T {}