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