1
//! Definitions for types used with [`NetStreamProvider`](crate::NetStreamProvider).
2

            
3
/// Options to use when initializing a listening socket.
4
///
5
/// This may include both options that affect the listening,
6
/// and options that will apply to any individual accepted connection streams.
7
///
8
/// It can include options set with `setsockopt`,
9
/// as well as options that influence higher layers (eg, the runtime).
10
///
11
/// For established streams that are accepted from a listener,
12
/// you can use [`StreamOps`](crate::StreamOps) to perform additional operations
13
/// or to configure additional options.
14
#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
15
#[non_exhaustive]
16
pub struct CommonListenOptions {
17
    /// Value set for `SO_SNDBUF` on the listening socket.
18
    #[builder(default)]
19
    pub(crate) send_buffer_size: Option<usize>,
20

            
21
    /// Value set for `SO_RCVBUF` on the listening socket.
22
    #[builder(default)]
23
    pub(crate) recv_buffer_size: Option<usize>,
24
}
25

            
26
impl CommonListenOptions {
27
    /// Returns a builder for this [`CommonListenOptions`].
28
2
    pub fn builder() -> CommonListenOptionsBuilder {
29
2
        Default::default()
30
2
    }
31
}
32

            
33
// We want to make sure that the defaults are set to the defaults that the builder uses.
34
#[allow(clippy::derivable_impls)]
35
impl Default for CommonListenOptions {
36
871
    fn default() -> Self {
37
        // This needs to match the result of `Self::builder().build().unwrap()`,
38
        // which is tested by the `builder_defaults()` test below.
39
871
        Self {
40
871
            send_buffer_size: None,
41
871
            recv_buffer_size: None,
42
871
        }
43
871
    }
44
}
45

            
46
/// Options to use when initializing a TCP listening socket.
47
///
48
/// See [`CommonListenOptions`] for more information.
49
#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
50
#[non_exhaustive]
51
pub struct TcpListenOptions {
52
    /// Options that are common for all socket types.
53
    #[builder(sub_builder)]
54
    pub(crate) common: CommonListenOptions,
55
}
56

            
57
impl TcpListenOptions {
58
    /// Returns a builder for this [`TcpListenOptions`].
59
2
    pub fn builder() -> TcpListenOptionsBuilder {
60
2
        Default::default()
61
2
    }
62
}
63

            
64
// We want to make sure that the defaults are set to the defaults that the builder uses.
65
#[allow(clippy::derivable_impls)]
66
impl Default for TcpListenOptions {
67
869
    fn default() -> Self {
68
        // This needs to match the result of `Self::builder().build().unwrap()`,
69
        // which is tested by the `builder_defaults()` test below.
70
869
        Self {
71
869
            common: CommonListenOptions::default(),
72
869
        }
73
869
    }
74
}
75

            
76
/// Options to use when initializing a unix stream listening socket.
77
// TODO: We should support at least the options in `CommonListenOptions`.
78
#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
79
#[non_exhaustive]
80
pub struct UnixListenOptions {}
81

            
82
impl UnixListenOptions {
83
    /// Returns a builder for this [`UnixListenOptions`].
84
2
    pub fn builder() -> UnixListenOptionsBuilder {
85
2
        Default::default()
86
2
    }
87
}
88

            
89
// We want to make sure that the defaults are set to the defaults that the builder uses.
90
#[allow(clippy::derivable_impls)]
91
impl Default for UnixListenOptions {
92
2
    fn default() -> Self {
93
        // This needs to match the result of `Self::builder().build().unwrap()`,
94
        // which is tested by the `builder_defaults()` test below.
95
2
        Self {}
96
2
    }
97
}
98

            
99
/// Options to use when connecting a socket.
100
///
101
/// This may include both options that affect the connection attempt,
102
/// and options that will apply to the resulting connection stream.
103
///
104
/// It can include options set with `setsockopt`,
105
/// as well as options that influence higher layers (eg, the runtime).
106
///
107
/// For established streams,
108
/// you can use [`StreamOps`](crate::StreamOps) to perform additional operations
109
/// or to configure additional options.
110
#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
111
#[non_exhaustive]
112
pub struct CommonConnectOptions {
113
    /// Value set for `SO_SNDBUF` on the socket.
114
    #[builder(default)]
115
    pub(crate) send_buffer_size: Option<usize>,
116

            
117
    /// Value set for `SO_RCVBUF` on the socket.
118
    #[builder(default)]
119
    pub(crate) recv_buffer_size: Option<usize>,
120
}
121

            
122
impl CommonConnectOptions {
123
    /// Returns a builder for this [`CommonConnectOptions`].
124
2
    pub fn builder() -> CommonConnectOptionsBuilder {
125
2
        Default::default()
126
2
    }
127
}
128

            
129
// We want to make sure that the defaults are set to the defaults that the builder uses.
130
#[allow(clippy::derivable_impls)]
131
impl Default for CommonConnectOptions {
132
3290
    fn default() -> Self {
133
        // This needs to match the result of `Self::builder().build().unwrap()`,
134
        // which is tested by the `builder_defaults()` test below.
135
3290
        Self {
136
3290
            send_buffer_size: None,
137
3290
            recv_buffer_size: None,
138
3290
        }
139
3290
    }
140
}
141

            
142
/// Options to use when connecting a TCP socket.
143
///
144
/// See [`CommonConnectOptions`] for more information.
145
#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
146
#[non_exhaustive]
147
pub struct TcpConnectOptions {
148
    /// Options that are common for all socket types.
149
    #[builder(sub_builder)]
150
    pub(crate) common: CommonConnectOptions,
151
}
152

            
153
impl TcpConnectOptions {
154
    /// Returns a builder for this [`TcpConnectOptions`].
155
2
    pub fn builder() -> TcpConnectOptionsBuilder {
156
2
        Default::default()
157
2
    }
158
}
159

            
160
// We want to make sure that the defaults are set to the defaults that the builder uses.
161
#[allow(clippy::derivable_impls)]
162
impl Default for TcpConnectOptions {
163
3288
    fn default() -> Self {
164
        // This needs to match the result of `Self::builder().build().unwrap()`,
165
        // which is tested by the `builder_defaults()` test below.
166
3288
        Self {
167
3288
            common: CommonConnectOptions::default(),
168
3288
        }
169
3288
    }
170
}
171

            
172
/// Options to use when connecting a unix stream socket.
173
// TODO: We should support at least the options in `CommonConnectOptions`.
174
#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
175
#[non_exhaustive]
176
pub struct UnixConnectOptions {}
177

            
178
impl UnixConnectOptions {
179
    /// Returns a builder for this [`UnixConnectOptions`].
180
2
    pub fn builder() -> UnixConnectOptionsBuilder {
181
2
        Default::default()
182
2
    }
183
}
184

            
185
// We want to make sure that the defaults are set to the defaults that the builder uses.
186
#[allow(clippy::derivable_impls)]
187
impl Default for UnixConnectOptions {
188
2
    fn default() -> Self {
189
        // This needs to match the result of `Self::builder().build().unwrap()`,
190
        // which is tested by the `builder_defaults()` test below.
191
2
        Self {}
192
2
    }
193
}
194

            
195
#[cfg(test)]
196
mod test {
197
    // @@ begin test lint list maintained by maint/add_warning @@
198
    #![allow(clippy::bool_assert_comparison)]
199
    #![allow(clippy::clone_on_copy)]
200
    #![allow(clippy::dbg_macro)]
201
    #![allow(clippy::mixed_attributes_style)]
202
    #![allow(clippy::print_stderr)]
203
    #![allow(clippy::print_stdout)]
204
    #![allow(clippy::single_char_pattern)]
205
    #![allow(clippy::unwrap_used)]
206
    #![allow(clippy::unchecked_time_subtraction)]
207
    #![allow(clippy::useless_vec)]
208
    #![allow(clippy::needless_pass_by_value)]
209
    #![allow(clippy::string_slice)] // See arti#2571
210
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
211

            
212
    use super::*;
213

            
214
    #[test]
215
    fn builder_defaults() {
216
        // Ensure that the builder default matches the type's default.
217
        macro_rules! check {
218
            ($type:tt) => {
219
                assert_eq!($type::builder().build().unwrap(), $type::default());
220
            };
221
        }
222

            
223
        check!(CommonListenOptions);
224
        check!(TcpListenOptions);
225
        check!(UnixListenOptions);
226

            
227
        check!(CommonConnectOptions);
228
        check!(TcpConnectOptions);
229
        check!(UnixConnectOptions);
230
    }
231
}