Skip to content

Commit db6dde8

Browse files
committed
Document tokio-postgres-openssl
1 parent 4f084e7 commit db6dde8

File tree

1 file changed

+35
-5
lines changed
  • tokio-postgres-openssl/src

1 file changed

+35
-5
lines changed

tokio-postgres-openssl/src/lib.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
#![warn(rust_2018_idioms, clippy::all)]
1+
//! TLS support for `tokio-postgres` via `openssl`.
2+
//!
3+
//! # Example
4+
//!
5+
//! ```no_run
6+
//! use openssl::ssl::{SslConnector, SslMethod};
7+
//! use tokio_postgres_openssl::MakeTlsConnector;
8+
//!
9+
//! let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
10+
//! builder.set_ca_file("database_cert.pem").unwrap();
11+
//! let connector = MakeTlsConnector::new(builder.build());
12+
//!
13+
//! let connect_future = tokio_postgres::connect(
14+
//! "host=localhost user=postgres sslmode=require",
15+
//! connector,
16+
//! );
17+
//!
18+
//! // ...
19+
//! ```
20+
21+
#![warn(rust_2018_idioms, clippy::all, missing_docs)]
222

323
use futures::{try_ready, Async, Future, Poll};
424
#[cfg(feature = "runtime")]
@@ -20,25 +40,32 @@ use tokio_postgres::tls::{ChannelBinding, TlsConnect};
2040
#[cfg(test)]
2141
mod test;
2242

43+
/// A `MakeTlsConnect` implementation using the `openssl` crate.
44+
///
45+
/// Requires the `runtime` Cargo feature (enabled by default).
2346
#[cfg(feature = "runtime")]
2447
#[derive(Clone)]
2548
pub struct MakeTlsConnector {
2649
connector: SslConnector,
27-
config: Arc<dyn Fn(&mut ConnectConfiguration) -> Result<(), ErrorStack> + Sync + Send>,
50+
config: Arc<dyn Fn(&mut ConnectConfiguration, &str) -> Result<(), ErrorStack> + Sync + Send>,
2851
}
2952

3053
#[cfg(feature = "runtime")]
3154
impl MakeTlsConnector {
55+
/// Creates a new connector.
3256
pub fn new(connector: SslConnector) -> MakeTlsConnector {
3357
MakeTlsConnector {
3458
connector,
35-
config: Arc::new(|_| Ok(())),
59+
config: Arc::new(|_, _| Ok(())),
3660
}
3761
}
3862

63+
/// Sets a callback used to apply per-connection configuration.
64+
///
65+
/// The the callback is provided the domain name along with the `ConnectConfiguration`.
3966
pub fn set_callback<F>(&mut self, f: F)
4067
where
41-
F: Fn(&mut ConnectConfiguration) -> Result<(), ErrorStack> + 'static + Sync + Send,
68+
F: Fn(&mut ConnectConfiguration, &str) -> Result<(), ErrorStack> + 'static + Sync + Send,
4269
{
4370
self.config = Arc::new(f);
4471
}
@@ -55,17 +82,19 @@ where
5582

5683
fn make_tls_connect(&mut self, domain: &str) -> Result<TlsConnector, ErrorStack> {
5784
let mut ssl = self.connector.configure()?;
58-
(self.config)(&mut ssl)?;
85+
(self.config)(&mut ssl, domain)?;
5986
Ok(TlsConnector::new(ssl, domain))
6087
}
6188
}
6289

90+
/// A `TlsConnect` implementation using the `openssl` crate.
6391
pub struct TlsConnector {
6492
ssl: ConnectConfiguration,
6593
domain: String,
6694
}
6795

6896
impl TlsConnector {
97+
/// Creates a new connector configured to connect to the specified domain.
6998
pub fn new(ssl: ConnectConfiguration, domain: &str) -> TlsConnector {
7099
TlsConnector {
71100
ssl,
@@ -87,6 +116,7 @@ where
87116
}
88117
}
89118

119+
/// The future returned by `TlsConnector`.
90120
pub struct TlsConnectFuture<S>(ConnectAsync<S>);
91121

92122
impl<S> Future for TlsConnectFuture<S>

0 commit comments

Comments
 (0)