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;
#[derive(Clone)]
pub struct TlsConnector {
inner: Arc<ClientConfig>
}
#[derive(Clone)]
pub struct TlsAcceptor {
inner: Arc<ServerConfig>
}
@ -49,16 +51,16 @@ impl From<Arc<ServerConfig>> for TlsAcceptor {
}
impl TlsConnector {
pub fn connect<S>(&self, domain: DNSNameRef, stream: S) -> Connect<S>
where S: io::Read + io::Write
pub fn connect<IO>(&self, domain: DNSNameRef, stream: IO) -> Connect<IO>
where IO: io::Read + io::Write
{
Self::connect_with_session(stream, ClientSession::new(&self.inner, domain))
}
#[inline]
pub fn connect_with_session<S>(stream: S, session: ClientSession)
-> Connect<S>
where S: io::Read + io::Write
pub fn connect_with_session<IO>(stream: IO, session: ClientSession)
-> Connect<IO>
where IO: io::Read + io::Write
{
Connect(MidHandshake {
inner: Some(TlsStream { session, io: stream, is_shutdown: false, eof: false })
@ -67,15 +69,15 @@ impl TlsConnector {
}
impl TlsAcceptor {
pub fn accept<S>(&self, stream: S) -> Accept<S>
where S: io::Read + io::Write,
pub fn accept<IO>(&self, stream: IO) -> Accept<IO>
where IO: io::Read + io::Write,
{
Self::accept_with_session(stream, ServerSession::new(&self.inner))
}
#[inline]
pub fn accept_with_session<S>(stream: S, session: ServerSession) -> Accept<S>
where S: io::Read + io::Write
pub fn accept_with_session<IO>(stream: IO, session: ServerSession) -> Accept<IO>
where IO: io::Read + io::Write
{
Accept(MidHandshake {
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
/// 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
/// once the accept handshake has finished.
pub struct Accept<S>(MidHandshake<S, ServerSession>);
pub struct Accept<IO>(MidHandshake<IO, ServerSession>);
struct MidHandshake<S, C> {
inner: Option<TlsStream<S, C>>
struct MidHandshake<IO, S> {
inner: Option<TlsStream<IO, S>>
}
/// A wrapper around an underlying raw stream which implements the TLS or SSL
/// protocol.
#[derive(Debug)]
pub struct TlsStream<S, C> {
pub struct TlsStream<IO, S> {
is_shutdown: bool,
eof: bool,
io: S,
session: C
io: IO,
session: S
}
impl<S, C> TlsStream<S, C> {
impl<IO, S> TlsStream<IO, S> {
#[inline]
pub fn get_ref(&self) -> (&S, &C) {
pub fn get_ref(&self) -> (&IO, &S) {
(&self.io, &self.session)
}
#[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)
}
#[inline]
pub fn into_inner(self) -> (IO, S) {
(self.io, self.session)
}
}
impl<S, C> io::Read for TlsStream<S, C>
where S: io::Read + io::Write, C: Session
impl<IO, S> From<(IO, S)> for TlsStream<IO, S> {
#[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> {
if self.eof {
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>
where S: io::Read + io::Write, C: Session
impl<IO, S> io::Write for TlsStream<IO, S>
where IO: io::Read + io::Write, S: Session
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Stream::new(&mut self.session, &mut self.io).write(buf)

View File

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