#[cfg(feature = "alloc")]
use alloc::boxed::Box;
use crate::{
prelude::*,
producer::compat::{iterator_to_producer, IteratorToProducer},
};
pub struct IntoProducerRef<'s, T>(IteratorToProducer<<&'s [T] as IntoIterator>::IntoIter>);
impl<'s, T> Producer for IntoProducerRef<'s, T> {
type Item = &'s T;
type Final = ();
type Error = Infallible;
async fn produce(&mut self) -> Result<Either<Self::Item, Self::Final>, Self::Error> {
self.0.produce().await
}
async fn slurp(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
impl<'s, T> crate::IntoProducer for &'s [T] {
type Item = &'s T;
type Final = ();
type Error = Infallible;
type IntoProducer = IntoProducerRef<'s, T>;
fn into_producer(self) -> Self::IntoProducer {
IntoProducerRef(iterator_to_producer(self.iter()))
}
}
pub struct IntoProducerMut<'s, T>(IteratorToProducer<<&'s mut [T] as IntoIterator>::IntoIter>);
impl<'s, T> Producer for IntoProducerMut<'s, T> {
type Item = &'s mut T;
type Final = ();
type Error = Infallible;
async fn produce(&mut self) -> Result<Either<Self::Item, Self::Final>, Self::Error> {
self.0.produce().await
}
async fn slurp(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
impl<'s, T> crate::IntoProducer for &'s mut [T] {
type Item = &'s mut T;
type Final = ();
type Error = Infallible;
type IntoProducer = IntoProducerMut<'s, T>;
fn into_producer(self) -> Self::IntoProducer {
IntoProducerMut(iterator_to_producer(self.iter_mut()))
}
}
#[cfg(feature = "alloc")]
pub struct IntoProducerBoxed<T>(IteratorToProducer<<Box<[T]> as IntoIterator>::IntoIter>);
#[cfg(feature = "alloc")]
impl<T> Producer for IntoProducerBoxed<T> {
type Item = T;
type Final = ();
type Error = Infallible;
async fn produce(&mut self) -> Result<Either<Self::Item, Self::Final>, Self::Error> {
self.0.produce().await
}
async fn slurp(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(feature = "alloc")]
impl<T> crate::IntoProducer for Box<[T]> {
type Item = T;
type Final = ();
type Error = Infallible;
type IntoProducer = IntoProducerBoxed<T>;
fn into_producer(self) -> Self::IntoProducer {
IntoProducerBoxed(iterator_to_producer(<Box<[T]> as IntoIterator>::into_iter(
self,
)))
}
}
#[cfg(feature = "alloc")]
#[allow(clippy::borrowed_box)]
pub struct IntoProducerBoxedRef<'s, T>(
IteratorToProducer<<&'s Box<[T]> as IntoIterator>::IntoIter>,
);
#[cfg(feature = "alloc")]
impl<'s, T> Producer for IntoProducerBoxedRef<'s, T> {
type Item = &'s T;
type Final = ();
type Error = Infallible;
async fn produce(&mut self) -> Result<Either<Self::Item, Self::Final>, Self::Error> {
self.0.produce().await
}
async fn slurp(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(feature = "alloc")]
impl<'s, T> crate::IntoProducer for &'s Box<[T]> {
type Item = &'s T;
type Final = ();
type Error = Infallible;
type IntoProducer = IntoProducerBoxedRef<'s, T>;
fn into_producer(self) -> Self::IntoProducer {
IntoProducerBoxedRef(iterator_to_producer(self.into_iter()))
}
}
#[cfg(feature = "alloc")]
pub struct IntoProducerBoxedMut<'s, T>(
IteratorToProducer<<&'s mut Box<[T]> as IntoIterator>::IntoIter>,
);
#[cfg(feature = "alloc")]
impl<'s, T> Producer for IntoProducerBoxedMut<'s, T> {
type Item = &'s mut T;
type Final = ();
type Error = Infallible;
async fn produce(&mut self) -> Result<Either<Self::Item, Self::Final>, Self::Error> {
self.0.produce().await
}
async fn slurp(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(feature = "alloc")]
impl<'s, T> crate::IntoProducer for &'s mut Box<[T]> {
type Item = &'s mut T;
type Final = ();
type Error = Infallible;
type IntoProducer = IntoProducerBoxedMut<'s, T>;
fn into_producer(self) -> Self::IntoProducer {
IntoProducerBoxedMut(iterator_to_producer(self.into_iter()))
}
}