Skip to main content

Connector

Trait Connector 

Source
pub trait Connector:
    Send
    + Sync
    + 'static {
    // Required method
    fn connect(&self, host: &str, port: u16) -> ConnectFuture;
}
Expand description

A source of outbound TCP connections in place of the operating system.

An embedder implements this trait when it supplies the sockets itself, for example a WASI host that connects sockets on a virtual network. install_connector makes a connector the source of every connection that dial opens, for the life of the process.

§Examples

use krabka_client_core::transport::{ConnectFuture, Connector, install_connector};
use tokio::net::TcpStream;

/// Connects every broker address to the same port on this machine.
struct Loopback;

impl Connector for Loopback {
    fn connect(&self, _host: &str, port: u16) -> ConnectFuture {
        Box::pin(TcpStream::connect(("127.0.0.1", port)))
    }
}

install_connector(Box::new(Loopback)).expect("no other connector in this process");

Required Methods§

Source

fn connect(&self, host: &str, port: u16) -> ConnectFuture

Start a TCP connection to host at port, and return the future of the connection.

host is the text that the caller dials: a host name or an IP address literal, with no resolution. The future owns the data that it needs, so it can outlive host.

An implementation must not block the thread, in this call or in a poll of the future. dial awaits the future, so the timeouts of its callers stop a connection that does not open, and other tasks on the same runtime continue. The stream can still be connecting when the future completes. A refused connection then shows as an error on the first read or write.

§Errors

The future fails with the error that stops the connection, for example an unknown host. dial gives this error to its caller.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§