Skip to main content

base_scheduler/
socket_options.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use super::{SocketGetOption, SocketSetOption};
5use crate::SocketOption;
6
7/// Convenience aggregate of `SocketGetOption<O>` and `SocketSetOption<O>`, automatically implemented for schedulers providing both operations for an option. Generic consumers require only the options they use, such as `S: SocketOptions<TcpNoDelay>`.
8///
9/// An absent implementation is a compile-time error. Socket type, protocol, state, and OS policy can still reject an operation at runtime; preserve those errors. Options retain their OS semantics, including address reuse and inheritance on accepted sockets. Read-only and write-only controls implement only their respective operation trait. Differently shaped controls can use separate extension traits.
10///
11/// Both futures are lazy and check cancellation before the syscall. Once executed, the syscall's result wins concurrent cancellation; dropping an unpolled setter changes nothing. Await setters before operations that depend on them. There is no extra serialization between concurrent option operations, and get-followed-by-set is not atomic.
12///
13/// Option values are checked at compile time:
14/// ```compile_fail
15/// use base_scheduler::{Cancellation, SocketOptions, TcpNoDelay};
16/// async fn configure<S: SocketOptions<TcpNoDelay>>(scheduler: &S, socket: &S::Socket) {
17///     scheduler.socket_set_option(socket, TcpNoDelay, 1_u32, &Cancellation::never()).await;
18/// }
19/// ```
20/// Merely supporting sockets does not imply support for every option:
21/// ```compile_fail
22/// use base_scheduler::{Cancellation, SocketOperation, SocketSetOption, TcpNoDelay};
23/// async fn configure<S: SocketOperation>(scheduler: &S, socket: &S::Socket) {
24///     scheduler.socket_set_option(socket, TcpNoDelay, true, &Cancellation::never()).await;
25/// }
26/// ```
27pub trait SocketOptions<O: SocketOption>: SocketGetOption<O> + SocketSetOption<O> {}
28
29impl<S: ?Sized, O: SocketOption> SocketOptions<O> for S where
30    S: SocketGetOption<O> + SocketSetOption<O>
31{
32}