Skip to content

Commit

Permalink
Merge pull request #21 from sfackler/tokio-03
Browse files Browse the repository at this point in the history
Update to tokio 0.3
  • Loading branch information
sfackler authored Oct 16, 2020
2 parents ce265e1 + 8bff465 commit 4272041
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ An implementation of SSL streams for Tokio backed by OpenSSL
[dependencies]
openssl = "0.10.30"
openssl-sys = "0.9.58"
tokio = "0.2"
tokio = "0.3"

[dev-dependencies]
futures = "0.3"
tokio = { version = "0.2", features = ["full"] }
tokio = { version = "0.3", features = ["full"] }
38 changes: 18 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ use std::future::Future;
use std::io::{self, Read, Write};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite};
use std::mem::MaybeUninit;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

/// Asynchronously performs a client-side TLS handshake over the provided stream.
pub async fn connect<S>(
Expand Down Expand Up @@ -99,10 +98,13 @@ where
S: AsyncRead + Unpin,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.with_context(|ctx, stream| stream.poll_read(ctx, buf)) {
Poll::Ready(r) => r,
Poll::Pending => Err(io::Error::from(io::ErrorKind::WouldBlock)),
}
self.with_context(|ctx, stream| {
let mut buf = ReadBuf::new(buf);
match stream.poll_read(ctx, &mut buf)? {
Poll::Ready(()) => Ok(buf.filled().len()),
Poll::Pending => Err(io::Error::from(io::ErrorKind::WouldBlock)),
}
})
}
}

Expand Down Expand Up @@ -182,10 +184,7 @@ where
///
/// The caller must ensure the pointer is valid.
pub unsafe fn from_raw_parts(ssl: *mut ffi::SSL, stream: S) -> Self {
let stream = StreamWrapper {
stream,
context: 0,
};
let stream = StreamWrapper { stream, context: 0 };
SslStream(ssl::SslStream::from_raw_parts(ssl, stream))
}
}
Expand All @@ -194,19 +193,18 @@ impl<S> AsyncRead for SslStream<S>
where
S: AsyncRead + AsyncWrite + Unpin,
{
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [MaybeUninit<u8>]) -> bool {
// Note that this does not forward to `S` because the buffer is
// unconditionally filled in by OpenSSL, not the actual object `S`.
// We're decrypting bytes from `S` into the buffer above!
false
}

fn poll_read(
mut self: Pin<&mut Self>,
ctx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
self.with_context(ctx, |s| cvt(s.read(buf)))
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
self.with_context(ctx, |s| match cvt(s.read(buf.initialize_unfilled()))? {
Poll::Ready(nread) => {
buf.advance(nread);
Poll::Ready(Ok(()))
}
Poll::Pending => Poll::Pending,
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn google() {

#[tokio::test]
async fn server() {
let mut listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();

let server = async move {
Expand Down

0 comments on commit 4272041

Please sign in to comment.