//! Synchronous combinator extensions to futures::TryStream #![allow(clippy::type_complexity)] use futures::{ future::{ready, Ready}, stream::{AndThen, TryFold, 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 where S: TryStream> + Send + ?Sized, Self: TryStream + Send + Sized, { fn ready_and_then(self, f: F) -> AndThen>, impl FnMut(S::Ok) -> Ready>> where F: Fn(S::Ok) -> Result; fn ready_try_for_each( self, f: F, ) -> TryForEach>, impl FnMut(S::Ok) -> Ready>> where F: FnMut(S::Ok) -> Result<(), E>; fn ready_try_fold( self, init: U, f: F, ) -> TryFold>, U, impl FnMut(U, S::Ok) -> Ready>> where F: Fn(U, S::Ok) -> Result; fn ready_try_fold_default( self, f: F, ) -> TryFold>, U, impl FnMut(U, S::Ok) -> Ready>> where F: Fn(U, S::Ok) -> Result, U: Default; } impl TryReadyExt for S where S: TryStream> + Send + ?Sized, Self: TryStream + Send + Sized, { #[inline] fn ready_and_then(self, f: F) -> AndThen>, impl FnMut(S::Ok) -> Ready>> where F: Fn(S::Ok) -> Result, { self.and_then(move |t| ready(f(t))) } #[inline] fn ready_try_for_each( self, mut f: F, ) -> TryForEach>, impl FnMut(S::Ok) -> Ready>> where F: FnMut(S::Ok) -> Result<(), E>, { self.try_for_each(move |t| ready(f(t))) } #[inline] fn ready_try_fold( self, init: U, f: F, ) -> TryFold>, U, impl FnMut(U, S::Ok) -> Ready>> where F: Fn(U, S::Ok) -> Result, { self.try_fold(init, move |a, t| ready(f(a, t))) } #[inline] fn ready_try_fold_default( self, f: F, ) -> TryFold>, U, impl FnMut(U, S::Ok) -> Ready>> where F: Fn(U, S::Ok) -> Result, U: Default, { self.ready_try_fold(U::default(), f) } }