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