Add unwrap_or_else_async to OptionFutureExt.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2026-02-12 06:50:38 +00:00
parent 5dcb4c9a34
commit 02cd1dc124

View File

@@ -2,6 +2,8 @@
use futures::{Future, FutureExt, future::OptionFuture};
use super::super::BoolExt;
pub trait OptionFutureExt<T> {
fn is_none_or(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future<Output = bool> + Send;
@@ -9,11 +11,16 @@ pub trait OptionFutureExt<T> {
fn unwrap_or(self, t: T) -> impl Future<Output = T> + Send;
fn unwrap_or_else(self, f: impl FnOnce() -> T + Send) -> impl Future<Output = T> + Send;
fn unwrap_or_default(self) -> impl Future<Output = T> + Send
where
T: Default;
fn unwrap_or_else(self, f: impl FnOnce() -> T + Send) -> impl Future<Output = T> + Send;
fn unwrap_or_else_async<F: Future<Output = T> + Send>(
self,
f: impl FnOnce() -> F + Send,
) -> impl Future<Output = Option<T>> + Send;
}
impl<T, Fut> OptionFutureExt<T> for OptionFuture<Fut>
@@ -34,11 +41,6 @@ where
#[inline]
fn unwrap_or(self, t: T) -> impl Future<Output = T> + Send { self.map(|o| o.unwrap_or(t)) }
#[inline]
fn unwrap_or_else(self, f: impl FnOnce() -> T + Send) -> impl Future<Output = T> + Send {
self.map(|o| o.unwrap_or_else(f))
}
#[inline]
fn unwrap_or_default(self) -> impl Future<Output = T> + Send
where
@@ -46,4 +48,17 @@ where
{
self.map(Option::unwrap_or_default)
}
#[inline]
fn unwrap_or_else(self, f: impl FnOnce() -> T + Send) -> impl Future<Output = T> + Send {
self.map(|o| o.unwrap_or_else(f))
}
#[inline]
fn unwrap_or_else_async<F: Future<Output = T> + Send>(
self,
f: impl FnOnce() -> F + Send,
) -> impl Future<Output = Option<T>> + Send {
self.map(|o| o.is_none().then_async(f)).flatten()
}
}