From 074fe4a5ac0bf6174136b572394e2f0d1963c26c Mon Sep 17 00:00:00 2001 From: quininer Date: Sun, 8 Dec 2019 00:52:55 +0800 Subject: [PATCH] Move TlsState to common --- src/common/mod.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 55 +---------------------------------------------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/common/mod.rs b/src/common/mod.rs index 1deb21d..a53f548 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -7,6 +7,59 @@ use tokio::io::{ AsyncRead, AsyncWrite }; use futures_core as futures; +#[derive(Debug)] +pub enum TlsState { + #[cfg(feature = "early-data")] + EarlyData(usize, Vec), + Stream, + ReadShutdown, + WriteShutdown, + FullyShutdown, +} + +impl TlsState { + pub fn shutdown_read(&mut self) { + match *self { + TlsState::WriteShutdown | TlsState::FullyShutdown => *self = TlsState::FullyShutdown, + _ => *self = TlsState::ReadShutdown, + } + } + + pub fn shutdown_write(&mut self) { + match *self { + TlsState::ReadShutdown | TlsState::FullyShutdown => *self = TlsState::FullyShutdown, + _ => *self = TlsState::WriteShutdown, + } + } + + pub fn writeable(&self) -> bool { + match *self { + TlsState::WriteShutdown | TlsState::FullyShutdown => false, + _ => true, + } + } + + pub fn readable(&self) -> bool { + match self { + TlsState::ReadShutdown | TlsState::FullyShutdown => false, + _ => true, + } + } + + #[cfg(feature = "early-data")] + pub fn is_early_data(&self) -> bool { + match self { + TlsState::EarlyData(..) => true, + _ => false + } + } + + #[cfg(not(feature = "early-data"))] + pub const fn is_early_data(&self) -> bool { + false + } +} + pub struct Stream<'a, IO, S> { pub io: &'a mut IO, pub session: &'a mut S, diff --git a/src/lib.rs b/src/lib.rs index 706e972..09c10e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,64 +13,11 @@ use futures_core as futures; use tokio::io::{ AsyncRead, AsyncWrite }; use webpki::DNSNameRef; use rustls::{ ClientConfig, ClientSession, ServerConfig, ServerSession, Session }; -use common::Stream; +use common::{ Stream, TlsState }; pub use rustls; pub use webpki; -#[derive(Debug)] -enum TlsState { - #[cfg(feature = "early-data")] - EarlyData(usize, Vec), - Stream, - ReadShutdown, - WriteShutdown, - FullyShutdown, -} - -impl TlsState { - fn shutdown_read(&mut self) { - match *self { - TlsState::WriteShutdown | TlsState::FullyShutdown => *self = TlsState::FullyShutdown, - _ => *self = TlsState::ReadShutdown, - } - } - - fn shutdown_write(&mut self) { - match *self { - TlsState::ReadShutdown | TlsState::FullyShutdown => *self = TlsState::FullyShutdown, - _ => *self = TlsState::WriteShutdown, - } - } - - fn writeable(&self) -> bool { - match *self { - TlsState::WriteShutdown | TlsState::FullyShutdown => false, - _ => true, - } - } - - fn readable(&self) -> bool { - match self { - TlsState::ReadShutdown | TlsState::FullyShutdown => false, - _ => true, - } - } - - #[cfg(feature = "early-data")] - fn is_early_data(&self) -> bool { - match self { - TlsState::EarlyData(..) => true, - _ => false - } - } - - #[cfg(not(feature = "early-data"))] - const fn is_early_data(&self) -> bool { - false - } -} - /// A wrapper around a `rustls::ClientConfig`, providing an async `connect` method. #[derive(Clone)] pub struct TlsConnector {