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§
Required Methods§
Provided Methods§
Sourcefn bind(self) -> impl Future<Output = Self::Acceptor> + Send
fn bind(self) -> impl Future<Output = Self::Acceptor> + Send
Binds to the configured address and returns an acceptor.
§Panics
Panics if binding fails. Use try_bind() for
fallible binding.
Sourcefn join<T>(self, other: T) -> JoinedListener<Self, T>
fn join<T>(self, other: T) -> JoinedListener<Self, T>
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".