Skip to content

Commit 78a4ea8

Browse files
committed
Move Connect and FallibleConnect into client module
1 parent 8c2af0c commit 78a4ea8

File tree

2 files changed

+53
-55
lines changed

2 files changed

+53
-55
lines changed

src/client.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::future::Future;
12
#[cfg(unix)]
23
use std::os::unix::io::{AsRawFd, RawFd};
34
#[cfg(windows)]
@@ -14,10 +15,7 @@ use std::{
1415
use rustls::{pki_types::ServerName, ClientConfig, ClientConnection};
1516
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
1617

17-
use crate::{
18-
common::{IoSession, MidHandshake, Stream, TlsState},
19-
Connect,
20-
};
18+
use crate::common::{IoSession, MidHandshake, Stream, TlsState};
2119

2220
/// A wrapper around a `rustls::ClientConfig`, providing an async `connect` method.
2321
#[derive(Clone)]
@@ -151,6 +149,56 @@ impl TlsConnectorWithAlpn<'_> {
151149
}
152150
}
153151

152+
/// Future returned from `TlsConnector::connect` which will resolve
153+
/// once the connection handshake has finished.
154+
pub struct Connect<IO>(MidHandshake<TlsStream<IO>>);
155+
156+
impl<IO> Connect<IO> {
157+
#[inline]
158+
pub fn into_fallible(self) -> FallibleConnect<IO> {
159+
FallibleConnect(self.0)
160+
}
161+
162+
pub fn get_ref(&self) -> Option<&IO> {
163+
match &self.0 {
164+
MidHandshake::Handshaking(sess) => Some(sess.get_ref().0),
165+
MidHandshake::SendAlert { io, .. } => Some(io),
166+
MidHandshake::Error { io, .. } => Some(io),
167+
MidHandshake::End => None,
168+
}
169+
}
170+
171+
pub fn get_mut(&mut self) -> Option<&mut IO> {
172+
match &mut self.0 {
173+
MidHandshake::Handshaking(sess) => Some(sess.get_mut().0),
174+
MidHandshake::SendAlert { io, .. } => Some(io),
175+
MidHandshake::Error { io, .. } => Some(io),
176+
MidHandshake::End => None,
177+
}
178+
}
179+
}
180+
181+
impl<IO: AsyncRead + AsyncWrite + Unpin> Future for Connect<IO> {
182+
type Output = io::Result<TlsStream<IO>>;
183+
184+
#[inline]
185+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
186+
Pin::new(&mut self.0).poll(cx).map_err(|(err, _)| err)
187+
}
188+
}
189+
190+
impl<IO: AsyncRead + AsyncWrite + Unpin> Future for FallibleConnect<IO> {
191+
type Output = Result<TlsStream<IO>, (io::Error, IO)>;
192+
193+
#[inline]
194+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
195+
Pin::new(&mut self.0).poll(cx)
196+
}
197+
}
198+
199+
/// Like [Connect], but returns `IO` on failure.
200+
pub struct FallibleConnect<IO>(MidHandshake<TlsStream<IO>>);
201+
154202
/// A wrapper around an underlying raw stream which implements the TLS or SSL
155203
/// protocol.
156204
#[derive(Debug)]

src/lib.rs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ macro_rules! ready {
6464
}
6565

6666
pub mod client;
67-
pub use client::{TlsConnector, TlsConnectorWithAlpn};
67+
pub use client::{Connect, FallibleConnect, TlsConnector, TlsConnectorWithAlpn};
6868
mod common;
6969
use common::{MidHandshake, TlsState};
7070
pub mod server;
@@ -295,45 +295,13 @@ where
295295
}
296296
}
297297

298-
/// Future returned from `TlsConnector::connect` which will resolve
299-
/// once the connection handshake has finished.
300-
pub struct Connect<IO>(MidHandshake<client::TlsStream<IO>>);
301-
302298
/// Future returned from `TlsAcceptor::accept` which will resolve
303299
/// once the accept handshake has finished.
304300
pub struct Accept<IO>(MidHandshake<server::TlsStream<IO>>);
305301

306-
/// Like [Connect], but returns `IO` on failure.
307-
pub struct FallibleConnect<IO>(MidHandshake<client::TlsStream<IO>>);
308-
309302
/// Like [Accept], but returns `IO` on failure.
310303
pub struct FallibleAccept<IO>(MidHandshake<server::TlsStream<IO>>);
311304

312-
impl<IO> Connect<IO> {
313-
#[inline]
314-
pub fn into_fallible(self) -> FallibleConnect<IO> {
315-
FallibleConnect(self.0)
316-
}
317-
318-
pub fn get_ref(&self) -> Option<&IO> {
319-
match &self.0 {
320-
MidHandshake::Handshaking(sess) => Some(sess.get_ref().0),
321-
MidHandshake::SendAlert { io, .. } => Some(io),
322-
MidHandshake::Error { io, .. } => Some(io),
323-
MidHandshake::End => None,
324-
}
325-
}
326-
327-
pub fn get_mut(&mut self) -> Option<&mut IO> {
328-
match &mut self.0 {
329-
MidHandshake::Handshaking(sess) => Some(sess.get_mut().0),
330-
MidHandshake::SendAlert { io, .. } => Some(io),
331-
MidHandshake::Error { io, .. } => Some(io),
332-
MidHandshake::End => None,
333-
}
334-
}
335-
}
336-
337305
impl<IO> Accept<IO> {
338306
#[inline]
339307
pub fn into_fallible(self) -> FallibleAccept<IO> {
@@ -359,15 +327,6 @@ impl<IO> Accept<IO> {
359327
}
360328
}
361329

362-
impl<IO: AsyncRead + AsyncWrite + Unpin> Future for Connect<IO> {
363-
type Output = io::Result<client::TlsStream<IO>>;
364-
365-
#[inline]
366-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
367-
Pin::new(&mut self.0).poll(cx).map_err(|(err, _)| err)
368-
}
369-
}
370-
371330
impl<IO: AsyncRead + AsyncWrite + Unpin> Future for Accept<IO> {
372331
type Output = io::Result<server::TlsStream<IO>>;
373332

@@ -377,15 +336,6 @@ impl<IO: AsyncRead + AsyncWrite + Unpin> Future for Accept<IO> {
377336
}
378337
}
379338

380-
impl<IO: AsyncRead + AsyncWrite + Unpin> Future for FallibleConnect<IO> {
381-
type Output = Result<client::TlsStream<IO>, (io::Error, IO)>;
382-
383-
#[inline]
384-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
385-
Pin::new(&mut self.0).poll(cx)
386-
}
387-
}
388-
389339
impl<IO: AsyncRead + AsyncWrite + Unpin> Future for FallibleAccept<IO> {
390340
type Output = Result<server::TlsStream<IO>, (io::Error, IO)>;
391341

0 commit comments

Comments
 (0)