Skip to main content

Runtime

Trait Runtime 

Source
pub trait Runtime:
    Send
    + Sync
    + Debug
    + 'static {
    // Required methods
    fn spawn(&self, future: BoxFuture<'static, ()>) -> Box<dyn JoinHandle>;
    fn now(&self) -> Instant;
    fn sleep_until(&self, deadline: Instant) -> BoxFuture<'static, ()>;
    fn bind_udp(&self, local: SocketAddr) -> Result<Arc<dyn AsyncUdpSocket>>;
    fn bind_udp_multicast(
        &self,
        config: &MulticastConfig,
    ) -> Result<Arc<dyn AsyncUdpSocket>>;
    fn listen_tcp(
        &self,
        local: SocketAddr,
        reuse_address: bool,
    ) -> Result<Arc<dyn AsyncTcpListener>>;
    fn connect_tcp(
        &self,
        local: SocketAddr,
        remote: SocketAddr,
        reuse_address: bool,
    ) -> BoxFuture<'static, Result<Arc<dyn AsyncTcpStream>>>;
    fn resolve_host(
        &self,
        host: &str,
        port: u16,
    ) -> BoxFuture<'static, Result<Vec<SocketAddr>>>;
    fn block_on(&self, future: Pin<Box<dyn Future<Output = ()> + '_>>);

    // Provided methods
    fn resolve_family(
        &self,
        host: &str,
        port: u16,
        ipv6: bool,
    ) -> Option<BoxFuture<'static, Vec<SocketAddr>>> { ... }
    fn resolve_records(
        &self,
        name: &str,
        kind: RecordKind,
    ) -> BoxFuture<'static, Vec<DnsRecord>> { ... }
    fn virtual_clock(&self) -> bool { ... }
}
Expand description

What a driver needs from an async runtime: the primitives that touch a reactor or a clock.

Object safe (&self receivers, no generic methods): drivers hold an Arc<dyn Runtime>. See the module documentation for what is and is not here.

Required Methods§

Source

fn spawn(&self, future: BoxFuture<'static, ()>) -> Box<dyn JoinHandle>

Runs future as a task of its own. Dropping the handle detaches the task (it runs on); JoinHandle::abort cancels it.

Source

fn now(&self) -> Instant

The runtime’s clock. Every time the driver gives the engine comes from here, so a virtual clock drives the engine’s timers too.

Source

fn sleep_until(&self, deadline: Instant) -> BoxFuture<'static, ()>

A future that completes once now has reached deadline (at once when it has already).

Source

fn bind_udp(&self, local: SocketAddr) -> Result<Arc<dyn AsyncUdpSocket>>

A UDP socket bound to local (port 0: any free port).

§Errors

The bind failed: AddrInUse for a port in use, PermissionDenied for one the host refuses (both are skipped inside a port range).

Source

fn bind_udp_multicast( &self, config: &MulticastConfig, ) -> Result<Arc<dyn AsyncUdpSocket>>

A UDP socket joined to a multicast group (mDNS: 224.0.0.251 or ff02::fb on 5353).

§Errors

The bind or the join failed (a host without IPv6, say).

Source

fn listen_tcp( &self, local: SocketAddr, reuse_address: bool, ) -> Result<Arc<dyn AsyncTcpListener>>

A TCP listener on local (backlog 128). reuse_address sets SO_REUSEADDR (and SO_REUSEPORT where the platform has it).

§Errors

As bind_udp.

Source

fn connect_tcp( &self, local: SocketAddr, remote: SocketAddr, reuse_address: bool, ) -> BoxFuture<'static, Result<Arc<dyn AsyncTcpStream>>>

A TCP connection from local (bound first: an ICE-TCP active candidate’s address) to remote, with TCP_NODELAY set. The driver bounds it with its connect timeout.

Source

fn resolve_host( &self, host: &str, port: u16, ) -> BoxFuture<'static, Result<Vec<SocketAddr>>>

The addresses of host with port, both families together (the system resolver asks for AAAA and A at once, RFC 8305 section 3).

Source

fn block_on(&self, future: Pin<Box<dyn Future<Output = ()> + '_>>)

Runs future to completion on the calling thread. The future need not be Send.

Provided Methods§

Source

fn resolve_family( &self, host: &str, port: u16, ipv6: bool, ) -> Option<BoxFuture<'static, Vec<SocketAddr>>>

The addresses of one family of host, when the runtime can ask for AAAA and A apart (RFC 8305 section 3: the engine starts on the first answer). None, the default: it cannot, and the driver uses resolve_host.

Source

fn resolve_records( &self, name: &str, kind: RecordKind, ) -> BoxFuture<'static, Vec<DnsRecord>>

The SRV or NAPTR records of name (RFC 5928, RFC 8489 section 8). The default answers none, and the engine then uses the host and the scheme’s default port.

Source

fn virtual_clock(&self) -> bool

Whether now is a virtual clock (a mock’s) rather than the wall clock. What is kept per process on the wall clock (the mDNS budget, process_mdns_rate_limit) is not shared with a driver on a virtual clock, whose times run elsewhere. false by default.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§