add TryWideband trait to similar to TryBroadband

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-01-20 07:38:19 +00:00
parent ac944496c1
commit 388730d6dd
2 changed files with 59 additions and 0 deletions

View file

@ -9,6 +9,7 @@ mod tools;
mod try_broadband;
mod try_ready;
mod try_tools;
mod try_wideband;
mod wideband;
pub use band::{
@ -25,4 +26,5 @@ pub use tools::Tools;
pub use try_broadband::TryBroadbandExt;
pub use try_ready::TryReadyExt;
pub use try_tools::TryTools;
pub use try_wideband::TryWidebandExt;
pub use wideband::WidebandExt;

View file

@ -0,0 +1,57 @@
//! Synchronous combinator extensions to futures::TryStream
use futures::{TryFuture, TryStream, TryStreamExt};
use super::automatic_width;
use crate::Result;
/// Concurrency extensions to augment futures::TryStreamExt. wide_ combinators
/// produce in-order results
pub trait TryWidebandExt<T, E>
where
Self: TryStream<Ok = T, Error = E, Item = Result<T, E>> + Send + Sized,
{
fn widen_and_then<U, F, Fut, N>(
self,
n: N,
f: F,
) -> impl TryStream<Ok = U, Error = E, Item = Result<U, E>> + Send
where
N: Into<Option<usize>>,
F: Fn(Self::Ok) -> Fut + Send,
Fut: TryFuture<Ok = U, Error = E, Output = Result<U, E>> + Send,
U: Send;
fn wide_and_then<U, F, Fut>(
self,
f: F,
) -> impl TryStream<Ok = U, Error = E, Item = Result<U, E>> + Send
where
F: Fn(Self::Ok) -> Fut + Send,
Fut: TryFuture<Ok = U, Error = E, Output = Result<U, E>> + Send,
U: Send,
{
self.widen_and_then(None, f)
}
}
impl<T, E, S> TryWidebandExt<T, E> for S
where
S: TryStream<Ok = T, Error = E, Item = Result<T, E>> + Send + Sized,
E: Send,
{
fn widen_and_then<U, F, Fut, N>(
self,
n: N,
f: F,
) -> impl TryStream<Ok = U, Error = E, Item = Result<U, E>> + Send
where
N: Into<Option<usize>>,
F: Fn(Self::Ok) -> Fut + Send,
Fut: TryFuture<Ok = U, Error = E, Output = Result<U, E>> + Send,
U: Send,
{
self.map_ok(f)
.try_buffered(n.into().unwrap_or_else(automatic_width))
}
}