use crate::Span;
use bytes::Bytes;
use commonware_utils::channel::{fallible::OneshotExt, oneshot};
use std::collections::HashMap;
#[derive(Clone, Default)]
pub struct Producer<K: Span, V> {
data: HashMap<K, V>,
}
impl<K: Span, V> Producer<K, V> {
pub fn insert(&mut self, key: K, value: V) {
self.data.insert(key, value);
}
}
impl<K: Span, V: Into<Bytes> + Clone + Send + 'static> crate::p2p::Producer for Producer<K, V> {
type Key = K;
async fn produce(&mut self, key: Self::Key) -> oneshot::Receiver<Bytes> {
let (sender, receiver) = oneshot::channel();
if let Some(value) = self.data.get(&key) {
sender.send_lossy(value.clone().into());
}
receiver
}
}