@@ -48,15 +48,36 @@ pub struct UdpSocket(net_imp::UdpSocket);
48
48
impl UdpSocket {
49
49
/// Creates a UDP socket from the given address.
50
50
///
51
- /// The address type can be any implementor of `ToSocketAddr` trait. See
51
+ /// The address type can be any implementor of [`ToSocketAddrs`] trait. See
52
52
/// its documentation for concrete examples.
53
+ ///
54
+ /// [`ToSocketAddrs`]: ../../std/net/trait.ToSocketAddrs.html
55
+ ///
56
+ /// # Examples
57
+ ///
58
+ /// ```no_run
59
+ /// use std::net::UdpSocket;
60
+ ///
61
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
62
+ /// ```
53
63
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
54
64
pub fn bind < A : ToSocketAddrs > ( addr : A ) -> io:: Result < UdpSocket > {
55
65
super :: each_addr ( addr, net_imp:: UdpSocket :: bind) . map ( UdpSocket )
56
66
}
57
67
58
68
/// Receives data from the socket. On success, returns the number of bytes
59
69
/// read and the address from whence the data came.
70
+ ///
71
+ /// # Examples
72
+ ///
73
+ /// ```no_run
74
+ /// use std::net::UdpSocket;
75
+ ///
76
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
77
+ /// let mut buf = [0; 10];
78
+ /// let (number_of_bytes, src_addr) = socket.recv_from(&mut buf)
79
+ /// .expect("Didn't receive data");
80
+ /// ```
60
81
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
61
82
pub fn recv_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
62
83
self . 0 . recv_from ( buf)
@@ -65,11 +86,24 @@ impl UdpSocket {
65
86
/// Sends data on the socket to the given address. On success, returns the
66
87
/// number of bytes written.
67
88
///
68
- /// Address type can be any implementor of `ToSocketAddrs` trait. See its
89
+ /// Address type can be any implementor of [ `ToSocketAddrs`] trait. See its
69
90
/// documentation for concrete examples.
91
+ ///
70
92
/// This will return an error when the IP version of the local socket
71
- /// does not match that returned from `ToSocketAddrs`
93
+ /// does not match that returned from [`ToSocketAddrs`].
94
+ ///
72
95
/// See https://github.com/rust-lang/rust/issues/34202 for more details.
96
+ ///
97
+ /// [`ToSocketAddrs`]: ../../std/net/trait.ToSocketAddrs.html
98
+ ///
99
+ /// # Examples
100
+ ///
101
+ /// ```no_run
102
+ /// use std::net::UdpSocket;
103
+ ///
104
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
105
+ /// socket.send_to(&[0; 10], "127.0.0.1:4242").expect("couldn't send data");
106
+ /// ```
73
107
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
74
108
pub fn send_to < A : ToSocketAddrs > ( & self , buf : & [ u8 ] , addr : A )
75
109
-> io:: Result < usize > {
@@ -81,6 +115,16 @@ impl UdpSocket {
81
115
}
82
116
83
117
/// Returns the socket address that this socket was created from.
118
+ ///
119
+ /// # Examples
120
+ ///
121
+ /// ```no_run
122
+ /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
123
+ ///
124
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
125
+ /// assert_eq!(socket.local_addr().unwrap(),
126
+ /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 34254)));
127
+ /// ```
84
128
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
85
129
pub fn local_addr ( & self ) -> io:: Result < SocketAddr > {
86
130
self . 0 . socket_addr ( )
@@ -91,54 +135,119 @@ impl UdpSocket {
91
135
/// The returned `UdpSocket` is a reference to the same socket that this
92
136
/// object references. Both handles will read and write the same port, and
93
137
/// options set on one socket will be propagated to the other.
138
+ ///
139
+ /// # Examples
140
+ ///
141
+ /// ```no_run
142
+ /// use std::net::UdpSocket;
143
+ ///
144
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
145
+ /// let socket_clone = socket.try_clone().expect("couldn't clone the socket");
146
+ /// ```
94
147
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
95
148
pub fn try_clone ( & self ) -> io:: Result < UdpSocket > {
96
149
self . 0 . duplicate ( ) . map ( UdpSocket )
97
150
}
98
151
99
152
/// Sets the read timeout to the timeout specified.
100
153
///
101
- /// If the value specified is `None`, then `read` calls will block
102
- /// indefinitely. It is an error to pass the zero `Duration` to this
154
+ /// If the value specified is [ `None`] , then [ `read()`] calls will block
155
+ /// indefinitely. It is an error to pass the zero [ `Duration`] to this
103
156
/// method.
104
157
///
105
158
/// # Note
106
159
///
107
160
/// Platforms may return a different error code whenever a read times out as
108
161
/// a result of setting this option. For example Unix typically returns an
109
- /// error of the kind `WouldBlock`, but Windows may return `TimedOut`.
162
+ /// error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
163
+ ///
164
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
165
+ /// [`read()`]: ../../std/io/trait.Read.html#tymethod.read
166
+ /// [`Duration`]: ../../std/time/struct.Duration.html
167
+ /// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
168
+ /// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
169
+ ///
170
+ /// # Examples
171
+ ///
172
+ /// ```no_run
173
+ /// use std::net::UdpSocket;
174
+ ///
175
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
176
+ /// socket.set_read_timeout(None).expect("set_read_timeout call failed");
177
+ /// ```
110
178
#[ stable( feature = "socket_timeout" , since = "1.4.0" ) ]
111
179
pub fn set_read_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
112
180
self . 0 . set_read_timeout ( dur)
113
181
}
114
182
115
183
/// Sets the write timeout to the timeout specified.
116
184
///
117
- /// If the value specified is `None`, then `write` calls will block
118
- /// indefinitely. It is an error to pass the zero `Duration` to this
185
+ /// If the value specified is [ `None`] , then [ `write()`] calls will block
186
+ /// indefinitely. It is an error to pass the zero [ `Duration`] to this
119
187
/// method.
120
188
///
121
189
/// # Note
122
190
///
123
191
/// Platforms may return a different error code whenever a write times out
124
192
/// as a result of setting this option. For example Unix typically returns
125
- /// an error of the kind `WouldBlock`, but Windows may return `TimedOut`.
193
+ /// an error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
194
+ ///
195
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
196
+ /// [`write()`]: ../../std/io/trait.Write.html#tymethod.write
197
+ /// [`Duration`]: ../../std/time/struct.Duration.html
198
+ /// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
199
+ /// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
200
+ ///
201
+ /// # Examples
202
+ ///
203
+ /// ```no_run
204
+ /// use std::net::UdpSocket;
205
+ ///
206
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
207
+ /// socket.set_write_timeout(None).expect("set_write_timeout call failed");
208
+ /// ```
126
209
#[ stable( feature = "socket_timeout" , since = "1.4.0" ) ]
127
210
pub fn set_write_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
128
211
self . 0 . set_write_timeout ( dur)
129
212
}
130
213
131
214
/// Returns the read timeout of this socket.
132
215
///
133
- /// If the timeout is `None`, then `read` calls will block indefinitely.
216
+ /// If the timeout is [`None`], then [`read()`] calls will block indefinitely.
217
+ ///
218
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
219
+ /// [`read()`]: ../../std/io/trait.Read.html#tymethod.read
220
+ ///
221
+ /// # Examples
222
+ ///
223
+ /// ```no_run
224
+ /// use std::net::UdpSocket;
225
+ ///
226
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
227
+ /// socket.set_read_timeout(None).expect("set_read_timeout call failed");
228
+ /// assert_eq!(socket.read_timeout().unwrap(), None);
229
+ /// ```
134
230
#[ stable( feature = "socket_timeout" , since = "1.4.0" ) ]
135
231
pub fn read_timeout ( & self ) -> io:: Result < Option < Duration > > {
136
232
self . 0 . read_timeout ( )
137
233
}
138
234
139
235
/// Returns the write timeout of this socket.
140
236
///
141
- /// If the timeout is `None`, then `write` calls will block indefinitely.
237
+ /// If the timeout is [`None`], then [`write()`] calls will block indefinitely.
238
+ ///
239
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
240
+ /// [`write()`]: ../../std/io/trait.Write.html#tymethod.write
241
+ ///
242
+ /// # Examples
243
+ ///
244
+ /// ```no_run
245
+ /// use std::net::UdpSocket;
246
+ ///
247
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
248
+ /// socket.set_write_timeout(None).expect("set_write_timeout call failed");
249
+ /// assert_eq!(socket.write_timeout().unwrap(), None);
250
+ /// ```
142
251
#[ stable( feature = "socket_timeout" , since = "1.4.0" ) ]
143
252
pub fn write_timeout ( & self ) -> io:: Result < Option < Duration > > {
144
253
self . 0 . write_timeout ( )
@@ -148,6 +257,15 @@ impl UdpSocket {
148
257
///
149
258
/// When enabled, this socket is allowed to send packets to a broadcast
150
259
/// address.
260
+ ///
261
+ /// # Examples
262
+ ///
263
+ /// ```no_run
264
+ /// use std::net::UdpSocket;
265
+ ///
266
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
267
+ /// socket.set_broadcast(false).expect("set_broadcast call failed");
268
+ /// ```
151
269
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
152
270
pub fn set_broadcast ( & self , broadcast : bool ) -> io:: Result < ( ) > {
153
271
self . 0 . set_broadcast ( broadcast)
@@ -159,6 +277,16 @@ impl UdpSocket {
159
277
/// [`set_broadcast`][link].
160
278
///
161
279
/// [link]: #method.set_broadcast
280
+ ///
281
+ /// # Examples
282
+ ///
283
+ /// ```no_run
284
+ /// use std::net::UdpSocket;
285
+ ///
286
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
287
+ /// socket.set_broadcast(false).expect("set_broadcast call failed");
288
+ /// assert_eq!(socket.broadcast().unwrap(), false);
289
+ /// ```
162
290
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
163
291
pub fn broadcast ( & self ) -> io:: Result < bool > {
164
292
self . 0 . broadcast ( )
@@ -168,6 +296,15 @@ impl UdpSocket {
168
296
///
169
297
/// If enabled, multicast packets will be looped back to the local socket.
170
298
/// Note that this may not have any affect on IPv6 sockets.
299
+ ///
300
+ /// # Examples
301
+ ///
302
+ /// ```no_run
303
+ /// use std::net::UdpSocket;
304
+ ///
305
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
306
+ /// socket.set_multicast_loop_v4(false).expect("set_multicast_loop_v4 call failed");
307
+ /// ```
171
308
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
172
309
pub fn set_multicast_loop_v4 ( & self , multicast_loop_v4 : bool ) -> io:: Result < ( ) > {
173
310
self . 0 . set_multicast_loop_v4 ( multicast_loop_v4)
@@ -179,6 +316,16 @@ impl UdpSocket {
179
316
/// [`set_multicast_loop_v4`][link].
180
317
///
181
318
/// [link]: #method.set_multicast_loop_v4
319
+ ///
320
+ /// # Examples
321
+ ///
322
+ /// ```no_run
323
+ /// use std::net::UdpSocket;
324
+ ///
325
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
326
+ /// socket.set_multicast_loop_v4(false).expect("set_multicast_loop_v4 call failed");
327
+ /// assert_eq!(socket.multicast_loop_v4().unwrap(), false);
328
+ /// ```
182
329
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
183
330
pub fn multicast_loop_v4 ( & self ) -> io:: Result < bool > {
184
331
self . 0 . multicast_loop_v4 ( )
@@ -191,6 +338,15 @@ impl UdpSocket {
191
338
/// don't leave the local network unless explicitly requested.
192
339
///
193
340
/// Note that this may not have any affect on IPv6 sockets.
341
+ ///
342
+ /// # Examples
343
+ ///
344
+ /// ```no_run
345
+ /// use std::net::UdpSocket;
346
+ ///
347
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
348
+ /// socket.set_multicast_ttl_v4(42).expect("set_multicast_ttl_v4 call failed");
349
+ /// ```
194
350
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
195
351
pub fn set_multicast_ttl_v4 ( & self , multicast_ttl_v4 : u32 ) -> io:: Result < ( ) > {
196
352
self . 0 . set_multicast_ttl_v4 ( multicast_ttl_v4)
@@ -202,6 +358,16 @@ impl UdpSocket {
202
358
/// [`set_multicast_ttl_v4`][link].
203
359
///
204
360
/// [link]: #method.set_multicast_ttl_v4
361
+ ///
362
+ /// # Examples
363
+ ///
364
+ /// ```no_run
365
+ /// use std::net::UdpSocket;
366
+ ///
367
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
368
+ /// socket.set_multicast_ttl_v4(42).expect("set_multicast_ttl_v4 call failed");
369
+ /// assert_eq!(socket.multicast_ttl_v4().unwrap(), 42);
370
+ /// ```
205
371
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
206
372
pub fn multicast_ttl_v4 ( & self ) -> io:: Result < u32 > {
207
373
self . 0 . multicast_ttl_v4 ( )
@@ -211,6 +377,15 @@ impl UdpSocket {
211
377
///
212
378
/// Controls whether this socket sees the multicast packets it sends itself.
213
379
/// Note that this may not have any affect on IPv4 sockets.
380
+ ///
381
+ /// # Examples
382
+ ///
383
+ /// ```no_run
384
+ /// use std::net::UdpSocket;
385
+ ///
386
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
387
+ /// socket.set_multicast_loop_v6(false).expect("set_multicast_loop_v6 call failed");
388
+ /// ```
214
389
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
215
390
pub fn set_multicast_loop_v6 ( & self , multicast_loop_v6 : bool ) -> io:: Result < ( ) > {
216
391
self . 0 . set_multicast_loop_v6 ( multicast_loop_v6)
@@ -222,6 +397,16 @@ impl UdpSocket {
222
397
/// [`set_multicast_loop_v6`][link].
223
398
///
224
399
/// [link]: #method.set_multicast_loop_v6
400
+ ///
401
+ /// # Examples
402
+ ///
403
+ /// ```no_run
404
+ /// use std::net::UdpSocket;
405
+ ///
406
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
407
+ /// socket.set_multicast_loop_v6(false).expect("set_multicast_loop_v6 call failed");
408
+ /// assert_eq!(socket.multicast_loop_v6().unwrap(), false);
409
+ /// ```
225
410
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
226
411
pub fn multicast_loop_v6 ( & self ) -> io:: Result < bool > {
227
412
self . 0 . multicast_loop_v6 ( )
@@ -231,6 +416,15 @@ impl UdpSocket {
231
416
///
232
417
/// This value sets the time-to-live field that is used in every packet sent
233
418
/// from this socket.
419
+ ///
420
+ /// # Examples
421
+ ///
422
+ /// ```no_run
423
+ /// use std::net::UdpSocket;
424
+ ///
425
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
426
+ /// socket.set_ttl(42).expect("set_ttl call failed");
427
+ /// ```
234
428
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
235
429
pub fn set_ttl ( & self , ttl : u32 ) -> io:: Result < ( ) > {
236
430
self . 0 . set_ttl ( ttl)
@@ -241,6 +435,16 @@ impl UdpSocket {
241
435
/// For more information about this option, see [`set_ttl`][link].
242
436
///
243
437
/// [link]: #method.set_ttl
438
+ ///
439
+ /// # Examples
440
+ ///
441
+ /// ```no_run
442
+ /// use std::net::UdpSocket;
443
+ ///
444
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
445
+ /// socket.set_ttl(42).expect("set_ttl call failed");
446
+ /// assert_eq!(socket.ttl().unwrap(), 42);
447
+ /// ```
244
448
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
245
449
pub fn ttl ( & self ) -> io:: Result < u32 > {
246
450
self . 0 . ttl ( )
0 commit comments