add is_ok to futures TryExtExt utils

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-21 20:21:00 +00:00 committed by strawberry
parent 1fdcab0319
commit 93130fbb85

View file

@ -10,6 +10,17 @@ pub trait TryExtExt<T, E>
where
Self: TryFuture<Ok = T, Error = E> + Send,
{
/// Resolves to a bool for whether the TryFuture (Future of a Result)
/// resolved to Ok or Err.
///
/// is_ok() has to consume *self rather than borrow. The intent of this
/// extension is therefor for a caller only ever caring about result status
/// while discarding all contents.
#[allow(clippy::wrong_self_convention)]
fn is_ok(self) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> bool, impl FnOnce(Self::Error) -> bool>
where
Self: Sized;
fn map_ok_or<U, F>(
self, default: U, f: F,
) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> U, impl FnOnce(Self::Error) -> U>
@ -32,6 +43,14 @@ impl<T, E, Fut> TryExtExt<T, E> for Fut
where
Fut: TryFuture<Ok = T, Error = E> + Send,
{
#[inline]
fn is_ok(self) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> bool, impl FnOnce(Self::Error) -> bool>
where
Self: Sized,
{
self.map_ok_or(false, |_| true)
}
#[inline]
fn map_ok_or<U, F>(
self, default: U, f: F,