tokio only

* remove io::Read/io::Write support
* stable vecio
This commit is contained in:
quininer 2019-02-16 01:31:46 +08:00
parent db21c4c947
commit 5f6d0233ed
5 changed files with 36 additions and 67 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "tokio-rustls" name = "tokio-rustls"
version = "0.9.0" version = "0.10.0-alpha"
authors = ["quininer kel <quininer@live.com>"] authors = ["quininer kel <quininer@live.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
repository = "https://github.com/quininer/tokio-rustls" repository = "https://github.com/quininer/tokio-rustls"
@ -15,18 +15,13 @@ travis-ci = { repository = "quininer/tokio-rustls" }
appveyor = { repository = "quininer/tokio-rustls" } appveyor = { repository = "quininer/tokio-rustls" }
[dependencies] [dependencies]
futures = { version = "0.1", optional = true } futures = "0.1"
tokio-io = { version = "0.1.6", optional = true } tokio-io = "0.1.6"
bytes = { version = "0.4", optional = true } bytes = "0.4"
iovec = { version = "0.1", optional = true } iovec = "0.1"
rustls = "0.15" rustls = "0.15"
webpki = "0.19" webpki = "0.19"
[dev-dependencies] [dev-dependencies]
tokio = "0.1.6" tokio = "0.1.6"
lazy_static = "1" lazy_static = "1"
[features]
default = ["tokio-support"]
nightly = ["bytes", "iovec"]
tokio-support = ["futures", "tokio-io"]

View File

@ -1,16 +1,9 @@
#[cfg(feature = "nightly")]
#[cfg(feature = "tokio-support")]
mod vecbuf; mod vecbuf;
use std::io::{ self, Read, Write }; use std::io::{ self, Read, Write };
#[cfg(feature = "nightly")]
use std::io::Initializer;
use rustls::Session; use rustls::Session;
#[cfg(feature = "nightly")]
use rustls::WriteV; use rustls::WriteV;
#[cfg(feature = "nightly")] use tokio_io::{ AsyncRead, AsyncWrite };
#[cfg(feature = "tokio-support")]
use tokio_io::AsyncWrite;
pub struct Stream<'a, S: 'a, IO: 'a> { 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 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<usize>; fn write_tls(&mut self) -> io::Result<usize>;
} }
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 { pub fn new(session: &'a mut S, io: &'a mut IO) -> Self {
Stream { session, io } 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: AsyncRead + AsyncWrite> WriteTls<'a, S, IO> for Stream<'a, S, IO> {
impl<'a, S: Session, IO: Read + Write> WriteTls<'a, S, IO> for Stream<'a, S, IO> {
fn write_tls(&mut self) -> io::Result<usize> {
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<usize> {
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> {
fn write_tls(&mut self) -> io::Result<usize> { fn write_tls(&mut self) -> io::Result<usize> {
use futures::Async; use futures::Async;
use self::vecbuf::VecBuf; 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> { impl<'a, S: Session, IO: AsyncRead + AsyncWrite> Read for Stream<'a, S, IO> {
#[cfg(feature = "nightly")]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
}
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
while self.session.wants_read() { while self.session.wants_read() {
if let (0, 0) = self.complete_io()? { 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<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let len = self.session.write(buf)?; let len = self.session.write(buf)?;
while self.session.wants_write() { while self.session.wants_write() {

View File

@ -7,6 +7,8 @@ use rustls::{
ServerSession, ClientSession, ServerSession, ClientSession,
Session, NoClientAuth Session, NoClientAuth
}; };
use futures::{ Async, Poll };
use tokio_io::{ AsyncRead, AsyncWrite };
use super::Stream; 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); struct Bad(bool);
impl Read for Bad { 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] #[test]
fn stream_good() -> io::Result<()> { fn stream_good() -> io::Result<()> {

View File

@ -1,34 +1,25 @@
//! Asynchronous TLS/SSL streams for Tokio using [Rustls](https://github.com/ctz/rustls). //! 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 rustls;
pub extern crate webpki; pub extern crate webpki;
#[cfg(feature = "tokio-support")]
extern crate futures; extern crate futures;
#[cfg(feature = "tokio-support")]
extern crate tokio_io; extern crate tokio_io;
#[cfg(feature = "nightly")]
#[cfg(feature = "tokio-support")]
extern crate bytes; extern crate bytes;
#[cfg(feature = "nightly")]
#[cfg(feature = "tokio-support")]
extern crate iovec; extern crate iovec;
mod common; mod common;
#[cfg(feature = "tokio-support")] mod tokio_impl; mod tokio_impl;
use std::io; use std::io;
use std::sync::Arc; use std::sync::Arc;
#[cfg(feature = "nightly")]
use std::io::Initializer;
use webpki::DNSNameRef; use webpki::DNSNameRef;
use rustls::{ use rustls::{
Session, ClientSession, ServerSession, Session, ClientSession, ServerSession,
ClientConfig, ServerConfig, ClientConfig, ServerConfig,
}; };
use tokio_io::{ AsyncRead, AsyncWrite };
use common::Stream; use common::Stream;
@ -56,7 +47,7 @@ impl From<Arc<ServerConfig>> for TlsAcceptor {
impl TlsConnector { impl TlsConnector {
pub fn connect<IO>(&self, domain: DNSNameRef, stream: IO) -> Connect<IO> pub fn connect<IO>(&self, domain: DNSNameRef, stream: IO) -> Connect<IO>
where IO: io::Read + io::Write where IO: AsyncRead + AsyncWrite
{ {
Self::connect_with_session(stream, ClientSession::new(&self.inner, domain)) Self::connect_with_session(stream, ClientSession::new(&self.inner, domain))
} }
@ -64,7 +55,7 @@ impl TlsConnector {
#[inline] #[inline]
pub fn connect_with_session<IO>(stream: IO, session: ClientSession) pub fn connect_with_session<IO>(stream: IO, session: ClientSession)
-> Connect<IO> -> Connect<IO>
where IO: io::Read + io::Write where IO: AsyncRead + AsyncWrite
{ {
Connect(MidHandshake { Connect(MidHandshake {
inner: Some(TlsStream { session, io: stream, is_shutdown: false, eof: false }) inner: Some(TlsStream { session, io: stream, is_shutdown: false, eof: false })
@ -74,14 +65,14 @@ impl TlsConnector {
impl TlsAcceptor { impl TlsAcceptor {
pub fn accept<IO>(&self, stream: IO) -> Accept<IO> pub fn accept<IO>(&self, stream: IO) -> Accept<IO>
where IO: io::Read + io::Write, where IO: AsyncRead + AsyncWrite,
{ {
Self::accept_with_session(stream, ServerSession::new(&self.inner)) Self::accept_with_session(stream, ServerSession::new(&self.inner))
} }
#[inline] #[inline]
pub fn accept_with_session<IO>(stream: IO, session: ServerSession) -> Accept<IO> pub fn accept_with_session<IO>(stream: IO, session: ServerSession) -> Accept<IO>
where IO: io::Read + io::Write where IO: AsyncRead + AsyncWrite
{ {
Accept(MidHandshake { Accept(MidHandshake {
inner: Some(TlsStream { session, io: stream, is_shutdown: false, eof: false }) inner: Some(TlsStream { session, io: stream, is_shutdown: false, eof: false })
@ -145,13 +136,8 @@ impl<IO, S: Session> From<(IO, S)> for TlsStream<IO, S> {
} }
impl<IO, S> io::Read for TlsStream<IO, S> impl<IO, S> io::Read for TlsStream<IO, S>
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<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if self.eof { if self.eof {
return Ok(0); return Ok(0);
@ -172,7 +158,7 @@ impl<IO, S> io::Read for TlsStream<IO, S>
} }
impl<IO, S> io::Write for TlsStream<IO, S> impl<IO, S> io::Write for TlsStream<IO, S>
where IO: io::Read + io::Write, S: Session where IO: AsyncRead + AsyncWrite, S: Session
{ {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Stream::new(&mut self.session, &mut self.io).write(buf) Stream::new(&mut self.session, &mut self.io).write(buf)

View File

@ -35,7 +35,7 @@ impl<IO: AsyncRead + AsyncWrite> Future for Accept<IO> {
impl<IO, S> Future for MidHandshake<IO, S> impl<IO, S> Future for MidHandshake<IO, S>
where where
IO: io::Read + io::Write, IO: AsyncRead + AsyncWrite,
S: Session S: Session
{ {
type Item = TlsStream<IO, S>; type Item = TlsStream<IO, S>;