add unwrap_or_default() to future TryExt extension
start an OptionFuture extension Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
d921b82376
commit
aa6d0fcaa7
3 changed files with 38 additions and 0 deletions
|
@ -1,3 +1,5 @@
|
|||
mod option_ext;
|
||||
mod try_ext_ext;
|
||||
|
||||
pub use option_ext::OptionExt;
|
||||
pub use try_ext_ext::TryExtExt;
|
||||
|
|
22
src/core/utils/future/option_ext.rs
Normal file
22
src/core/utils/future/option_ext.rs
Normal file
|
@ -0,0 +1,22 @@
|
|||
#![allow(clippy::wrong_self_convention)]
|
||||
|
||||
use futures::{future::OptionFuture, Future, FutureExt};
|
||||
|
||||
pub trait OptionExt<T> {
|
||||
fn is_none_or(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future<Output = bool> + Send;
|
||||
|
||||
fn is_some_and(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future<Output = bool> + Send;
|
||||
}
|
||||
|
||||
impl<T, Fut> OptionExt<T> for OptionFuture<Fut>
|
||||
where
|
||||
Fut: Future<Output = T> + Send,
|
||||
{
|
||||
fn is_none_or(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future<Output = bool> + Send {
|
||||
self.map(|o| o.as_ref().is_none_or(f))
|
||||
}
|
||||
|
||||
fn is_some_and(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future<Output = bool> + Send {
|
||||
self.map(|o| o.as_ref().is_some_and(f))
|
||||
}
|
||||
}
|
|
@ -39,6 +39,11 @@ where
|
|||
fn unwrap_or(self, default: Self::Ok) -> UnwrapOrElse<Self, impl FnOnce(Self::Error) -> Self::Ok>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
fn unwrap_or_default(self) -> UnwrapOrElse<Self, impl FnOnce(Self::Error) -> Self::Ok>
|
||||
where
|
||||
Self: Sized,
|
||||
Self::Ok: Default;
|
||||
}
|
||||
|
||||
impl<T, E, Fut> TryExtExt<T, E> for Fut
|
||||
|
@ -89,4 +94,13 @@ where
|
|||
{
|
||||
self.unwrap_or_else(move |_| default)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unwrap_or_default(self) -> UnwrapOrElse<Self, impl FnOnce(Self::Error) -> Self::Ok>
|
||||
where
|
||||
Self: Sized,
|
||||
Self::Ok: Default,
|
||||
{
|
||||
self.unwrap_or(Default::default())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue