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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use super::AsyncTls;
use pin_project_lite::pin_project;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::task::JoinHandle;
#[cfg_attr(docsrs, doc(cfg(feature = "rt")))]
#[derive(Clone, Debug)]
pub struct SpawningHandshakes<T>(pub T);
impl<C, T> AsyncTls<C> for SpawningHandshakes<T>
where
T: AsyncTls<C>,
C: AsyncRead + AsyncWrite,
T::AcceptFuture: Send + 'static,
T::Stream: Send + 'static,
T::Error: Send + 'static,
{
type Stream = T::Stream;
type Error = T::Error;
type AcceptFuture = HandshakeJoin<T::Stream, T::Error>;
fn accept(&self, stream: C) -> Self::AcceptFuture {
HandshakeJoin(tokio::spawn(self.0.accept(stream)))
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "rt")))]
pub struct HandshakeJoin<Stream, Error>(JoinHandle<Result<Stream, Error>>);
impl<Stream, Error> Future for HandshakeJoin<Stream, Error> {
type Output = Result<Stream, Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match Pin::new(&mut self.as_mut().0).poll(cx) {
Poll::Ready(Ok(v)) => Poll::Ready(v),
Poll::Pending => Poll::Pending,
Poll::Ready(Err(e)) => {
if e.is_panic() {
std::panic::resume_unwind(e.into_panic());
} else {
unreachable!("Tls handshake was aborted: {:?}", e);
}
}
}
}
}
impl<Stream, Error> Drop for HandshakeJoin<Stream, Error> {
fn drop(&mut self) {
self.0.abort();
}
}