continuwuity/src/core/utils/stream/try_ready.rs
Jason Volk 768e81741c use FnMut for ready_try_for_each extension
Signed-off-by: Jason Volk <jason@zemos.net>
2024-11-03 14:51:07 +00:00

52 lines
1.4 KiB
Rust

//! Synchronous combinator extensions to futures::TryStream
#![allow(clippy::type_complexity)]
use futures::{
future::{ready, Ready},
stream::{AndThen, TryForEach, TryStream, TryStreamExt},
};
use crate::Result;
/// Synchronous combinators to augment futures::TryStreamExt.
///
/// This interface is not necessarily complete; feel free to add as-needed.
pub trait TryReadyExt<T, E, S>
where
S: TryStream<Ok = T, Error = E, Item = Result<T, E>> + Send + ?Sized,
Self: TryStream + Send + Sized,
{
fn ready_and_then<U, F>(self, f: F) -> AndThen<Self, Ready<Result<U, E>>, impl FnMut(S::Ok) -> Ready<Result<U, E>>>
where
F: Fn(S::Ok) -> Result<U, E>;
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
where
S: TryStream<Ok = T, Error = E, Item = Result<T, E>> + Send + ?Sized,
Self: TryStream + Send + Sized,
{
#[inline]
fn ready_and_then<U, F>(self, f: F) -> AndThen<Self, Ready<Result<U, E>>, impl FnMut(S::Ok) -> Ready<Result<U, E>>>
where
F: Fn(S::Ok) -> Result<U, E>,
{
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>>>
where
F: FnMut(S::Ok) -> Result<(), E>,
{
self.try_for_each(move |t| ready(f(t)))
}
}