diff --git a/bindings/rust/s2n-tls-hyper/src/connector.rs b/bindings/rust/s2n-tls-hyper/src/connector.rs index c7ffe3fc05d..ee3004d8eba 100644 --- a/bindings/rust/s2n-tls-hyper/src/connector.rs +++ b/bindings/rust/s2n-tls-hyper/src/connector.rs @@ -10,6 +10,7 @@ use hyper_util::{ }; use s2n_tls::{config::Config, connection}; use s2n_tls_tokio::TlsConnector; +use std::marker::PhantomData; use std::{ future::Future, pin::Pin, @@ -34,10 +35,25 @@ where ConnBuilder: connection::Builder, ::Output: Unpin, { - /// Creates a new `HttpsConnector` with the default configuration. See `Builder::build` for - /// details. + /// Creates a new `HttpsConnector` with the default configuration. Use `HttpsConnector::builder` + /// instead to configure an `HttpsConnector`. + /// + /// `conn_builder` will be used to produce the s2n-tls Connections used for negotiating HTTPS, + /// which can be an `s2n_tls::config::Config` or other `s2n_tls::connection::Builder`. + /// + /// ``` + /// use s2n_tls_hyper::connector::HttpsConnector; + /// use s2n_tls::config::Config; + /// + /// // Create a new HttpsConnector. + /// let connector = HttpsConnector::new(Config::default()); + /// ``` + /// + /// Note that s2n-tls-hyper will override the ALPN extension to negotiate HTTP. Any ALPN values + /// configured on `conn_builder` with APIs like + /// `s2n_tls::config::Builder::set_application_protocol_preference()` will be ignored. pub fn new(conn_builder: ConnBuilder) -> HttpsConnector { - HttpsConnector::::builder().build(conn_builder) + HttpsConnector::builder().build(conn_builder) } } @@ -58,16 +74,22 @@ where /// // Build a new HttpsConnector. /// let connector = builder.build(Config::default()); /// ``` - pub fn builder() -> Builder { - Builder {} + pub fn builder() -> Builder { + Builder { + http: PhantomData, + conn_builder: PhantomData, + } } } /// Builder used to configure an `HttpsConnector`. Create a new Builder with /// `HttpsConnector::builder`. -pub struct Builder; +pub struct Builder { + http: PhantomData, + conn_builder: PhantomData, +} -impl Builder { +impl Builder { /// Builds a new `HttpsConnector`. /// /// `conn_builder` will be used to produce the s2n-tls Connections used for negotiating HTTPS, @@ -79,13 +101,7 @@ impl Builder { /// Note that s2n-tls-hyper will override the ALPN extension to negotiate HTTP. Any ALPN values /// configured on `conn_builder` with APIs like /// `s2n_tls::config::Builder::set_application_protocol_preference()` will be ignored. - pub fn build( - self, - conn_builder: ConnBuilder, - ) -> HttpsConnector - where - ConnBuilder: connection::Builder, - { + pub fn build(self, conn_builder: ConnBuilder) -> HttpsConnector { let mut http = HttpConnector::new(); // By default, the `HttpConnector` only allows the HTTP URI scheme to be used. To negotiate @@ -94,7 +110,9 @@ impl Builder { self.build_with_http(http, conn_builder) } +} +impl Builder { /// Builds a new `HttpsConnector`. /// /// `conn_builder` will be used to produce the s2n-tls Connections used for negotiating HTTPS, @@ -116,19 +134,16 @@ impl Builder { /// .build_with_http(http, Config::default()); /// ``` /// - /// `HttpsConnector::new()` can be used to create the HTTP connector automatically. + /// `Builder::build()` can be used to create the HTTP connector automatically. /// /// Note that s2n-tls-hyper will override the ALPN extension to negotiate HTTP. Any ALPN values /// configured on `conn_builder` with APIs like /// `s2n_tls::config::Builder::set_application_protocol_preference()` will be ignored. - pub fn build_with_http( + pub fn build_with_http( self, http: Http, conn_builder: ConnBuilder, - ) -> HttpsConnector - where - ConnBuilder: connection::Builder, - { + ) -> HttpsConnector { HttpsConnector { http, conn_builder } } } @@ -226,6 +241,21 @@ mod tests { use hyper_util::{client::legacy::Client, rt::TokioExecutor}; use std::{error::Error as StdError, str::FromStr}; + #[tokio::test] + async fn connector_creation() { + let config = Config::default(); + let connector_from_new = HttpsConnector::new(config.clone()); + let _assert_type: HttpsConnector = connector_from_new; + + let connector_from_build = HttpsConnector::builder().build(config.clone()); + let _assert_type: HttpsConnector = connector_from_build; + + let http: u32 = 10; + let connect_from_build_with_http = + HttpsConnector::builder().build_with_http(http, config.clone()); + let _assert_type: HttpsConnector = connect_from_build_with_http; + } + #[tokio::test] async fn test_unsecure_http() -> Result<(), Box> { let connector = HttpsConnector::new(Config::default());