use core::fmt::Debug;
use core::hash::Hash;
use std::collections::HashMap;
use crate::prelude::*;
#[derive(Debug)]
pub struct IntoConsumer<K, V>(HashMap<K, V>);
impl<K, V> From<IntoConsumer<K, V>> for HashMap<K, V> {
fn from(value: IntoConsumer<K, V>) -> Self {
value.0
}
}
impl<K: Hash + Eq, V> Consumer for IntoConsumer<K, V> {
type Item = (K, V);
type Final = ();
type Error = Infallible;
async fn consume(&mut self, val: Either<Self::Item, Self::Final>) -> Result<(), Self::Error> {
match val {
Left(item) => {
self.0.insert(item.0, item.1);
Ok(())
}
Right(()) => Ok(()),
}
}
async fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
impl<K: Hash + Eq, V> crate::IntoConsumer for HashMap<K, V> {
type Item = (K, V);
type Final = ();
type Error = Infallible;
type IntoConsumer = IntoConsumer<K, V>;
fn into_consumer(self) -> Self::IntoConsumer {
IntoConsumer(self)
}
}
#[derive(Debug)]
pub struct IntoConsumerMut<'a, K, V>(&'a mut HashMap<K, V>);
impl<'a, K: Hash + Eq, V> Consumer for IntoConsumerMut<'a, K, V> {
type Item = (K, V);
type Final = ();
type Error = Infallible;
async fn consume(&mut self, val: Either<Self::Item, Self::Final>) -> Result<(), Self::Error> {
match val {
Left(item) => {
self.0.insert(item.0, item.1);
Ok(())
}
Right(()) => Ok(()),
}
}
async fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
impl<'a, K: Hash + Eq, V> crate::IntoConsumer for &'a mut HashMap<K, V> {
type Item = (K, V);
type Final = ();
type Error = Infallible;
type IntoConsumer = IntoConsumerMut<'a, K, V>;
fn into_consumer(self) -> Self::IntoConsumer {
IntoConsumerMut(self)
}
}