//! Synchronous combinator extensions to futures::TryStream #![allow(clippy::type_complexity)] use futures::{ future::{Ready, ready}, stream::{AndThen, TryFilterMap, TryFold, TryForEach, TryStream, TryStreamExt, TryTakeWhile}, }; 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> + ?Sized, Self: TryStream + Sized, { fn ready_and_then( self, f: F, ) -> AndThen>, impl FnMut(S::Ok) -> Ready>> where F: Fn(S::Ok) -> Result; fn ready_try_filter_map( self, f: F, ) -> TryFilterMap< Self, Ready, E>>, impl FnMut(S::Ok) -> Ready, E>>, > where F: Fn(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; fn ready_try_for_each( self, f: F, ) -> TryForEach>, impl FnMut(S::Ok) -> Ready>> where F: FnMut(S::Ok) -> Result<(), E>; fn ready_try_take_while( self, f: F, ) -> TryTakeWhile>, impl FnMut(&S::Ok) -> Ready>> where F: Fn(&S::Ok) -> Result; } impl TryReadyExt for S where S: TryStream> + ?Sized, Self: TryStream + 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))) } fn ready_try_filter_map( self, f: F, ) -> TryFilterMap< Self, Ready, E>>, impl FnMut(S::Ok) -> Ready, E>>, > where F: Fn(S::Ok) -> Result, E>, { self.try_filter_map(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) } #[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_take_while( self, f: F, ) -> TryTakeWhile>, impl FnMut(&S::Ok) -> Ready>> where F: Fn(&S::Ok) -> Result, { self.try_take_while(move |t| ready(f(t))) } }