use super::types::AnyResult;
use crate::io::readers::Data;
pub use super::streams::Stream;
pub type CloneableBoxIterator<T, E> = Box<dyn CloneableIterator<Item = AnyResult<T, E>>>;
pub trait CloneableIterator: Iterator {
fn clone_box_iterator(&self) -> Box<dyn CloneableIterator<Item = Self::Item>>;
}
pub type CloneableSendBoxIterator<T, E> = Box<dyn CloneableSendIterator<Item = AnyResult<T, E>>>;
pub trait CloneableSendIterator: Iterator + Send + Sync {
fn clone_box_send_iterator(&self) -> Box<dyn CloneableSendIterator<Item = Self::Item>>;
}
impl<T, I> CloneableIterator for T
where
T: Iterator<Item = I> + Clone + 'static,
{
fn clone_box_iterator(&self) -> Box<dyn CloneableIterator<Item = I>> {
Box::new(self.clone())
}
}
impl<T, I> CloneableSendIterator for T
where
T: Iterator<Item = I> + Clone + Send + Sync + 'static,
{
fn clone_box_send_iterator(&self) -> Box<dyn CloneableSendIterator<Item = I>> {
Box::new(self.clone())
}
}
impl<T: 'static> Clone for Box<dyn CloneableIterator<Item = T>> {
fn clone(&self) -> Self {
self.clone_box_iterator()
}
}
pub struct CanCloneIterator<T>(Box<dyn CloneableIterator<Item = T>>);
impl<T> CanCloneIterator<T> {
#[must_use]
pub fn new(elem: Box<dyn CloneableIterator<Item = T>>) -> Self {
Self(elem)
}
}
impl<T: 'static> Clone for CanCloneIterator<T> {
fn clone(&self) -> Self {
Self(self.0.clone_box_iterator())
}
}
impl<T> Iterator for CanCloneIterator<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub struct CanCloneSendIterator<T>(Box<dyn CloneableSendIterator<Item = T>>);
impl<T> CanCloneSendIterator<T> {
#[must_use]
pub fn new(elem: Box<dyn CloneableSendIterator<Item = T>>) -> Self {
Self(elem)
}
}
impl<T: 'static> Clone for CanCloneSendIterator<T> {
fn clone(&self) -> Self {
Self(self.0.clone_box_send_iterator())
}
}
impl<T> Iterator for CanCloneSendIterator<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub type BoxedIterator<T> = Box<dyn Iterator<Item = T>>;
pub type BoxedSendIterator<T> = Box<dyn Iterator<Item = T> + Send + Sync>;
pub type BoxedSendableIterator<T, E> = BoxedSendIterator<AnyResult<T, E>>;
pub type BoxedSendableVecIterator<E> = BoxedSendableIterator<Vec<u8>, E>;
pub type BoxedSendableDataIterator<E> = BoxedSendableIterator<Data, E>;
pub type BoxedResultIterator<T, E> = BoxedIterator<AnyResult<T, E>>;
pub type StringBoxedIterator<E> = BoxedResultIterator<String, E>;
pub type VecBoxedIterator<E> = BoxedResultIterator<Vec<u8>, E>;
pub trait SendableIterator<T>: Iterator<Item = T> + Send {}
pub type SendableBoxIterator<T, E> = Box<dyn SendableIterator<AnyResult<T, E>>>;
pub type SendVecIterator<E> = SendableBoxIterator<Vec<u8>, E>;
pub struct TransformSendIterator<T: Send, V: Send> {
transformer: Box<dyn Fn(T) -> Option<V> + Send + 'static>,
source: Box<dyn SendableIterator<T>>,
}
impl<T: Send, V: Send> TransformSendIterator<T, V> {
#[must_use]
pub fn new(
tn: Box<dyn Fn(T) -> Option<V> + Send + 'static>,
source: Box<dyn SendableIterator<T>>,
) -> Self {
Self {
transformer: tn,
source,
}
}
}
impl<T: Send, V: Send> SendableIterator<V> for TransformSendIterator<T, V> {}
impl<T: Send, V: Send> Iterator for TransformSendIterator<T, V> {
type Item = V;
fn next(&mut self) -> Option<Self::Item> {
for item in self.source.by_ref() {
if let Some(transformed) = (self.transformer)(item) {
return Some(transformed);
}
}
None
}
}
pub struct TransformIterator<T, V> {
transformer: Box<dyn Fn(T) -> Option<V>>,
source: BoxedIterator<T>,
}
impl<T, V> TransformIterator<T, V> {
#[must_use]
pub fn new(tn: Box<dyn Fn(T) -> Option<V>>, source: BoxedIterator<T>) -> Self {
Self {
transformer: tn,
source,
}
}
}
impl<T, V> Iterator for TransformIterator<T, V> {
type Item = V;
fn next(&mut self) -> Option<Self::Item> {
for item in self.source.by_ref() {
if let Some(transformed) = (self.transformer)(item) {
return Some(transformed);
}
}
None
}
}
pub struct TransformUntilIterator<T, V> {
transformer: Box<dyn Fn(T) -> Option<V>>,
source: BoxedIterator<T>,
}
impl<T, V> TransformUntilIterator<T, V> {
#[must_use]
pub fn new(tn: Box<dyn Fn(T) -> Option<V>>, source: BoxedIterator<T>) -> Self {
Self {
transformer: tn,
source,
}
}
}
impl<T, V> Iterator for TransformUntilIterator<T, V> {
type Item = V;
fn next(&mut self) -> Option<Self::Item> {
match self.source.next() {
Some(item) => (self.transformer)(item),
None => None,
}
}
}
pub struct TransformUntilSendIterator<T: Send, V: Send> {
transformer: Box<dyn Fn(T) -> Option<V> + Send + 'static>,
source: Box<dyn SendableIterator<T>>,
}
impl<T: Send, V: Send> TransformUntilSendIterator<T, V> {
#[must_use]
pub fn new(
tn: Box<dyn Fn(T) -> Option<V> + Send + 'static>,
source: Box<dyn SendableIterator<T>>,
) -> Self {
Self {
transformer: tn,
source,
}
}
}
impl<T: Send, V: Send> SendableIterator<V> for TransformUntilSendIterator<T, V> {}
impl<T: Send, V: Send> Iterator for TransformUntilSendIterator<T, V> {
type Item = V;
fn next(&mut self) -> Option<Self::Item> {
match self.source.next() {
Some(item) => (self.transformer)(item),
None => None,
}
}
}