use tokio::sync::oneshot;
#[derive(Debug)]
pub struct AlgebraMessage<Operation, Reply> {
operation: Operation,
responder: AlgebraResponder<Reply>,
}
#[derive(Debug)]
pub struct AlgebraResponder<Reply>(Option<oneshot::Sender<Reply>>);
impl<Operation, Reply> AlgebraMessage<Operation, Reply> {
pub const fn new(operation: Operation, answering: oneshot::Sender<Reply>) -> Self {
Self { operation, responder: AlgebraResponder(Some(answering)) }
}
pub(crate) const fn unheard(operation: Operation) -> Self {
Self { operation, responder: AlgebraResponder(None) }
}
pub const fn operation(&self) -> &Operation {
&self.operation
}
pub fn into_parts(self) -> (Operation, AlgebraResponder<Reply>) {
(self.operation, self.responder)
}
}
impl<Reply> AlgebraResponder<Reply> {
pub fn respond(self, reply: Reply) {
if let Some(answering) = self.0 {
let _unheard = answering.send(reply);
}
}
pub fn is_awaited(&self) -> bool {
self.0.as_ref().is_some_and(|answering| !answering.is_closed())
}
}