//! Synchronous combinator extensions to futures::Stream use futures::{ future::{ready, Ready}, stream::{Any, Filter, FilterMap, Fold, ForEach, SkipWhile, Stream, StreamExt, TakeWhile}, }; /// Synchronous combinators to augment futures::StreamExt. Most Stream /// combinators take asynchronous arguments, but often only simple predicates /// are required to steer a Stream like an Iterator. This suite provides a /// convenience to reduce boilerplate by de-cluttering non-async predicates. /// /// This interface is not necessarily complete; feel free to add as-needed. pub trait ReadyExt where S: Stream + Send + ?Sized, Self: Stream + Send + Sized, { fn ready_any(self, f: F) -> Any, impl FnMut(S::Item) -> Ready> where F: Fn(S::Item) -> bool; fn ready_filter<'a, F>(self, f: F) -> Filter, impl FnMut(&S::Item) -> Ready + 'a> where F: Fn(&S::Item) -> bool + 'a; fn ready_filter_map(self, f: F) -> FilterMap>, impl FnMut(S::Item) -> Ready>> where F: Fn(S::Item) -> Option; fn ready_fold(self, init: T, f: F) -> Fold, T, impl FnMut(T, S::Item) -> Ready> where F: Fn(T, S::Item) -> T; fn ready_for_each(self, f: F) -> ForEach, impl FnMut(S::Item) -> Ready<()>> where F: FnMut(S::Item); fn ready_take_while<'a, F>(self, f: F) -> TakeWhile, impl FnMut(&S::Item) -> Ready + 'a> where F: Fn(&S::Item) -> bool + 'a; fn ready_skip_while<'a, F>(self, f: F) -> SkipWhile, impl FnMut(&S::Item) -> Ready + 'a> where F: Fn(&S::Item) -> bool + 'a; } impl ReadyExt for S where S: Stream + Send + ?Sized, Self: Stream + Send + Sized, { #[inline] fn ready_any(self, f: F) -> Any, impl FnMut(S::Item) -> Ready> where F: Fn(S::Item) -> bool, { self.any(move |t| ready(f(t))) } #[inline] fn ready_filter<'a, F>(self, f: F) -> Filter, impl FnMut(&S::Item) -> Ready + 'a> where F: Fn(&S::Item) -> bool + 'a, { self.filter(move |t| ready(f(t))) } #[inline] fn ready_filter_map(self, f: F) -> FilterMap>, impl FnMut(S::Item) -> Ready>> where F: Fn(S::Item) -> Option, { self.filter_map(move |t| ready(f(t))) } #[inline] fn ready_fold(self, init: T, f: F) -> Fold, T, impl FnMut(T, S::Item) -> Ready> where F: Fn(T, S::Item) -> T, { self.fold(init, move |a, t| ready(f(a, t))) } #[inline] #[allow(clippy::unit_arg)] fn ready_for_each(self, mut f: F) -> ForEach, impl FnMut(S::Item) -> Ready<()>> where F: FnMut(S::Item), { self.for_each(move |t| ready(f(t))) } #[inline] fn ready_take_while<'a, F>(self, f: F) -> TakeWhile, impl FnMut(&S::Item) -> Ready + 'a> where F: Fn(&S::Item) -> bool + 'a, { self.take_while(move |t| ready(f(t))) } #[inline] fn ready_skip_while<'a, F>(self, f: F) -> SkipWhile, impl FnMut(&S::Item) -> Ready + 'a> where F: Fn(&S::Item) -> bool + 'a, { self.skip_while(move |t| ready(f(t))) } }