Skip to content

Commit ebcc6d2

Browse files
Add part of missing UdpSocket's urls and examples
1 parent 7e39c0e commit ebcc6d2

File tree

1 file changed

+215
-11
lines changed

1 file changed

+215
-11
lines changed

src/libstd/net/udp.rs

+215-11
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,36 @@ pub struct UdpSocket(net_imp::UdpSocket);
4848
impl UdpSocket {
4949
/// Creates a UDP socket from the given address.
5050
///
51-
/// The address type can be any implementor of `ToSocketAddr` trait. See
51+
/// The address type can be any implementor of [`ToSocketAddrs`] trait. See
5252
/// 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+
/// ```
5363
#[stable(feature = "rust1", since = "1.0.0")]
5464
pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> {
5565
super::each_addr(addr, net_imp::UdpSocket::bind).map(UdpSocket)
5666
}
5767

5868
/// Receives data from the socket. On success, returns the number of bytes
5969
/// 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+
/// ```
6081
#[stable(feature = "rust1", since = "1.0.0")]
6182
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
6283
self.0.recv_from(buf)
@@ -65,11 +86,24 @@ impl UdpSocket {
6586
/// Sends data on the socket to the given address. On success, returns the
6687
/// number of bytes written.
6788
///
68-
/// Address type can be any implementor of `ToSocketAddrs` trait. See its
89+
/// Address type can be any implementor of [`ToSocketAddrs`] trait. See its
6990
/// documentation for concrete examples.
91+
///
7092
/// 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+
///
7295
/// 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+
/// ```
73107
#[stable(feature = "rust1", since = "1.0.0")]
74108
pub fn send_to<A: ToSocketAddrs>(&self, buf: &[u8], addr: A)
75109
-> io::Result<usize> {
@@ -81,6 +115,16 @@ impl UdpSocket {
81115
}
82116

83117
/// 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+
/// ```
84128
#[stable(feature = "rust1", since = "1.0.0")]
85129
pub fn local_addr(&self) -> io::Result<SocketAddr> {
86130
self.0.socket_addr()
@@ -91,54 +135,119 @@ impl UdpSocket {
91135
/// The returned `UdpSocket` is a reference to the same socket that this
92136
/// object references. Both handles will read and write the same port, and
93137
/// 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+
/// ```
94147
#[stable(feature = "rust1", since = "1.0.0")]
95148
pub fn try_clone(&self) -> io::Result<UdpSocket> {
96149
self.0.duplicate().map(UdpSocket)
97150
}
98151

99152
/// Sets the read timeout to the timeout specified.
100153
///
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
103156
/// method.
104157
///
105158
/// # Note
106159
///
107160
/// Platforms may return a different error code whenever a read times out as
108161
/// 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+
/// ```
110178
#[stable(feature = "socket_timeout", since = "1.4.0")]
111179
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
112180
self.0.set_read_timeout(dur)
113181
}
114182

115183
/// Sets the write timeout to the timeout specified.
116184
///
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
119187
/// method.
120188
///
121189
/// # Note
122190
///
123191
/// Platforms may return a different error code whenever a write times out
124192
/// 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+
/// ```
126209
#[stable(feature = "socket_timeout", since = "1.4.0")]
127210
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
128211
self.0.set_write_timeout(dur)
129212
}
130213

131214
/// Returns the read timeout of this socket.
132215
///
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+
/// ```
134230
#[stable(feature = "socket_timeout", since = "1.4.0")]
135231
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
136232
self.0.read_timeout()
137233
}
138234

139235
/// Returns the write timeout of this socket.
140236
///
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+
/// ```
142251
#[stable(feature = "socket_timeout", since = "1.4.0")]
143252
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
144253
self.0.write_timeout()
@@ -148,6 +257,15 @@ impl UdpSocket {
148257
///
149258
/// When enabled, this socket is allowed to send packets to a broadcast
150259
/// 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+
/// ```
151269
#[stable(feature = "net2_mutators", since = "1.9.0")]
152270
pub fn set_broadcast(&self, broadcast: bool) -> io::Result<()> {
153271
self.0.set_broadcast(broadcast)
@@ -159,6 +277,16 @@ impl UdpSocket {
159277
/// [`set_broadcast`][link].
160278
///
161279
/// [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+
/// ```
162290
#[stable(feature = "net2_mutators", since = "1.9.0")]
163291
pub fn broadcast(&self) -> io::Result<bool> {
164292
self.0.broadcast()
@@ -168,6 +296,15 @@ impl UdpSocket {
168296
///
169297
/// If enabled, multicast packets will be looped back to the local socket.
170298
/// 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+
/// ```
171308
#[stable(feature = "net2_mutators", since = "1.9.0")]
172309
pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
173310
self.0.set_multicast_loop_v4(multicast_loop_v4)
@@ -179,6 +316,16 @@ impl UdpSocket {
179316
/// [`set_multicast_loop_v4`][link].
180317
///
181318
/// [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+
/// ```
182329
#[stable(feature = "net2_mutators", since = "1.9.0")]
183330
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
184331
self.0.multicast_loop_v4()
@@ -191,6 +338,15 @@ impl UdpSocket {
191338
/// don't leave the local network unless explicitly requested.
192339
///
193340
/// 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+
/// ```
194350
#[stable(feature = "net2_mutators", since = "1.9.0")]
195351
pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
196352
self.0.set_multicast_ttl_v4(multicast_ttl_v4)
@@ -202,6 +358,16 @@ impl UdpSocket {
202358
/// [`set_multicast_ttl_v4`][link].
203359
///
204360
/// [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+
/// ```
205371
#[stable(feature = "net2_mutators", since = "1.9.0")]
206372
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
207373
self.0.multicast_ttl_v4()
@@ -211,6 +377,15 @@ impl UdpSocket {
211377
///
212378
/// Controls whether this socket sees the multicast packets it sends itself.
213379
/// 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+
/// ```
214389
#[stable(feature = "net2_mutators", since = "1.9.0")]
215390
pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
216391
self.0.set_multicast_loop_v6(multicast_loop_v6)
@@ -222,6 +397,16 @@ impl UdpSocket {
222397
/// [`set_multicast_loop_v6`][link].
223398
///
224399
/// [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+
/// ```
225410
#[stable(feature = "net2_mutators", since = "1.9.0")]
226411
pub fn multicast_loop_v6(&self) -> io::Result<bool> {
227412
self.0.multicast_loop_v6()
@@ -231,6 +416,15 @@ impl UdpSocket {
231416
///
232417
/// This value sets the time-to-live field that is used in every packet sent
233418
/// 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+
/// ```
234428
#[stable(feature = "net2_mutators", since = "1.9.0")]
235429
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
236430
self.0.set_ttl(ttl)
@@ -241,6 +435,16 @@ impl UdpSocket {
241435
/// For more information about this option, see [`set_ttl`][link].
242436
///
243437
/// [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+
/// ```
244448
#[stable(feature = "net2_mutators", since = "1.9.0")]
245449
pub fn ttl(&self) -> io::Result<u32> {
246450
self.0.ttl()

0 commit comments

Comments
 (0)