Skip to content

Commit

Permalink
tokio-tls: enable Send and Sync (#1317)
Browse files Browse the repository at this point in the history
-  update 0 as *mut () calls to std::ptr::null_mut()
- impl Send and Sync for AllowStd
  • Loading branch information
jxs authored and carllerche committed Jul 19, 2019
1 parent d0bb161 commit a298472
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions tokio-tls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::future::Future;
use std::io::{self, Read, Write};
use std::marker::Unpin;
use std::pin::Pin;
use std::ptr::null_mut;
use std::task::{Context, Poll};
use tokio_io::{AsyncRead, AsyncWrite};

Expand Down Expand Up @@ -77,10 +78,14 @@ where
AllowStd<S>: Read + Write,
{
fn drop(&mut self) {
(self.0).0.get_mut().context = 0 as *mut ();
(self.0).0.get_mut().context = null_mut();
}
}

// *mut () context is neither Send nor Sync
unsafe impl<S: Send> Send for AllowStd<S> {}
unsafe impl<S: Sync> Sync for AllowStd<S> {}

impl<S> AllowStd<S>
where
S: Unpin,
Expand Down Expand Up @@ -234,11 +239,11 @@ where

match (inner.f)(stream) {
Ok(mut s) => {
s.get_mut().context = 0 as *mut ();
s.get_mut().context = null_mut();
Poll::Ready(Ok(StartedHandshake::Done(TlsStream(s))))
}
Err(HandshakeError::WouldBlock(mut s)) => {
s.get_mut().context = 0 as *mut ();
s.get_mut().context = null_mut();
Poll::Ready(Ok(StartedHandshake::Mid(s)))
}
Err(HandshakeError::Failure(e)) => Poll::Ready(Err(e)),
Expand Down Expand Up @@ -310,7 +315,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin> Future for MidHandshake<S> {
Ok(stream) => Poll::Ready(Ok(TlsStream(stream))),
Err(HandshakeError::Failure(e)) => Poll::Ready(Err(e)),
Err(HandshakeError::WouldBlock(mut s)) => {
s.get_mut().context = 0 as *mut ();
s.get_mut().context = null_mut();
mut_self.0 = Some(s);
Poll::Pending
}
Expand Down

0 comments on commit a298472

Please sign in to comment.