pub mod convert;
pub mod filter;
pub mod filter_map;
pub mod map;
mod _quad;
pub use _quad::*;
mod _stream_error;
pub use _stream_error::*;
mod _triple;
pub use _triple::*;
use std::{convert::Infallible, error::Error};
pub trait Source {
type Item<'x>;
type Error: Error + Send + Sync + 'static;
fn try_for_some_item<E, F>(&mut self, f: F) -> StreamResult<bool, Self::Error, E>
where
E: Error + Send + Sync + 'static,
F: FnMut(Self::Item<'_>) -> Result<(), E>;
#[inline]
fn try_for_each_item<F, E>(&mut self, mut f: F) -> StreamResult<(), Self::Error, E>
where
F: FnMut(Self::Item<'_>) -> Result<(), E>,
E: Error + Send + Sync + 'static,
{
while self.try_for_some_item(&mut f)? {}
Ok(())
}
#[inline]
fn for_some_item<F>(&mut self, mut f: F) -> Result<bool, Self::Error>
where
F: FnMut(Self::Item<'_>),
{
self.try_for_some_item(|t| -> Result<(), Self::Error> {
f(t);
Ok(())
})
.map_err(StreamError::inner_into)
}
#[inline]
fn for_each_item<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnMut(Self::Item<'_>),
{
let mut f = f;
while self.for_some_item(&mut f)? {}
Ok(())
}
#[inline]
fn filter_items<F>(self, predicate: F) -> filter::FilterSource<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item<'_>) -> bool,
{
filter::FilterSource {
source: self,
predicate,
}
}
#[inline]
fn filter_map_items<F, T>(self, filter_map: F) -> filter_map::FilterMapSource<Self, F>
where
Self: Sized,
F: FnMut(Self::Item<'_>) -> Option<T>,
{
filter_map::FilterMapSource {
source: self,
filter_map,
}
}
#[inline]
fn map_items<F, T>(self, map: F) -> map::MapSource<Self, F>
where
Self: Sized,
F: FnMut(Self::Item<'_>) -> T,
{
map::MapSource { source: self, map }
}
fn size_hint_items(&self) -> (usize, Option<usize>) {
(0, None)
}
}
impl<'a, I, T, E> Source for I
where
I: Iterator<Item = Result<T, E>> + 'a,
E: Error + Send + Sync + 'static,
{
type Item<'x> = T;
type Error = E;
fn try_for_some_item<E2, F>(&mut self, mut f: F) -> StreamResult<bool, Self::Error, E2>
where
E2: Error + Send + Sync + 'static,
F: FnMut(Self::Item<'_>) -> Result<(), E2>,
{
match self.next() {
Some(Err(e)) => Err(SourceError(e)),
Some(Ok(t)) => {
f(t).map_err(SinkError)?;
Ok(true)
}
None => Ok(false),
}
}
fn size_hint_items(&self) -> (usize, Option<usize>) {
self.size_hint()
}
}
pub trait IntoSource: Iterator + Sized {
#[allow(clippy::type_complexity)]
fn into_source(
self,
) -> std::iter::Map<
Self,
fn(<Self as Iterator>::Item) -> Result<<Self as Iterator>::Item, Infallible>,
> {
self.map(Ok::<_, Infallible>)
}
}
impl<I> IntoSource for I where I: Iterator {}