Skip to content

Commit

Permalink
tokio-rustls: release 0.14.0 (hyperium#17)
Browse files Browse the repository at this point in the history
* tokio-rustls: release 0.14.0

* Fix writev

* Fix fmt
  • Loading branch information
quininer authored Jul 5, 2020
1 parent fc90b3f commit c2dd82e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 46 deletions.
6 changes: 3 additions & 3 deletions tokio-rustls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tokio-rustls"
version = "0.13.1"
version = "0.14.0"
authors = ["quininer kel <quininer@live.com>"]
license = "MIT/Apache-2.0"
repository = "https://github.com/tokio-rs/tls"
Expand All @@ -14,7 +14,7 @@ edition = "2018"
[dependencies]
tokio = "0.2.0"
futures-core = "0.3.1"
rustls = "0.17"
rustls = "0.18"
webpki = "0.21"

bytes = { version = "0.5", optional = true }
Expand All @@ -28,4 +28,4 @@ unstable = ["bytes"]
tokio = { version = "0.2.0", features = ["macros", "net", "io-util", "rt-core", "time"] }
futures-util = "0.3.1"
lazy_static = "1"
webpki-roots = "0.19"
webpki-roots = "0.20"
1 change: 1 addition & 0 deletions tokio-rustls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ where
let mut stream =
Stream::new(&mut this.io, &mut this.session).set_eof(!this.state.readable());

#[allow(clippy::match_single_binding)]
match this.state {
#[cfg(feature = "early-data")]
TlsState::EarlyData(ref mut pos, ref mut data) => {
Expand Down
47 changes: 16 additions & 31 deletions tokio-rustls/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod vecbuf;
use futures_core as futures;
pub(crate) use handshake::{IoSession, MidHandshake};
use rustls::Session;
use std::io::{self, Read};
use std::io::{self, Read, Write};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite};
Expand Down Expand Up @@ -103,6 +103,7 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin, S: Session> Stream<'a, IO, S> {
}

impl<'a, 'b, T: AsyncRead + Unpin> Read for Reader<'a, 'b, T> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match Pin::new(&mut self.io).poll_read(self.cx, buf) {
Poll::Ready(result) => result,
Expand Down Expand Up @@ -131,55 +132,39 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin, S: Session> Stream<'a, IO, S> {
Poll::Ready(Ok(n))
}

#[cfg(not(feature = "unstable"))]
pub fn write_io(&mut self, cx: &mut Context) -> Poll<io::Result<usize>> {
use std::io::Write;
#[cfg(feature = "unstable")]
use std::io::IoSlice;

struct Writer<'a, 'b, T> {
io: &'a mut T,
cx: &'a mut Context<'b>,
}

impl<'a, 'b, T: AsyncWrite + Unpin> Write for Writer<'a, 'b, T> {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match Pin::new(&mut self.io).poll_write(self.cx, buf) {
Poll::Ready(result) => result,
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()),
}
}

fn flush(&mut self) -> io::Result<()> {
match Pin::new(&mut self.io).poll_flush(self.cx) {
#[cfg(feature = "unstable")]
#[inline]
fn write_vectored(&mut self, bufs: &[IoSlice]) -> io::Result<usize> {
use vecbuf::VecBuf;

let mut vbuf = VecBuf::new(bufs);

match Pin::new(&mut self.io).poll_write_buf(self.cx, &mut vbuf) {
Poll::Ready(result) => result,
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()),
}
}
}

let mut writer = Writer { io: self.io, cx };

match self.session.write_tls(&mut writer) {
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => Poll::Pending,
result => Poll::Ready(result),
}
}

