actix_ioframe/
sink.rs

1use std::fmt;
2use std::rc::Rc;
3
4use actix_utils::oneshot;
5use futures::future::{Future, FutureExt};
6
7use crate::dispatcher::Message;
8
9pub struct Sink<T>(Rc<dyn Fn(Message<T>)>);
10
11impl<T> Clone for Sink<T> {
12    fn clone(&self) -> Self {
13        Sink(self.0.clone())
14    }
15}
16
17impl<T> Sink<T> {
18    pub(crate) fn new(tx: Rc<dyn Fn(Message<T>)>) -> Self {
19        Sink(tx)
20    }
21
22    /// Close connection
23    pub fn close(&self) {
24        (self.0)(Message::Close);
25    }
26
27    /// Close connection
28    pub fn wait_close(&self) -> impl Future<Output = ()> {
29        let (tx, rx) = oneshot::channel();
30        (self.0)(Message::WaitClose(tx));
31
32        rx.map(|_| ())
33    }
34
35    /// Send item
36    pub fn send(&self, item: T) {
37        (self.0)(Message::Item(item));
38    }
39}
40
41impl<T> fmt::Debug for Sink<T> {
42    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
43        fmt.debug_struct("Sink").finish()
44    }
45}