add try_filter_map to TryReadyExt

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-12-02 10:49:08 +00:00
parent 3fbd74310f
commit 3b30bd3580

View file

@ -3,7 +3,7 @@
use futures::{
future::{ready, Ready},
stream::{AndThen, TryFold, TryForEach, TryStream, TryStreamExt},
stream::{AndThen, TryFilterMap, TryFold, TryForEach, TryStream, TryStreamExt},
};
use crate::Result;
@ -20,11 +20,11 @@ where
where
F: Fn(S::Ok) -> Result<U, E>;
fn ready_try_for_each<F>(
fn ready_try_filter_map<F, U>(
self, f: F,
) -> TryForEach<Self, Ready<Result<(), E>>, impl FnMut(S::Ok) -> Ready<Result<(), E>>>
) -> TryFilterMap<Self, Ready<Result<Option<U>, E>>, impl FnMut(S::Ok) -> Ready<Result<Option<U>, E>>>
where
F: FnMut(S::Ok) -> Result<(), E>;
F: Fn(S::Ok) -> Result<Option<U>, E>;
fn ready_try_fold<U, F>(
self, init: U, f: F,
@ -38,6 +38,12 @@ where
where
F: Fn(U, S::Ok) -> Result<U, E>,
U: Default;
fn ready_try_for_each<F>(
self, f: F,
) -> TryForEach<Self, Ready<Result<(), E>>, impl FnMut(S::Ok) -> Ready<Result<(), E>>>
where
F: FnMut(S::Ok) -> Result<(), E>;
}
impl<T, E, S> TryReadyExt<T, E, S> for S
@ -53,14 +59,13 @@ where
self.and_then(move |t| ready(f(t)))
}
#[inline]
fn ready_try_for_each<F>(
self, mut f: F,
) -> TryForEach<Self, Ready<Result<(), E>>, impl FnMut(S::Ok) -> Ready<Result<(), E>>>
fn ready_try_filter_map<F, U>(
self, f: F,
) -> TryFilterMap<Self, Ready<Result<Option<U>, E>>, impl FnMut(S::Ok) -> Ready<Result<Option<U>, E>>>
where
F: FnMut(S::Ok) -> Result<(), E>,
F: Fn(S::Ok) -> Result<Option<U>, E>,
{
self.try_for_each(move |t| ready(f(t)))
self.try_filter_map(move |t| ready(f(t)))
}
#[inline]
@ -83,4 +88,14 @@ where
{
self.ready_try_fold(U::default(), f)
}
#[inline]
fn ready_try_for_each<F>(
self, mut f: F,
) -> TryForEach<Self, Ready<Result<(), E>>, impl FnMut(S::Ok) -> Ready<Result<(), E>>>
where
F: FnMut(S::Ok) -> Result<(), E>,
{
self.try_for_each(move |t| ready(f(t)))
}
}