use bytes::Bytes;
use tokio::sync::mpsc;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Disconnected;
impl std::fmt::Display for Disconnected {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("client disconnected")
}
}
impl std::error::Error for Disconnected {}
#[derive(Clone)]
pub struct BodySender {
tx: mpsc::Sender<Bytes>,
}
impl BodySender {
pub(crate) fn new(tx: mpsc::Sender<Bytes>) -> Self {
Self { tx }
}
pub async fn send(&self, chunk: impl Into<Bytes>) -> Result<(), Disconnected> {
self.tx.send(chunk.into()).await.map_err(|_| Disconnected)
}
pub fn is_closed(&self) -> bool {
self.tx.is_closed()
}
pub async fn closed(&self) {
self.tx.closed().await
}
}