1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use async_trait::async_trait;
use dyn_clone::DynClone;
use std::io;
use tokio::sync::{mpsc, oneshot};
#[async_trait]
pub trait Shutdown: DynClone + Send + Sync {
async fn shutdown(&self) -> io::Result<()>;
}
#[async_trait]
impl Shutdown for mpsc::Sender<oneshot::Sender<io::Result<()>>> {
async fn shutdown(&self) -> io::Result<()> {
let (tx, rx) = oneshot::channel();
match self.send(tx).await {
Ok(_) => match rx.await {
Ok(x) => x,
Err(_) => Err(already_shutdown()),
},
Err(_) => Err(already_shutdown()),
}
}
}
#[inline]
fn already_shutdown() -> io::Error {
io::Error::new(io::ErrorKind::Other, "Client already shutdown")
}
impl Clone for Box<dyn Shutdown> {
fn clone(&self) -> Self {
dyn_clone::clone_box(&**self)
}
}