async_iter_ext/
option.rs

1pub trait AsyncOptionTools<T> {
2  #[allow(clippy::wrong_self_convention)]
3  fn is_some_and_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
4  where
5    F: FnOnce(T) -> Fut,
6    Fut: Future<Output = bool>;
7
8  #[allow(clippy::wrong_self_convention)]
9  fn is_none_or_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
10  where
11    F: FnOnce(T) -> Fut,
12    Fut: Future<Output = bool>;
13
14  fn map_async<B, F, Fut>(self, f: F) -> impl Future<Output = Option<B>>
15  where
16    F: FnOnce(T) -> Fut,
17    Fut: Future<Output = B>;
18}
19
20impl<T> AsyncOptionTools<T> for Option<T> {
21  async fn is_some_and_async<F, Fut>(self, f: F) -> bool
22  where
23    F: FnOnce(T) -> Fut,
24    Fut: Future<Output = bool>,
25  {
26    if let Some(x) = self { f(x).await } else { false }
27  }
28
29  async fn is_none_or_async<F, Fut>(self, f: F) -> bool
30  where
31    F: FnOnce(T) -> Fut,
32    Fut: Future<Output = bool>,
33  {
34    if let Some(x) = self { f(x).await } else { true }
35  }
36
37  async fn map_async<B, F, Fut>(self, f: F) -> Option<B>
38  where
39    F: FnOnce(T) -> Fut,
40    Fut: Future<Output = B>,
41  {
42    if let Some(x) = self { Some(f(x).await) } else { None }
43  }
44}