pub trait AsyncOptionTools<T> {
// Required methods
fn is_some_and_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
where F: FnOnce(T) -> Fut,
Fut: Future<Output = bool>;
fn is_none_or_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
where F: FnOnce(T) -> Fut,
Fut: Future<Output = bool>;
fn map_async<B, F, Fut>(self, f: F) -> impl Future<Output = Option<B>>
where F: FnOnce(T) -> Fut,
Fut: Future<Output = B>;
}Expand description
Asynchronous extension methods for Option<T>.
This trait allows using asynchronous functions with Option types,
Required Methods§
Sourcefn is_some_and_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
fn is_some_and_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
Asynchronously checks if the option is Some and satisfies a predicate.
Returns false if the value is None. If Some, the provided async function is run.
§Examples
use async_std::task;
use async_iter_ext::AsyncOptionTools;
task::block_on(async {
async fn is_positive(x: i32) -> bool {
x > 0
}
let some = Some(10);
let none: Option<i32> = None;
assert_eq!(some.is_some_and_async(is_positive).await, true);
assert_eq!(none.is_some_and_async(is_positive).await, false);
});Sourcefn is_none_or_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
fn is_none_or_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
Asynchronously checks if the option is None or satisfies a predicate.
- Returns
trueif the value isNone. - If
Some, runs the async predicate and returns its result.
§Examples
use async_std::task;
use async_iter_ext::AsyncOptionTools;
task::block_on(async {
async fn is_zero(x: u8) -> bool {
x == 0
}
let some = Some(0);
let none: Option<u8> = None;
assert_eq!(some.is_none_or_async(is_zero).await, true);
assert_eq!(
some.is_none_or_async(|x| async move { x > 1 }).await,
false
);
assert_eq!(none.is_none_or_async(is_zero).await, true);
});Sourcefn map_async<B, F, Fut>(self, f: F) -> impl Future<Output = Option<B>>
fn map_async<B, F, Fut>(self, f: F) -> impl Future<Output = Option<B>>
Asynchronously maps an Option<T> to an Option<B> using an async function.
- If
Some, the function is awaited and wrapped inSome. - If
None, returnsNone.
§Examples
use async_std::task;
use async_iter_ext::AsyncOptionTools;
task::block_on(async {
async fn to_string_async(n: i32) -> String {
format!("Number: {}", n)
}
let some = Some(42);
let result = some.map_async(to_string_async).await;
assert_eq!(result, Some("Number: 42".to_string()));
let none: Option<i32> = None;
let result = none.map_async(to_string_async).await;
assert_eq!(result, None);
});Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".