use std::fmt; use actix_utils::{mpsc, oneshot}; use futures::future::{Future, FutureExt}; use crate::dispatcher::FramedMessage; pub struct Sink(mpsc::Sender>); impl Clone for Sink { fn clone(&self) -> Self { Sink(self.0.clone()) } } impl Sink { pub(crate) fn new(tx: mpsc::Sender>) -> Self { Sink(tx) } /// Close connection pub fn close(&self) { let _ = self.0.send(FramedMessage::Close); } /// Close connection pub fn wait_close(&self) -> impl Future { let (tx, rx) = oneshot::channel(); let _ = self.0.send(FramedMessage::WaitClose(tx)); rx.map(|_| ()) } /// Send item pub fn send(&self, item: T) { let _ = self.0.send(FramedMessage::Message(item)); } } impl fmt::Debug for Sink { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_struct("Sink").finish() } }