add is_err() to TryFuture extension
add fold_default to Future tools extension Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
de3b137df8
commit
23cf2b2236
2 changed files with 31 additions and 6 deletions
|
@ -1,5 +1,8 @@
|
||||||
//! Extended external extensions to futures::TryFutureExt
|
//! Extended external extensions to futures::TryFutureExt
|
||||||
#![allow(clippy::type_complexity)]
|
#![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::{
|
use futures::{
|
||||||
future::{MapOkOrElse, UnwrapOrElse},
|
future::{MapOkOrElse, UnwrapOrElse},
|
||||||
|
@ -11,12 +14,10 @@ pub trait TryExtExt<T, E>
|
||||||
where
|
where
|
||||||
Self: TryFuture<Ok = T, Error = E> + Send,
|
Self: TryFuture<Ok = T, Error = E> + Send,
|
||||||
{
|
{
|
||||||
/// Resolves to a bool for whether the TryFuture (Future of a Result)
|
fn is_err(self) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> bool, impl FnOnce(Self::Error) -> bool>
|
||||||
/// resolved to Ok or Err.
|
where
|
||||||
///
|
Self: Sized;
|
||||||
/// 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)]
|
#[allow(clippy::wrong_self_convention)]
|
||||||
fn is_ok(self) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> bool, impl FnOnce(Self::Error) -> bool>
|
fn is_ok(self) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> bool, impl FnOnce(Self::Error) -> bool>
|
||||||
where
|
where
|
||||||
|
@ -44,6 +45,14 @@ impl<T, E, Fut> TryExtExt<T, E> for Fut
|
||||||
where
|
where
|
||||||
Fut: TryFuture<Ok = T, Error = E> + Send,
|
Fut: TryFuture<Ok = T, Error = E> + Send,
|
||||||
{
|
{
|
||||||
|
#[inline]
|
||||||
|
fn is_err(self) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> bool, impl FnOnce(Self::Error) -> bool>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
self.map_ok_or(true, |_| false)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_ok(self) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> bool, impl FnOnce(Self::Error) -> bool>
|
fn is_ok(self) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> bool, impl FnOnce(Self::Error) -> bool>
|
||||||
where
|
where
|
||||||
|
|
|
@ -32,6 +32,12 @@ where
|
||||||
fn counts_with_cap<const CAP: usize>(self) -> impl Future<Output = HashMap<Item, usize>> + Send
|
fn counts_with_cap<const CAP: usize>(self) -> impl Future<Output = HashMap<Item, usize>> + Send
|
||||||
where
|
where
|
||||||
<Self as Stream>::Item: Eq + Hash;
|
<Self as Stream>::Item: Eq + Hash;
|
||||||
|
|
||||||
|
fn fold_default<T, F, Fut>(self, f: F) -> impl Future<Output = T> + Send
|
||||||
|
where
|
||||||
|
F: Fn(T, Item) -> Fut + Send,
|
||||||
|
Fut: Future<Output = T> + Send,
|
||||||
|
T: Default + Send;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Item, S> Tools<Item> for S
|
impl<Item, S> Tools<Item> for S
|
||||||
|
@ -77,4 +83,14 @@ where
|
||||||
counts
|
counts
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn fold_default<T, F, Fut>(self, f: F) -> impl Future<Output = T> + Send
|
||||||
|
where
|
||||||
|
F: Fn(T, Item) -> Fut + Send,
|
||||||
|
Fut: Future<Output = T> + Send,
|
||||||
|
T: Default + Send,
|
||||||
|
{
|
||||||
|
self.fold(T::default(), f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue