rename generic name

This commit is contained in:
quininer 2018-09-23 02:00:52 +08:00
parent e723182108
commit 6b493615a9
2 changed files with 59 additions and 36 deletions

View File

@ -28,10 +28,12 @@ use rustls::{
use common::Stream; use common::Stream;
#[derive(Clone)]
pub struct TlsConnector { pub struct TlsConnector {
inner: Arc<ClientConfig> inner: Arc<ClientConfig>
} }
#[derive(Clone)]
pub struct TlsAcceptor { pub struct TlsAcceptor {
inner: Arc<ServerConfig> inner: Arc<ServerConfig>
} }
@ -49,16 +51,16 @@ impl From<Arc<ServerConfig>> for TlsAcceptor {
} }
impl TlsConnector { impl TlsConnector {
pub fn connect<S>(&self, domain: DNSNameRef, stream: S) -> Connect<S> pub fn connect<IO>(&self, domain: DNSNameRef, stream: IO) -> Connect<IO>
where S: io::Read + io::Write where IO: io::Read + io::Write
{ {
Self::connect_with_session(stream, ClientSession::new(&self.inner, domain)) Self::connect_with_session(stream, ClientSession::new(&self.inner, domain))
} }
#[inline] #[inline]
pub fn connect_with_session<S>(stream: S, session: ClientSession) pub fn connect_with_session<IO>(stream: IO, session: ClientSession)
-> Connect<S> -> Connect<IO>
where S: io::Read + io::Write where IO: io::Read + io::Write
{ {
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 })
@ -67,15 +69,15 @@ impl TlsConnector {
} }
impl TlsAcceptor { impl TlsAcceptor {
pub fn accept<S>(&self, stream: S) -> Accept<S> pub fn accept<IO>(&self, stream: IO) -> Accept<IO>
where S: io::Read + io::Write, where IO: io::Read + io::Write,
{ {
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<S>(stream: S, session: ServerSession) -> Accept<S> pub fn accept_with_session<IO>(stream: IO, session: ServerSession) -> Accept<IO>
where S: io::Read + io::Write where IO: io::Read + io::Write
{ {
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 })
@ -86,43 +88,64 @@ impl TlsAcceptor {
/// Future returned from `ClientConfigExt::connect_async` which will resolve /// Future returned from `ClientConfigExt::connect_async` which will resolve
/// once the connection handshake has finished. /// once the connection handshake has finished.
pub struct Connect<S>(MidHandshake<S, ClientSession>); pub struct Connect<IO>(MidHandshake<IO, ClientSession>);
/// Future returned from `ServerConfigExt::accept_async` which will resolve /// Future returned from `ServerConfigExt::accept_async` which will resolve
/// once the accept handshake has finished. /// once the accept handshake has finished.
pub struct Accept<S>(MidHandshake<S, ServerSession>); pub struct Accept<IO>(MidHandshake<IO, ServerSession>);
struct MidHandshake<S, C> { struct MidHandshake<IO, S> {
inner: Option<TlsStream<S, C>> inner: Option<TlsStream<IO, S>>
} }
/// A wrapper around an underlying raw stream which implements the TLS or SSL /// A wrapper around an underlying raw stream which implements the TLS or SSL
/// protocol. /// protocol.
#[derive(Debug)] #[derive(Debug)]
pub struct TlsStream<S, C> { pub struct TlsStream<IO, S> {
is_shutdown: bool, is_shutdown: bool,
eof: bool, eof: bool,
io: S, io: IO,
session: C session: S
} }
impl<S, C> TlsStream<S, C> { impl<IO, S> TlsStream<IO, S> {
#[inline] #[inline]
pub fn get_ref(&self) -> (&S, &C) { pub fn get_ref(&self) -> (&IO, &S) {
(&self.io, &self.session) (&self.io, &self.session)
} }
#[inline] #[inline]
pub fn get_mut(&mut self) -> (&mut S, &mut C) { pub fn get_mut(&mut self) -> (&mut IO, &mut S) {
(&mut self.io, &mut self.session) (&mut self.io, &mut self.session)
} }
#[inline]
pub fn into_inner(self) -> (IO, S) {
(self.io, self.session)
}
} }
impl<S, C> io::Read for TlsStream<S, C> impl<IO, S> From<(IO, S)> for TlsStream<IO, S> {
where S: io::Read + io::Write, C: Session #[inline]
fn from((io, session): (IO, S)) -> TlsStream<IO, S> {
TlsStream {
is_shutdown: false,
eof: false,
io, session
}
}
}
impl<IO, S> io::Read for TlsStream<IO, S>
where IO: io::Read + io::Write, 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);
@ -142,8 +165,8 @@ impl<S, C> io::Read for TlsStream<S, C>
} }
} }
impl<S, C> io::Write for TlsStream<S, C> impl<IO, S> io::Write for TlsStream<IO, S>
where S: io::Read + io::Write, C: Session where IO: io::Read + io::Write, 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

@ -5,8 +5,8 @@ use tokio::prelude::Poll;
use common::Stream; use common::Stream;
impl<S: AsyncRead + AsyncWrite> Future for Connect<S> { impl<IO: AsyncRead + AsyncWrite> Future for Connect<IO> {
type Item = TlsStream<S, ClientSession>; type Item = TlsStream<IO, ClientSession>;
type Error = io::Error; type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -14,8 +14,8 @@ impl<S: AsyncRead + AsyncWrite> Future for Connect<S> {
} }
} }
impl<S: AsyncRead + AsyncWrite> Future for Accept<S> { impl<IO: AsyncRead + AsyncWrite> Future for Accept<IO> {
type Item = TlsStream<S, ServerSession>; type Item = TlsStream<IO, ServerSession>;
type Error = io::Error; type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -23,10 +23,10 @@ impl<S: AsyncRead + AsyncWrite> Future for Accept<S> {
} }
} }
impl<S, C> Future for MidHandshake<S, C> impl<IO, S> Future for MidHandshake<IO, S>
where S: io::Read + io::Write, C: Session where IO: io::Read + io::Write, S: Session
{ {
type Item = TlsStream<S, C>; type Item = TlsStream<IO, S>;
type Error = io::Error; type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -48,20 +48,20 @@ impl<S, C> Future for MidHandshake<S, C>
} }
} }
impl<S, C> AsyncRead for TlsStream<S, C> impl<IO, S> AsyncRead for TlsStream<IO, S>
where where
S: AsyncRead + AsyncWrite, IO: AsyncRead + AsyncWrite,
C: Session S: Session
{ {
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool { unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
false false
} }
} }
impl<S, C> AsyncWrite for TlsStream<S, C> impl<IO, S> AsyncWrite for TlsStream<IO, S>
where where
S: AsyncRead + AsyncWrite, IO: AsyncRead + AsyncWrite,
C: Session S: Session
{ {
fn shutdown(&mut self) -> Poll<(), io::Error> { fn shutdown(&mut self) -> Poll<(), io::Error> {
if !self.is_shutdown { if !self.is_shutdown {