From 23cf2b2236405c64f990671e8610fa1a4471acd5 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 6 Dec 2024 12:42:14 +0000 Subject: [PATCH] add is_err() to TryFuture extension add fold_default to Future tools extension Signed-off-by: Jason Volk --- src/core/utils/future/try_ext_ext.rs | 21 +++++++++++++++------ src/core/utils/stream/tools.rs | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/core/utils/future/try_ext_ext.rs b/src/core/utils/future/try_ext_ext.rs index f97ae885..81c7aac0 100644 --- a/src/core/utils/future/try_ext_ext.rs +++ b/src/core/utils/future/try_ext_ext.rs @@ -1,5 +1,8 @@ //! Extended external extensions to futures::TryFutureExt #![allow(clippy::type_complexity)] +// is_ok() has to consume *self rather than borrow. This extension is for a +// caller only ever caring about result status while discarding all contents. +#![allow(clippy::wrong_self_convention)] use futures::{ future::{MapOkOrElse, UnwrapOrElse}, @@ -11,12 +14,10 @@ pub trait TryExtExt where Self: TryFuture + 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. + fn is_err(self) -> MapOkOrElse bool, impl FnOnce(Self::Error) -> bool> + where + Self: Sized; + #[allow(clippy::wrong_self_convention)] fn is_ok(self) -> MapOkOrElse bool, impl FnOnce(Self::Error) -> bool> where @@ -44,6 +45,14 @@ impl TryExtExt for Fut where Fut: TryFuture + Send, { + #[inline] + fn is_err(self) -> MapOkOrElse bool, impl FnOnce(Self::Error) -> bool> + where + Self: Sized, + { + self.map_ok_or(true, |_| false) + } + #[inline] fn is_ok(self) -> MapOkOrElse bool, impl FnOnce(Self::Error) -> bool> where diff --git a/src/core/utils/stream/tools.rs b/src/core/utils/stream/tools.rs index cc6b7ca9..b5b036cc 100644 --- a/src/core/utils/stream/tools.rs +++ b/src/core/utils/stream/tools.rs @@ -32,6 +32,12 @@ where fn counts_with_cap(self) -> impl Future> + Send where ::Item: Eq + Hash; + + fn fold_default(self, f: F) -> impl Future + Send + where + F: Fn(T, Item) -> Fut + Send, + Fut: Future + Send, + T: Default + Send; } impl Tools for S @@ -77,4 +83,14 @@ where counts }) } + + #[inline] + fn fold_default(self, f: F) -> impl Future + Send + where + F: Fn(T, Item) -> Fut + Send, + Fut: Future + Send, + T: Default + Send, + { + self.fold(T::default(), f) + } }