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§
type Connection
type Error
Required Methods§
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.