use async_trait::async_trait;
use crate::Result;
#[async_trait]
pub trait Producer<T: Send>: std::fmt::Debug + Send + Sync {
async fn publish(&self, item: T) -> Result<()>;
}
#[async_trait]
pub trait Consumer<T: Send>: std::fmt::Debug + Send {
async fn recv(&mut self) -> Result<Option<Delivery<T>>>;
}
#[derive(Debug)]
pub struct Delivery<T> {
item: T,
handle: Box<dyn AckHandle>,
}
impl<T> Delivery<T> {
pub fn new(item: T, handle: Box<dyn AckHandle>) -> Self {
Self { item, handle }
}
pub fn into_parts(self) -> (T, Box<dyn AckHandle>) {
(self.item, self.handle)
}
pub async fn nack(self) -> Result<()> {
self.handle.nack().await
}
}
#[async_trait]
pub trait AckHandle: std::fmt::Debug + Send {
async fn ack(self: Box<Self>) -> Result<()>;
async fn nack(self: Box<Self>) -> Result<()>;
}