mod ring_buf;
mod split_by;
mod split_by_buffered;
mod split_by_map;
mod split_by_map_buffered;
pub(crate) use split_by::SplitBy;
pub use split_by::{FalseSplitBy, TrueSplitBy};
pub(crate) use split_by_buffered::SplitByBuffered;
pub use split_by_buffered::{FalseSplitByBuffered, TrueSplitByBuffered};
pub(crate) use split_by_map::SplitByMap;
pub use split_by_map::{LeftSplitByMap, RightSplitByMap};
pub(crate) use split_by_map_buffered::SplitByMapBuffered;
pub use split_by_map_buffered::{LeftSplitByMapBuffered, RightSplitByMapBuffered};
pub use futures::future::Either;
use futures::Stream;
pub trait SplitStreamByExt<P>: Stream {
fn split_by(
self,
predicate: P,
) -> (
TrueSplitBy<Self::Item, Self, P>,
FalseSplitBy<Self::Item, Self, P>,
)
where
P: Fn(&Self::Item) -> bool,
Self: Sized,
{
let stream = SplitBy::new(self, predicate);
let true_stream = TrueSplitBy::new(stream.clone());
let false_stream = FalseSplitBy::new(stream);
(true_stream, false_stream)
}
fn split_by_buffered<const N: usize>(
self,
predicate: P,
) -> (
TrueSplitByBuffered<Self::Item, Self, P, N>,
FalseSplitByBuffered<Self::Item, Self, P, N>,
)
where
P: Fn(&Self::Item) -> bool,
Self: Sized,
{
let stream = SplitByBuffered::new(self, predicate);
let true_stream = TrueSplitByBuffered::new(stream.clone());
let false_stream = FalseSplitByBuffered::new(stream);
(true_stream, false_stream)
}
}
impl<T, P> SplitStreamByExt<P> for T where T: Stream + ?Sized {}
pub trait SplitStreamByMapExt<P, L, R>: Stream {
fn split_by_map(
self,
predicate: P,
) -> (
LeftSplitByMap<Self::Item, L, R, Self, P>,
RightSplitByMap<Self::Item, L, R, Self, P>,
)
where
P: Fn(Self::Item) -> Either<L, R>,
Self: Sized,
{
let stream = SplitByMap::new(self, predicate);
let true_stream = LeftSplitByMap::new(stream.clone());
let false_stream = RightSplitByMap::new(stream);
(true_stream, false_stream)
}
fn split_by_map_buffered<const N: usize>(
self,
predicate: P,
) -> (
LeftSplitByMapBuffered<Self::Item, L, R, Self, P, N>,
RightSplitByMapBuffered<Self::Item, L, R, Self, P, N>,
)
where
P: Fn(Self::Item) -> Either<L, R>,
Self: Sized,
{
let stream = SplitByMapBuffered::new(self, predicate);
let true_stream = LeftSplitByMapBuffered::new(stream.clone());
let false_stream = RightSplitByMapBuffered::new(stream);
(true_stream, false_stream)
}
}
impl<T, P, L, R> SplitStreamByMapExt<P, L, R> for T where T: Stream + ?Sized {}