From 5f6d0233ed971ece8955851c26a93fd852151e2d Mon Sep 17 00:00:00 2001 From: quininer Date: Sat, 16 Feb 2019 01:31:46 +0800 Subject: [PATCH] tokio only * remove io::Read/io::Write support * stable vecio --- Cargo.toml | 15 +++++---------- src/common/mod.rs | 40 ++++++--------------------------------- src/common/test_stream.rs | 16 ++++++++++++++++ src/lib.rs | 30 ++++++++--------------------- src/tokio_impl.rs | 2 +- 5 files changed, 36 insertions(+), 67 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 58973f0..9de53ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tokio-rustls" -version = "0.9.0" +version = "0.10.0-alpha" authors = ["quininer kel "] license = "MIT/Apache-2.0" repository = "https://github.com/quininer/tokio-rustls" @@ -15,18 +15,13 @@ travis-ci = { repository = "quininer/tokio-rustls" } appveyor = { repository = "quininer/tokio-rustls" } [dependencies] -futures = { version = "0.1", optional = true } -tokio-io = { version = "0.1.6", optional = true } -bytes = { version = "0.4", optional = true } -iovec = { version = "0.1", optional = true } +futures = "0.1" +tokio-io = "0.1.6" +bytes = "0.4" +iovec = "0.1" rustls = "0.15" webpki = "0.19" [dev-dependencies] tokio = "0.1.6" lazy_static = "1" - -[features] -default = ["tokio-support"] -nightly = ["bytes", "iovec"] -tokio-support = ["futures", "tokio-io"] diff --git a/src/common/mod.rs b/src/common/mod.rs index 8e4a5b1..19fedb1 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -1,16 +1,9 @@ -#[cfg(feature = "nightly")] -#[cfg(feature = "tokio-support")] mod vecbuf; use std::io::{ self, Read, Write }; -#[cfg(feature = "nightly")] -use std::io::Initializer; use rustls::Session; -#[cfg(feature = "nightly")] use rustls::WriteV; -#[cfg(feature = "nightly")] -#[cfg(feature = "tokio-support")] -use tokio_io::AsyncWrite; +use tokio_io::{ AsyncRead, AsyncWrite }; pub struct Stream<'a, S: 'a, IO: 'a> { @@ -18,11 +11,11 @@ pub struct Stream<'a, S: 'a, IO: 'a> { pub io: &'a mut IO } -pub trait WriteTls<'a, S: Session, IO: Read + Write>: Read + Write { +pub trait WriteTls<'a, S: Session, IO: AsyncRead + AsyncWrite>: Read + Write { fn write_tls(&mut self) -> io::Result; } -impl<'a, S: Session, IO: Read + Write> Stream<'a, S, IO> { +impl<'a, S: Session, IO: AsyncRead + AsyncWrite> Stream<'a, S, IO> { pub fn new(session: &'a mut S, io: &'a mut IO) -> Self { Stream { session, io } } @@ -73,23 +66,7 @@ impl<'a, S: Session, IO: Read + Write> Stream<'a, S, IO> { } } -#[cfg(not(feature = "nightly"))] -impl<'a, S: Session, IO: Read + Write> WriteTls<'a, S, IO> for Stream<'a, S, IO> { - fn write_tls(&mut self) -> io::Result { - self.session.write_tls(self.io) - } -} - -#[cfg(feature = "nightly")] -impl<'a, S: Session, IO: Read + Write> WriteTls<'a, S, IO> for Stream<'a, S, IO> { - default fn write_tls(&mut self) -> io::Result { - self.session.write_tls(self.io) - } -} - -#[cfg(feature = "nightly")] -#[cfg(feature = "tokio-support")] -impl<'a, S: Session, IO: Read + AsyncWrite> WriteTls<'a, S, IO> for Stream<'a, S, IO> { +impl<'a, S: Session, IO: AsyncRead + AsyncWrite> WriteTls<'a, S, IO> for Stream<'a, S, IO> { fn write_tls(&mut self) -> io::Result { use futures::Async; use self::vecbuf::VecBuf; @@ -112,12 +89,7 @@ impl<'a, S: Session, IO: Read + AsyncWrite> WriteTls<'a, S, IO> for Stream<'a, S } } -impl<'a, S: Session, IO: Read + Write> Read for Stream<'a, S, IO> { - #[cfg(feature = "nightly")] - unsafe fn initializer(&self) -> Initializer { - Initializer::nop() - } - +impl<'a, S: Session, IO: AsyncRead + AsyncWrite> Read for Stream<'a, S, IO> { fn read(&mut self, buf: &mut [u8]) -> io::Result { while self.session.wants_read() { if let (0, 0) = self.complete_io()? { @@ -128,7 +100,7 @@ impl<'a, S: Session, IO: Read + Write> Read for Stream<'a, S, IO> { } } -impl<'a, S: Session, IO: Read + Write> io::Write for Stream<'a, S, IO> { +impl<'a, S: Session, IO: AsyncRead + AsyncWrite> Write for Stream<'a, S, IO> { fn write(&mut self, buf: &[u8]) -> io::Result { let len = self.session.write(buf)?; while self.session.wants_write() { diff --git a/src/common/test_stream.rs b/src/common/test_stream.rs index 2cd9e87..66b34b6 100644 --- a/src/common/test_stream.rs +++ b/src/common/test_stream.rs @@ -7,6 +7,8 @@ use rustls::{ ServerSession, ClientSession, Session, NoClientAuth }; +use futures::{ Async, Poll }; +use tokio_io::{ AsyncRead, AsyncWrite }; use super::Stream; @@ -31,6 +33,13 @@ impl<'a> Write for Good<'a> { } } +impl<'a> AsyncRead for Good<'a> {} +impl<'a> AsyncWrite for Good<'a> { + fn shutdown(&mut self) -> Poll<(), io::Error> { + Ok(Async::Ready(())) + } +} + struct Bad(bool); impl Read for Bad { @@ -53,6 +62,13 @@ impl Write for Bad { } } +impl AsyncRead for Bad {} +impl AsyncWrite for Bad { + fn shutdown(&mut self) -> Poll<(), io::Error> { + Ok(Async::Ready(())) + } +} + #[test] fn stream_good() -> io::Result<()> { diff --git a/src/lib.rs b/src/lib.rs index 2aaf2e4..0cb0e3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,34 +1,25 @@ //! Asynchronous TLS/SSL streams for Tokio using [Rustls](https://github.com/ctz/rustls). -#![cfg_attr(feature = "nightly", feature(specialization, read_initializer))] - pub extern crate rustls; pub extern crate webpki; -#[cfg(feature = "tokio-support")] extern crate futures; -#[cfg(feature = "tokio-support")] extern crate tokio_io; -#[cfg(feature = "nightly")] -#[cfg(feature = "tokio-support")] extern crate bytes; -#[cfg(feature = "nightly")] -#[cfg(feature = "tokio-support")] extern crate iovec; mod common; -#[cfg(feature = "tokio-support")] mod tokio_impl; +mod tokio_impl; use std::io; use std::sync::Arc; -#[cfg(feature = "nightly")] -use std::io::Initializer; use webpki::DNSNameRef; use rustls::{ Session, ClientSession, ServerSession, ClientConfig, ServerConfig, }; +use tokio_io::{ AsyncRead, AsyncWrite }; use common::Stream; @@ -56,7 +47,7 @@ impl From> for TlsAcceptor { impl TlsConnector { pub fn connect(&self, domain: DNSNameRef, stream: IO) -> Connect - where IO: io::Read + io::Write + where IO: AsyncRead + AsyncWrite { Self::connect_with_session(stream, ClientSession::new(&self.inner, domain)) } @@ -64,7 +55,7 @@ impl TlsConnector { #[inline] pub fn connect_with_session(stream: IO, session: ClientSession) -> Connect - where IO: io::Read + io::Write + where IO: AsyncRead + AsyncWrite { Connect(MidHandshake { inner: Some(TlsStream { session, io: stream, is_shutdown: false, eof: false }) @@ -74,14 +65,14 @@ impl TlsConnector { impl TlsAcceptor { pub fn accept(&self, stream: IO) -> Accept - where IO: io::Read + io::Write, + where IO: AsyncRead + AsyncWrite, { Self::accept_with_session(stream, ServerSession::new(&self.inner)) } #[inline] pub fn accept_with_session(stream: IO, session: ServerSession) -> Accept - where IO: io::Read + io::Write + where IO: AsyncRead + AsyncWrite { Accept(MidHandshake { inner: Some(TlsStream { session, io: stream, is_shutdown: false, eof: false }) @@ -145,13 +136,8 @@ impl From<(IO, S)> for TlsStream { } impl io::Read for TlsStream - where IO: io::Read + io::Write, S: Session + where IO: AsyncRead + AsyncWrite, S: Session { - #[cfg(feature = "nightly")] - unsafe fn initializer(&self) -> Initializer { - Initializer::nop() - } - fn read(&mut self, buf: &mut [u8]) -> io::Result { if self.eof { return Ok(0); @@ -172,7 +158,7 @@ impl io::Read for TlsStream } impl io::Write for TlsStream - where IO: io::Read + io::Write, S: Session + where IO: AsyncRead + AsyncWrite, S: Session { fn write(&mut self, buf: &[u8]) -> io::Result { Stream::new(&mut self.session, &mut self.io).write(buf) diff --git a/src/tokio_impl.rs b/src/tokio_impl.rs index 7d9ba57..0897e93 100644 --- a/src/tokio_impl.rs +++ b/src/tokio_impl.rs @@ -35,7 +35,7 @@ impl Future for Accept { impl Future for MidHandshake where - IO: io::Read + io::Write, + IO: AsyncRead + AsyncWrite, S: Session { type Item = TlsStream;