base_endpoint/bind_endpoint.rs
1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use base_scheduler::{Cancellation, OperationResult};
5use std::future::Future;
6
7/// Listening configuration passed to a binding operation.
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub struct BindOptions {
10 /// Requested listen backlog. Operating-system limits and semantics apply.
11 pub backlog: i32,
12}
13
14impl Default for BindOptions {
15 fn default() -> Self {
16 Self { backlog: 128 }
17 }
18}
19
20/// A reusable description of how to create listeners. Consumers need not know how addresses are resolved or sockets are created. Binding may produce multiple listeners, for example one per IP family.
21pub trait BindEndpoint<S: ?Sized>: Send + Sync {
22 /// One bound listener, owned by the caller on success. Its representation belongs to the endpoint implementation.
23 type Listener: Send;
24
25 /// Bind and start listening on every address. Success returns a nonempty collection; failure transfers no listeners to the caller. Futures are lazy. Await completion after cooperative cancellation to finish asynchronous rollback; dropping a pending future provides ordinary destruction only.
26 fn bind(
27 &self,
28 scheduler: &S,
29 options: BindOptions,
30 cancellation: &Cancellation,
31 ) -> impl Future<Output = OperationResult<Vec<Self::Listener>>> + Send;
32}