Skip to content

Commit

Permalink
Document tokio-postgres-native-tls
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Mar 7, 2019
1 parent 374fadb commit e12902a
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion tokio-postgres-native-tls/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
#![warn(rust_2018_idioms, clippy::all)]
//! TLS support for `tokio-postgres` via `native-tls.
//!
//! # Example
//!
//! ```no_run
//! use native_tls::{Certificate, TlsConnector};
//! use tokio_postgres_native_tls::MakeTlsConnector;
//! use std::fs;
//!
//! let cert = fs::read("database_cert.pem").unwrap();
//! let cert = Certificate::from_pem(&cert).unwrap();
//! let connector = TlsConnector::builder()
//! .add_root_certificate(cert)
//! .build()
//! .unwrap();
//! let connector = MakeTlsConnector::new(connector);
//!
//! let connect_future = tokio_postgres::connect(
//! "host=localhost user=postgres sslmode=require",
//! connector,
//! );
//!
//! // ...
//! ```
#![warn(rust_2018_idioms, clippy::all, missing_docs)]

use futures::{try_ready, Async, Future, Poll};
use tokio_io::{AsyncRead, AsyncWrite};
Expand All @@ -10,12 +35,16 @@ use tokio_tls::{Connect, TlsStream};
#[cfg(test)]
mod test;

/// A `MakeTlsConnect` implementation using the `native-tls` crate.
///
/// Requires the `runtime` Cargo feature (enabled by default).
#[cfg(feature = "runtime")]
#[derive(Clone)]
pub struct MakeTlsConnector(native_tls::TlsConnector);

#[cfg(feature = "runtime")]
impl MakeTlsConnector {
/// Creates a new connector.
pub fn new(connector: native_tls::TlsConnector) -> MakeTlsConnector {
MakeTlsConnector(connector)
}
Expand All @@ -35,12 +64,14 @@ where
}
}

/// A `TlsConnect` implementation using the `native-tls` crate.
pub struct TlsConnector {
connector: tokio_tls::TlsConnector,
domain: String,
}

impl TlsConnector {
/// Creates a new connector configured to connect to the specified domain.
pub fn new(connector: native_tls::TlsConnector, domain: &str) -> TlsConnector {
TlsConnector {
connector: tokio_tls::TlsConnector::from(connector),
Expand All @@ -62,6 +93,7 @@ where
}
}

/// The future returned by `TlsConnector`.
pub struct TlsConnectFuture<S>(Connect<S>);

impl<S> Future for TlsConnectFuture<S>
Expand Down

0 comments on commit e12902a

Please sign in to comment.