diff --git a/src/core/utils/future/mod.rs b/src/core/utils/future/mod.rs index 6d45b656..3d8ec8f4 100644 --- a/src/core/utils/future/mod.rs +++ b/src/core/utils/future/mod.rs @@ -1,3 +1,5 @@ +mod option_ext; mod try_ext_ext; +pub use option_ext::OptionExt; pub use try_ext_ext::TryExtExt; diff --git a/src/core/utils/future/option_ext.rs b/src/core/utils/future/option_ext.rs new file mode 100644 index 00000000..ed61de56 --- /dev/null +++ b/src/core/utils/future/option_ext.rs @@ -0,0 +1,22 @@ +#![allow(clippy::wrong_self_convention)] + +use futures::{future::OptionFuture, Future, FutureExt}; + +pub trait OptionExt { + fn is_none_or(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future + Send; + + fn is_some_and(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future + Send; +} + +impl OptionExt for OptionFuture +where + Fut: Future + Send, +{ + fn is_none_or(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future + Send { + self.map(|o| o.as_ref().is_none_or(f)) + } + + fn is_some_and(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future + Send { + self.map(|o| o.as_ref().is_some_and(f)) + } +} diff --git a/src/core/utils/future/try_ext_ext.rs b/src/core/utils/future/try_ext_ext.rs index 81c7aac0..977f74d2 100644 --- a/src/core/utils/future/try_ext_ext.rs +++ b/src/core/utils/future/try_ext_ext.rs @@ -39,6 +39,11 @@ where fn unwrap_or(self, default: Self::Ok) -> UnwrapOrElse Self::Ok> where Self: Sized; + + fn unwrap_or_default(self) -> UnwrapOrElse Self::Ok> + where + Self: Sized, + Self::Ok: Default; } impl TryExtExt for Fut @@ -89,4 +94,13 @@ where { self.unwrap_or_else(move |_| default) } + + #[inline] + fn unwrap_or_default(self) -> UnwrapOrElse Self::Ok> + where + Self: Sized, + Self::Ok: Default, + { + self.unwrap_or(Default::default()) + } }