use std::collections::HashMap;
use crate::{
prelude::*,
producer::compat::{iterator_to_producer, IteratorToProducer},
};
pub struct IntoProducer<K, V>(IteratorToProducer<<HashMap<K, V> as IntoIterator>::IntoIter>);
impl<K, V> Producer for IntoProducer<K, V> {
type Item = (K, V);
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<K, V> crate::IntoProducer for HashMap<K, V> {
type Item = (K, V);
type Final = ();
type Error = Infallible;
type IntoProducer = IntoProducer<K, V>;
fn into_producer(self) -> Self::IntoProducer {
IntoProducer(iterator_to_producer(
<HashMap<K, V> as IntoIterator>::into_iter(self),
))
}
}
pub struct IntoProducerRef<'s, K, V>(
IteratorToProducer<<&'s HashMap<K, V> as IntoIterator>::IntoIter>,
);
impl<'s, K, V> Producer for IntoProducerRef<'s, K, V> {
type Item = (&'s K, &'s V);
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, K, V> crate::IntoProducer for &'s HashMap<K, V> {
type Item = (&'s K, &'s V);
type Final = ();
type Error = Infallible;
type IntoProducer = IntoProducerRef<'s, K, V>;
fn into_producer(self) -> Self::IntoProducer {
IntoProducerRef(iterator_to_producer(self.iter()))
}
}
pub struct IntoProducerMut<'s, K, V>(
IteratorToProducer<<&'s mut HashMap<K, V> as IntoIterator>::IntoIter>,
);
impl<'s, K, V> Producer for IntoProducerMut<'s, K, V> {
type Item = (&'s K, &'s mut V);
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, K, V> crate::IntoProducer for &'s mut HashMap<K, V> {
type Item = (&'s K, &'s mut V);
type Final = ();
type Error = Infallible;
type IntoProducer = IntoProducerMut<'s, K, V>;
fn into_producer(self) -> Self::IntoProducer {
IntoProducerMut(iterator_to_producer(self.iter_mut()))
}
}