use anyhow::anyhow;
use std::fmt::Debug;
use tokio::sync::oneshot;
#[derive(Debug)]
pub struct ReplyTo<T> {
sender: oneshot::Sender<T>,
}
impl<T> ReplyTo<T> {
pub fn new() -> (oneshot::Receiver<T>, Self) {
let (tx, rx) = oneshot::channel();
let ask_reply = Self { sender: tx };
(rx, ask_reply)
}
}
pub trait ReplyWith<T> {
fn reply_with(self, reply: T) -> anyhow::Result<()>;
}
impl<T> ReplyWith<T> for ReplyTo<T> {
fn reply_with(self, reply: T) -> anyhow::Result<()> {
match self.sender.send(reply) {
Ok(_) => Ok(()),
Err(_) => {
tracing::error!("Error sending reply");
Err(anyhow!("Error sending reply"))
}
}
}
}