Skip to main content

base_scheduler/
socket_get_option.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use super::SocketOperation;
5use crate::{Cancellation, OperationResult, SocketOption};
6use std::future::Future;
7
8/// Read a typed socket option. See [`SocketOptions`](crate::SocketOptions) for the shared option contract; the opposite operation is not required.
9///
10/// A getter-only adapter needs neither a setter nor any core socket operations:
11///
12/// ```
13/// use base_scheduler::{Cancellation, OperationResult, SocketGetOption, SocketOperation, SocketOption};
14/// struct Inspector<S>(S);
15/// impl<S: SocketOperation> SocketOperation for Inspector<S> {
16///     type Socket = S::Socket;
17/// }
18/// impl<S: SocketGetOption<O>, O: SocketOption> SocketGetOption<O> for Inspector<S> {
19///     async fn socket_get_option(&self, socket: &Self::Socket, option: O, cancellation: &Cancellation) -> OperationResult<O::Value> {
20///         self.0.socket_get_option(socket, option, cancellation).await
21///     }
22/// }
23/// ```
24///
25/// Reading an option does not grant permission to set it:
26///
27/// ```compile_fail,E0599
28/// use base_scheduler::{Cancellation, SocketGetOption, SocketSetOption, TcpNoDelay};
29/// async fn configure<S: SocketGetOption<TcpNoDelay>>(scheduler: &S, socket: &S::Socket) {
30///     scheduler.socket_set_option(socket, TcpNoDelay, true, &Cancellation::never()).await;
31/// }
32/// ```
33pub trait SocketGetOption<O: SocketOption>: SocketOperation {
34    /// Query the option's current value, preserving OS errors.
35    fn socket_get_option(
36        &self,
37        socket: &Self::Socket,
38        option: O,
39        cancellation: &Cancellation,
40    ) -> impl Future<Output = OperationResult<O::Value>> + Send;
41}