Skip to content

Commit 78e8359

Browse files
committed
Fix and write Documentation
1 parent e71d763 commit 78e8359

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

src/acceptor.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,35 @@ use std::io;
1212
/// The TLS accepting part. The acceptor drives
1313
/// the server side of the TLS handshake process. It works
1414
/// on any asynchronous stream.
15-
///
15+
///
1616
/// It provides a simple interface (`accept`), returning a future
1717
/// that will resolve when the handshake process completed. On
1818
/// success, it will hand you an async `TLSStream`.
19-
///
19+
///
2020
/// ## Example
21-
///
22-
/// ```rust
23-
/// let mut stream = acceptor.accept(stream).await?;
24-
/// ```
21+
///
22+
/// See /examples/server for an example.
2523
#[derive(Clone)]
2624
pub struct TlsAcceptor {
2725
inner: Arc<ServerConfig>,
2826
}
2927

3028
impl TlsAcceptor {
29+
/// Accept a client connections. `stream` can be any type implementing `AsyncRead` and `AsyncWrite`,
30+
/// such as TcpStreams or Unix domain sockets.
31+
///
32+
/// Otherwise, it will return a `Accept` Future, representing the Acceptance part of a
33+
/// Tls handshake. It will resolve when the handshake is over.
34+
#[inline]
3135
pub fn accept<IO>(&self, stream: IO) -> Accept<IO>
3236
where
3337
IO: AsyncRead + AsyncWrite + Unpin,
3438
{
3539
self.accept_with(stream, |_| ())
3640
}
3741

38-
#[inline]
39-
pub fn accept_with<IO, F>(&self, stream: IO, f: F) -> Accept<IO>
42+
// Currently private, as exposing ServerSessions exposes rusttls
43+
fn accept_with<IO, F>(&self, stream: IO, f: F) -> Accept<IO>
4044
where
4145
IO: AsyncRead + AsyncWrite + Unpin,
4246
F: FnOnce(&mut ServerSession),

src/client.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::{io, mem};
99

1010
use rustls::Session;
1111

12-
/// A wrapper around an underlying raw stream which implements the TLS or SSL
13-
/// protocol.
12+
/// The client end of a TLS connection. Can be used like any other bidirectional IO stream.
13+
/// Wraps the underlying TCP stream.
1414
#[derive(Debug)]
1515
pub struct TlsStream<IO> {
1616
pub(crate) io: IO,
@@ -28,6 +28,8 @@ pub(crate) enum MidHandshake<IO> {
2828
End,
2929
}
3030

31+
// TODO unexpose, maybe without Client?
32+
3133
impl<IO> TlsStream<IO> {
3234
#[inline]
3335
pub fn get_ref(&self) -> (&IO, &ClientSession) {

src/connector.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,26 @@ use webpki::DNSNameRef;
1717
///
1818
/// It provides a simple interface (`connect`), returning a future
1919
/// that will resolve when the handshake process completed. On
20-
/// success, it will hand you an async `TLSStream`.
20+
/// success, it will hand you an async `TlsStream`.
2121
///
22+
/// To create a `TlsConnector` with a non-default configuation, create
23+
/// a `rusttls::ClientConfig` and call `.into()` on it.
24+
///
2225
/// ## Example
2326
///
2427
/// ```rust
25-
/// let mut stream = acceptor.accept(stream).await?;
28+
/// #![feature(async_await)]
29+
///
30+
/// use async_tls::TlsConnector;
31+
///
32+
/// async_std::task::block_on(async {
33+
/// let connector = TlsConnector::default();
34+
/// let tcp_stream = async_std::net::TcpStream::connect("example.com").await?;
35+
/// let handshake = connector.connect("example.com", tcp_stream)?;
36+
/// let encrypted_stream = handshake.await?;
37+
///
38+
/// Ok(()) as async_std::io::Result<()>
39+
/// });
2640
/// ```
2741
#[derive(Clone)]
2842
pub struct TlsConnector {
@@ -52,29 +66,39 @@ impl Default for TlsConnector {
5266
}
5367

5468
impl TlsConnector {
69+
/// Create a new TlsConnector with default configuration.
70+
///
71+
/// This is the same as calling `TlsConnector::default()`.
5572
pub fn new() -> Self {
5673
Default::default()
5774
}
5875

5976
/// Enable 0-RTT.
6077
///
61-
/// Note that you want to use 0-RTT.
62-
/// You must set `enable_early_data` to `true` in `ClientConfig`.
78+
/// You must also set `enable_early_data` to `true` in `ClientConfig`.
6379
#[cfg(feature = "early-data")]
6480
pub fn early_data(mut self, flag: bool) -> TlsConnector {
6581
self.early_data = flag;
6682
self
6783
}
6884

85+
/// Connect to a server. `stream` can be any type implementing `AsyncRead` and `AsyncWrite`,
86+
/// such as TcpStreams or Unix domain sockets.
87+
///
88+
/// The function will return an error if the domain is not of valid format.
89+
/// Otherwise, it will return a `Connect` Future, representing the connecting part of a
90+
/// Tls handshake. It will resolve when the handshake is over.
91+
#[inline]
6992
pub fn connect<'a, IO>(&self, domain: impl AsRef<str>, stream: IO) -> io::Result<Connect<IO>>
7093
where
7194
IO: AsyncRead + AsyncWrite + Unpin,
7295
{
7396
self.connect_with(domain, stream, |_| ())
7497
}
7598

76-
#[inline]
77-
pub fn connect_with<'a, IO, F>(
99+
// NOTE: Currently private, exposing ClientSession exposes rusttls
100+
// Early data should be exposed differently
101+
fn connect_with<'a, IO, F>(
78102
&self,
79103
domain: impl AsRef<str>,
80104
stream: IO,

src/server.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::{io, mem};
1010

1111
use rustls::Session;
1212

13-
/// A wrapper around an underlying raw stream which implements the TLS or SSL
14-
/// protocol.
13+
/// The server end of a TLS connection. Can be used like any other bidirectional IO stream.
14+
/// Wraps the underlying TCP stream.
1515
#[derive(Debug)]
1616
pub struct TlsStream<IO> {
1717
pub(crate) io: IO,
@@ -24,6 +24,7 @@ pub(crate) enum MidHandshake<IO> {
2424
End,
2525
}
2626

27+
// TODO unexpose, maybe without ServerSession?
2728
impl<IO> TlsStream<IO> {
2829
#[inline]
2930
pub fn get_ref(&self) -> (&IO, &ServerSession) {
@@ -53,7 +54,7 @@ where
5354

5455
if let MidHandshake::Handshaking(stream) = this {
5556
let eof = !stream.state.readable();
56-
let (io, session) = stream.get_mut();
57+
let (io, session) = (&mut stream.io, &mut stream.session);
5758
let mut stream = Stream::new(io, session).set_eof(eof);
5859

5960
if stream.session.is_handshaking() {

0 commit comments

Comments
 (0)