From 02cd1dc124eab466ac280e043c613bafa04f6daf Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 12 Feb 2026 06:50:38 +0000 Subject: [PATCH] Add unwrap_or_else_async to OptionFutureExt. Signed-off-by: Jason Volk --- src/core/utils/future/option_ext.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/core/utils/future/option_ext.rs b/src/core/utils/future/option_ext.rs index 2e6da123..071215fa 100644 --- a/src/core/utils/future/option_ext.rs +++ b/src/core/utils/future/option_ext.rs @@ -2,6 +2,8 @@ use futures::{Future, FutureExt, future::OptionFuture}; +use super::super::BoolExt; + pub trait OptionFutureExt { fn is_none_or(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future + Send; @@ -9,11 +11,16 @@ pub trait OptionFutureExt { fn unwrap_or(self, t: T) -> impl Future + Send; - fn unwrap_or_else(self, f: impl FnOnce() -> T + Send) -> impl Future + Send; - fn unwrap_or_default(self) -> impl Future + Send where T: Default; + + fn unwrap_or_else(self, f: impl FnOnce() -> T + Send) -> impl Future + Send; + + fn unwrap_or_else_async + Send>( + self, + f: impl FnOnce() -> F + Send, + ) -> impl Future> + Send; } impl OptionFutureExt for OptionFuture @@ -34,11 +41,6 @@ where #[inline] fn unwrap_or(self, t: T) -> impl Future + Send { self.map(|o| o.unwrap_or(t)) } - #[inline] - fn unwrap_or_else(self, f: impl FnOnce() -> T + Send) -> impl Future + Send { - self.map(|o| o.unwrap_or_else(f)) - } - #[inline] fn unwrap_or_default(self) -> impl Future + 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 + Send { + self.map(|o| o.unwrap_or_else(f)) + } + + #[inline] + fn unwrap_or_else_async + Send>( + self, + f: impl FnOnce() -> F + Send, + ) -> impl Future> + Send { + self.map(|o| o.is_none().then_async(f)).flatten() + } }