Skip to content

Commit 8bf026d

Browse files
committed
Rollup merge of rust-lang#48328 - frewsxcv:frewsxcv-clarify-error-zero-duration, r=sfackler
Fixes rust-lang#47311. r? @nrc
2 parents 65253fd + 0700bd1 commit 8bf026d

File tree

3 files changed

+134
-14
lines changed

3 files changed

+134
-14
lines changed

src/libstd/net/tcp.rs

+35-4
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ impl TcpStream {
259259
/// Sets the read timeout to the timeout specified.
260260
///
261261
/// If the value specified is [`None`], then [`read`] calls will block
262-
/// indefinitely. It is an error to pass the zero `Duration` to this
263-
/// method.
262+
/// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
263+
/// passed to this method.
264264
///
265265
/// # Platform-specific behavior
266266
///
@@ -269,9 +269,11 @@ impl TcpStream {
269269
/// error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
270270
///
271271
/// [`None`]: ../../std/option/enum.Option.html#variant.None
272+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
272273
/// [`read`]: ../../std/io/trait.Read.html#tymethod.read
273274
/// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
274275
/// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
276+
/// [`Duration`]: ../../std/time/struct.Duration.html
275277
///
276278
/// # Examples
277279
///
@@ -282,6 +284,20 @@ impl TcpStream {
282284
/// .expect("Couldn't connect to the server...");
283285
/// stream.set_read_timeout(None).expect("set_read_timeout call failed");
284286
/// ```
287+
///
288+
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
289+
/// method:
290+
///
291+
/// ```no_run
292+
/// use std::io;
293+
/// use std::net::TcpStream;
294+
/// use std::time::Duration;
295+
///
296+
/// let stream = TcpStream::connect("127.0.0.1:8080").unwrap();
297+
/// let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
298+
/// let err = result.unwrap_err();
299+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
300+
/// ```
285301
#[stable(feature = "socket_timeout", since = "1.4.0")]
286302
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
287303
self.0.set_read_timeout(dur)
@@ -290,8 +306,8 @@ impl TcpStream {
290306
/// Sets the write timeout to the timeout specified.
291307
///
292308
/// If the value specified is [`None`], then [`write`] calls will block
293-
/// indefinitely. It is an error to pass the zero [`Duration`] to this
294-
/// method.
309+
/// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
310+
/// passed to this method.
295311
///
296312
/// # Platform-specific behavior
297313
///
@@ -300,6 +316,7 @@ impl TcpStream {
300316
/// an error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
301317
///
302318
/// [`None`]: ../../std/option/enum.Option.html#variant.None
319+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
303320
/// [`write`]: ../../std/io/trait.Write.html#tymethod.write
304321
/// [`Duration`]: ../../std/time/struct.Duration.html
305322
/// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
@@ -314,6 +331,20 @@ impl TcpStream {
314331
/// .expect("Couldn't connect to the server...");
315332
/// stream.set_write_timeout(None).expect("set_write_timeout call failed");
316333
/// ```
334+
///
335+
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
336+
/// method:
337+
///
338+
/// ```no_run
339+
/// use std::io;
340+
/// use std::net::TcpStream;
341+
/// use std::time::Duration;
342+
///
343+
/// let stream = TcpStream::connect("127.0.0.1:8080").unwrap();
344+
/// let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
345+
/// let err = result.unwrap_err();
346+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
347+
/// ```
317348
#[stable(feature = "socket_timeout", since = "1.4.0")]
318349
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
319350
self.0.set_write_timeout(dur)

src/libstd/net/udp.rs

+34-4
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ impl UdpSocket {
228228
/// Sets the read timeout to the timeout specified.
229229
///
230230
/// If the value specified is [`None`], then [`read`] calls will block
231-
/// indefinitely. It is an error to pass the zero [`Duration`] to this
232-
/// method.
231+
/// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
232+
/// passed to this method.
233233
///
234234
/// # Platform-specific behavior
235235
///
@@ -238,6 +238,7 @@ impl UdpSocket {
238238
/// error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
239239
///
240240
/// [`None`]: ../../std/option/enum.Option.html#variant.None
241+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
241242
/// [`read`]: ../../std/io/trait.Read.html#tymethod.read
242243
/// [`Duration`]: ../../std/time/struct.Duration.html
243244
/// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
@@ -251,6 +252,20 @@ impl UdpSocket {
251252
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
252253
/// socket.set_read_timeout(None).expect("set_read_timeout call failed");
253254
/// ```
255+
///
256+
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
257+
/// method:
258+
///
259+
/// ```no_run
260+
/// use std::io;
261+
/// use std::net::UdpSocket;
262+
/// use std::time::Duration;
263+
///
264+
/// let socket = UdpSocket::bind("127.0.0.1:34254").unwrap();
265+
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
266+
/// let err = result.unwrap_err();
267+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
268+
/// ```
254269
#[stable(feature = "socket_timeout", since = "1.4.0")]
255270
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
256271
self.0.set_read_timeout(dur)
@@ -259,8 +274,8 @@ impl UdpSocket {
259274
/// Sets the write timeout to the timeout specified.
260275
///
261276
/// If the value specified is [`None`], then [`write`] calls will block
262-
/// indefinitely. It is an error to pass the zero [`Duration`] to this
263-
/// method.
277+
/// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
278+
/// passed to this method.
264279
///
265280
/// # Platform-specific behavior
266281
///
@@ -269,6 +284,7 @@ impl UdpSocket {
269284
/// an error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
270285
///
271286
/// [`None`]: ../../std/option/enum.Option.html#variant.None
287+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
272288
/// [`write`]: ../../std/io/trait.Write.html#tymethod.write
273289
/// [`Duration`]: ../../std/time/struct.Duration.html
274290
/// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
@@ -282,6 +298,20 @@ impl UdpSocket {
282298
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
283299
/// socket.set_write_timeout(None).expect("set_write_timeout call failed");
284300
/// ```
301+
///
302+
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
303+
/// method:
304+
///
305+
/// ```no_run
306+
/// use std::io;
307+
/// use std::net::UdpSocket;
308+
/// use std::time::Duration;
309+
///
310+
/// let socket = UdpSocket::bind("127.0.0.1:34254").unwrap();
311+
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
312+
/// let err = result.unwrap_err();
313+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
314+
/// ```
285315
#[stable(feature = "socket_timeout", since = "1.4.0")]
286316
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
287317
self.0.set_write_timeout(dur)

src/libstd/sys/unix/ext/net.rs

+65-6
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,11 @@ impl UnixStream {
387387
/// Sets the read timeout for the socket.
388388
///
389389
/// If the provided value is [`None`], then [`read`] calls will block
390-
/// indefinitely. It is an error to pass the zero [`Duration`] to this
390+
/// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
391391
/// method.
392392
///
393393
/// [`None`]: ../../../../std/option/enum.Option.html#variant.None
394+
/// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
394395
/// [`read`]: ../../../../std/io/trait.Read.html#tymethod.read
395396
/// [`Duration`]: ../../../../std/time/struct.Duration.html
396397
///
@@ -403,6 +404,20 @@ impl UnixStream {
403404
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
404405
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
405406
/// ```
407+
///
408+
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
409+
/// method:
410+
///
411+
/// ```no_run
412+
/// use std::io;
413+
/// use std::os::unix::net::UnixStream;
414+
/// use std::time::Duration;
415+
///
416+
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
417+
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
418+
/// let err = result.unwrap_err();
419+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
420+
/// ```
406421
#[stable(feature = "unix_socket", since = "1.10.0")]
407422
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
408423
self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
@@ -411,10 +426,11 @@ impl UnixStream {
411426
/// Sets the write timeout for the socket.
412427
///
413428
/// If the provided value is [`None`], then [`write`] calls will block
414-
/// indefinitely. It is an error to pass the zero [`Duration`] to this
415-
/// method.
429+
/// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
430+
/// passed to this method.
416431
///
417432
/// [`None`]: ../../../../std/option/enum.Option.html#variant.None
433+
/// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
418434
/// [`write`]: ../../../../std/io/trait.Write.html#tymethod.write
419435
/// [`Duration`]: ../../../../std/time/struct.Duration.html
420436
///
@@ -427,6 +443,20 @@ impl UnixStream {
427443
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
428444
/// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
429445
/// ```
446+
///
447+
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
448+
/// method:
449+
///
450+
/// ```no_run
451+
/// use std::io;
452+
/// use std::net::UdpSocket;
453+
/// use std::time::Duration;
454+
///
455+
/// let socket = UdpSocket::bind("127.0.0.1:34254").unwrap();
456+
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
457+
/// let err = result.unwrap_err();
458+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
459+
/// ```
430460
#[stable(feature = "unix_socket", since = "1.10.0")]
431461
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
432462
self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
@@ -1250,10 +1280,11 @@ impl UnixDatagram {
12501280
/// Sets the read timeout for the socket.
12511281
///
12521282
/// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
1253-
/// block indefinitely. It is an error to pass the zero [`Duration`] to this
1254-
/// method.
1283+
/// block indefinitely. An [`Err`] is returned if the zero [`Duration`]
1284+
/// is passed to this method.
12551285
///
12561286
/// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1287+
/// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
12571288
/// [`recv`]: #method.recv
12581289
/// [`recv_from`]: #method.recv_from
12591290
/// [`Duration`]: ../../../../std/time/struct.Duration.html
@@ -1267,6 +1298,20 @@ impl UnixDatagram {
12671298
/// let sock = UnixDatagram::unbound().unwrap();
12681299
/// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
12691300
/// ```
1301+
///
1302+
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
1303+
/// method:
1304+
///
1305+
/// ```no_run
1306+
/// use std::io;
1307+
/// use std::os::unix::net::UnixDatagram;
1308+
/// use std::time::Duration;
1309+
///
1310+
/// let socket = UnixDatagram::unbound().unwrap();
1311+
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
1312+
/// let err = result.unwrap_err();
1313+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
1314+
/// ```
12701315
#[stable(feature = "unix_socket", since = "1.10.0")]
12711316
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
12721317
self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
@@ -1275,7 +1320,7 @@ impl UnixDatagram {
12751320
/// Sets the write timeout for the socket.
12761321
///
12771322
/// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
1278-
/// block indefinitely. It is an error to pass the zero [`Duration`] to this
1323+
/// block indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
12791324
/// method.
12801325
///
12811326
/// [`None`]: ../../../../std/option/enum.Option.html#variant.None
@@ -1293,6 +1338,20 @@ impl UnixDatagram {
12931338
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
12941339
/// .expect("set_write_timeout function failed");
12951340
/// ```
1341+
///
1342+
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
1343+
/// method:
1344+
///
1345+
/// ```no_run
1346+
/// use std::io;
1347+
/// use std::os::unix::net::UnixDatagram;
1348+
/// use std::time::Duration;
1349+
///
1350+
/// let socket = UnixDatagram::unbound().unwrap();
1351+
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
1352+
/// let err = result.unwrap_err();
1353+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
1354+
/// ```
12961355
#[stable(feature = "unix_socket", since = "1.10.0")]
12971356
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
12981357
self.0.set_timeout(timeout, libc::SO_SNDTIMEO)

0 commit comments

Comments
 (0)