#[cfg(feature = "unstable")]
pub fn write_io(&mut self, cx: &mut Context) -> Poll<io::Result<usize>> {
use rustls::WriteV;

struct Writer<'a, 'b, T> {
io: &'a mut T,
cx: &'a mut Context<'b>,
}

impl<'a, 'b, T: AsyncWrite + Unpin> WriteV for Writer<'a, 'b, T> {
fn writev(&mut self, vbuf: &[&[u8]]) -> io::Result<usize> {
use vecbuf::VecBuf;

let mut vbuf = VecBuf::new(vbuf);

match Pin::new(&mut self.io).poll_write_buf(self.cx, &mut vbuf) {
fn flush(&mut self) -> io::Result<()> {
match Pin::new(&mut self.io).poll_flush(self.cx) {
Poll::Ready(result) => result,
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()),
}
Expand All @@ -188,7 +173,7 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin, S: Session> Stream<'a, IO, S> {

let mut writer = Writer { io: self.io, cx };

match self.session.writev_tls(&mut writer) {
match self.session.write_tls(&mut writer) {
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => Poll::Pending,
result => Poll::Ready(result),
}
Expand Down
31 changes: 19 additions & 12 deletions tokio-rustls/src/common/vecbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use bytes::Buf;
use std::cmp::{self, Ordering};
use std::io::IoSlice;

pub struct VecBuf<'a, 'b: 'a> {
pub struct VecBuf<'a> {
pos: usize,
cur: usize,
inner: &'a [&'b [u8]],
inner: &'a [IoSlice<'a>],
}

impl<'a, 'b> VecBuf<'a, 'b> {
pub fn new(vbytes: &'a [&'b [u8]]) -> Self {
impl<'a> VecBuf<'a> {
pub fn new(vbytes: &'a [IoSlice<'a>]) -> Self {
VecBuf {
pos: 0,
cur: 0,
Expand All @@ -18,7 +18,7 @@ impl<'a, 'b> VecBuf<'a, 'b> {
}
}

impl<'a, 'b> Buf for VecBuf<'a, 'b> {
impl<'a> Buf for VecBuf<'a> {
fn remaining(&self) -> usize {
let sum = self
.inner
Expand Down Expand Up @@ -56,6 +56,7 @@ impl<'a, 'b> Buf for VecBuf<'a, 'b> {
}

#[allow(clippy::needless_range_loop)]
#[inline]
fn bytes_vectored<'c>(&'c self, dst: &mut [IoSlice<'c>]) -> usize {
let len = cmp::min(self.inner.len() - self.pos, dst.len());

Expand All @@ -64,7 +65,7 @@ impl<'a, 'b> Buf for VecBuf<'a, 'b> {
}

for i in 1..len {
dst[i] = IoSlice::new(&self.inner[self.pos + i]);
dst[i] = self.inner[self.pos + i];
}

len
Expand All @@ -77,7 +78,8 @@ mod test_vecbuf {

#[test]
fn test_fresh_cursor_vec() {
let mut buf = VecBuf::new(&[b"he", b"llo"]);
let buf = [IoSlice::new(b"he"), IoSlice::new(b"llo")];
let mut buf = VecBuf::new(&buf);

assert_eq!(buf.remaining(), 5);
assert_eq!(buf.bytes(), b"he");
Expand All @@ -100,28 +102,33 @@ mod test_vecbuf {

#[test]
fn test_get_u8() {
let mut buf = VecBuf::new(&[b"\x21z", b"omg"]);
let buf = [IoSlice::new(b"\x21z"), IoSlice::new(b"omg")];
let mut buf = VecBuf::new(&buf);
assert_eq!(0x21, buf.get_u8());
}

#[test]
fn test_get_u16() {
let mut buf = VecBuf::new(&[b"\x21\x54z", b"omg"]);
let buf = [IoSlice::new(b"\x21\x54z"), IoSlice::new(b"omg")];
let mut buf = VecBuf::new(&buf);
assert_eq!(0x2154, buf.get_u16());
let mut buf = VecBuf::new(&[b"\x21\x54z", b"omg"]);
let buf = [IoSlice::new(b"\x21\x54z"), IoSlice::new(b"omg")];
let mut buf = VecBuf::new(&buf);
assert_eq!(0x5421, buf.get_u16_le());
}

#[test]
#[should_panic]
fn test_get_u16_buffer_underflow() {
let mut buf = VecBuf::new(&[b"\x21"]);
let buf = [IoSlice::new(b"\x21")];
let mut buf = VecBuf::new(&buf);
buf.get_u16();
}

#[test]
fn test_bufs_vec() {
let buf = VecBuf::new(&[b"he", b"llo"]);
let buf = [IoSlice::new(b"he"), IoSlice::new(b"llo")];
let buf = VecBuf::new(&buf);

let b1: &[u8] = &mut [0];
let b2: &[u8] = &mut [0];
Expand Down

0 comments on commit c2dd82e

Please sign in to comment.