Skip to main content

Listener

Trait Listener 

Source
pub trait Listener: Send {
    type Acceptor: Acceptor;

    // Required method
    fn try_bind(
        self,
    ) -> impl Future<Output = Result<Self::Acceptor, Error>> + Send;

    // Provided methods
    fn bind(self) -> impl Future<Output = Self::Acceptor> + Send
       where Self: Sized + Send + 'static { ... }
    fn join<T>(self, other: T) -> JoinedListener<Self, T>
       where Self: Sized + Send { ... }
}
Expand description

A trait for types that can bind to an address and create an acceptor.

Listeners are the starting point for accepting connections. They encapsulate the address binding logic and produce an Acceptor that can accept connections.

§Basic Usage

use salvo_core::conn::{Listener, TcpListener};

let acceptor = TcpListener::new("127.0.0.1:8080").bind().await;

§Error Handling

Use try_bind() instead of bind() when you need to handle binding errors gracefully:

use salvo_core::conn::{Listener, TcpListener};

let acceptor = TcpListener::new("127.0.0.1:8080").try_bind().await?;

§Combining Listeners

Multiple listeners can be combined using the join() method:

let combined = TcpListener::new("0.0.0.0:80")
    .join(TcpListener::new("0.0.0.0:443"));

Required Associated Types§

Source

type Acceptor: Acceptor

The type of acceptor this listener produces.

Required Methods§

Source

fn try_bind(self) -> impl Future<Output = Result<Self::Acceptor, Error>> + Send

Attempts to bind to the configured address.

§Errors

Returns an error if the address cannot be bound (e.g., already in use, permission denied, or invalid address).

Provided Methods§

Source

fn bind(self) -> impl Future<Output = Self::Acceptor> + Send
where Self: Sized + Send + 'static,

Binds to the configured address and returns an acceptor.

§Panics

Panics if binding fails. Use try_bind() for fallible binding.

Source

fn join<T>(self, other: T) -> JoinedListener<Self, T>
where Self: Sized + Send,

Joins this listener with another, creating a combined listener.

The resulting JoinedListener will accept connections from both listeners simultaneously.

§Example
let listener = http_listener.join(https_listener);

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl<A, B> Listener for JoinedListener<A, B>
where A: Listener + Send + Unpin + 'static, B: Listener + Send + Unpin + 'static, <A as Listener>::Acceptor: Acceptor + Send + Unpin + 'static, <B as Listener>::Acceptor: Acceptor + Send + Unpin + 'static,

Source§

impl<T> Listener for TcpListener<T>
where T: ToSocketAddrs + Send + 'static,