monoio_transports::connectors

Trait Connector

Source
pub trait Connector<K> {
    type Connection;
    type Error;

    // Required method
    fn connect(
        &self,
        key: K,
    ) -> impl Future<Output = Result<Self::Connection, Self::Error>>;
}
Expand description

The Connector trait defines an interface for establishing connections. This trait is designed to be composable, allowing for the creation of modular and stackable connectors. Each connector in the stack can add its own layer of functionality, such as TCP connection, Unix Domain Socket (UDS) connection, TLS encryption, or HTTP protocol handling.

§Type Parameters

  • K: The key type used to identify the connection target.

§Associated Types

  • Connection: The type of the established connection.
  • Error: The error type that may occur during connection.

§Examples

Here are examples of how to stack different connectors:

§HTTP over Unix Domain Socket

use http::request;
use monoio_http::{common::body::HttpBody, h1::payload::Payload};
use monoio_transports::{
    connectors::{Connector, UnixConnector},
    http::HttpConnector,
};

#[monoio::main]
async fn main() -> Result<(), monoio_transports::TransportError> {
    let connector: HttpConnector<UnixConnector, _, _> = HttpConnector::default();
    let mut conn = connector.connect("./examples/uds.sock").await?;

    let req = request::Builder::new()
        .uri("/get")
        .header("Host", "test")
        .body(HttpBody::H1(Payload::None))
        .unwrap();

    let (res, _) = conn.send_request(req).await;
    let resp = res?;
    assert_eq!(200, resp.status());

    Ok(())
}

§HTTPS over TCP

use monoio_transports::{
    connectors::{Connector, TcpConnector, TcpTlsAddr, TlsConnector},
    http::HttpConnector,
};

#[monoio::main]
async fn main() -> Result<(), monoio_transports::TransportError> {
    let connector: HttpConnector<TlsConnector<TcpConnector>, _, _> =
        HttpConnector::build_tls_http2_only();

    let addr: TcpTlsAddr = "https://example.com".try_into()?;
    let conn = connector.connect(addr).await?;

    // Use the connection...

    Ok(())
}

These examples demonstrate how connectors can be stacked to create different connection types (HTTP over UDS, HTTPS over TCP) using the same Connector trait.

Required Associated Types§

Required Methods§

Source

fn connect( &self, key: K, ) -> impl Future<Output = Result<Self::Connection, Self::Error>>

Attempts to establish a connection.

§Parameters
  • key: The key identifying the connection target.
§Returns

A Future that resolves to a Result containing either the established connection or an error.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a> Connector<&'a UnifiedAddr> for UnifiedConnector

Source§

impl<'a> Connector<&'a UnifiedTlsAddr> for UnifiedConnector

Source§

impl<C, K> Connector<K> for ReuseConnector<C>
where C: Connector<K>,

Source§

impl<C, K: Key, IO> Connector<K> for H1Connector<C, K, IO>
where C: Connector<K, Connection = IO>, IO: AsyncReadRent + AsyncWriteRent + Split,

Source§

type Connection = Pooled<K, Http1Connection<IO>>

Source§

type Error = <C as Connector<K>>::Error

Source§

impl<C, K: Key, IO> Connector<K> for HttpConnector<C, K, IO>
where C: Connector<K, Connection = IO>, C::Connection: TransportConnMetadata<Metadata = TransportConnMeta>, TransportError: From<C::Error>, IO: AsyncReadRent + AsyncWriteRent + Split + Unpin + 'static,

Source§

impl<C, K: Key, T: Poolable> Connector<K> for PooledConnector<C, K, T>
where C: Connector<K, Connection = T>,

Source§

type Connection = Pooled<K, T>

Source§

type Error = <C as Connector<K>>::Error

Source§

impl<C, T, CN> Connector<T> for TlsConnector<C>
where T: AsRef<ServerName<'static>>, for<'a> C: Connector<&'a T, Error = Error, Connection = CN>, CN: AsyncReadRent + AsyncWriteRent,

Source§

impl<K, C, M> Connector<K> for ConnectorMap<C, M>
where C: Connector<K>, M: ConnectorMapper<C::Connection, C::Error>,

Source§

impl<P: AsRef<Path>> Connector<P> for UnixConnector

Source§

impl<T: AsRef<UnifiedL4Addr>> Connector<T> for UnifiedL4Connector

Source§

impl<T: ToSocketAddrs> Connector<T> for TcpConnector