Skip to content

Commit c7cd275

Browse files
committed
std::net: Use Into<Option<Duration>> for optional timeouts
This allows writing code like stream.set_read_timeout(Duration::from_millis(200)); instead of requiring an additional `Some` wrapping the duration as is currently the case: stream.set_read_timeout(Some(Duration::from_millis(200)));
1 parent 1d0ae3d commit c7cd275

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/libstd/net/tcp.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ impl TcpStream {
136136
/// a result of setting this option. For example Unix typically returns an
137137
/// error of the kind `WouldBlock`, but Windows may return `TimedOut`.
138138
#[stable(feature = "socket_timeout", since = "1.4.0")]
139-
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
140-
self.0.set_read_timeout(dur)
139+
pub fn set_read_timeout<D>(&self, dur: D) -> io::Result<()>
140+
where D: Into<Option<Duration>> {
141+
self.0.set_read_timeout(dur.into())
141142
}
142143

143144
/// Sets the write timeout to the timeout specified.
@@ -152,8 +153,9 @@ impl TcpStream {
152153
/// as a result of setting this option. For example Unix typically returns
153154
/// an error of the kind `WouldBlock`, but Windows may return `TimedOut`.
154155
#[stable(feature = "socket_timeout", since = "1.4.0")]
155-
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
156-
self.0.set_write_timeout(dur)
156+
pub fn set_write_timeout<D>(&self, dur: D) -> io::Result<()>
157+
where D: Into<Option<Duration>> {
158+
self.0.set_write_timeout(dur.into())
157159
}
158160

159161
/// Returns the read timeout of this socket.
@@ -1059,12 +1061,12 @@ mod tests {
10591061

10601062
assert_eq!(None, t!(stream.read_timeout()));
10611063

1062-
t!(stream.set_read_timeout(Some(dur)));
1064+
t!(stream.set_read_timeout(dur));
10631065
assert_eq!(Some(dur), t!(stream.read_timeout()));
10641066

10651067
assert_eq!(None, t!(stream.write_timeout()));
10661068

1067-
t!(stream.set_write_timeout(Some(dur)));
1069+
t!(stream.set_write_timeout(dur));
10681070
assert_eq!(Some(dur), t!(stream.write_timeout()));
10691071

10701072
t!(stream.set_read_timeout(None));
@@ -1081,7 +1083,7 @@ mod tests {
10811083
let listener = t!(TcpListener::bind(&addr));
10821084

10831085
let mut stream = t!(TcpStream::connect(&("localhost", addr.port())));
1084-
t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1086+
t!(stream.set_read_timeout(Duration::from_millis(1000)));
10851087

10861088
let mut buf = [0; 10];
10871089
let start = Instant::now();
@@ -1097,7 +1099,7 @@ mod tests {
10971099
let listener = t!(TcpListener::bind(&addr));
10981100

10991101
let mut stream = t!(TcpStream::connect(&("localhost", addr.port())));
1100-
t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1102+
t!(stream.set_read_timeout(Duration::from_millis(1000)));
11011103

11021104
let mut other_end = t!(listener.accept()).0;
11031105
t!(other_end.write_all(b"hello world"));

src/libstd/net/udp.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ impl UdpSocket {
105105
/// a result of setting this option. For example Unix typically returns an
106106
/// error of the kind `WouldBlock`, but Windows may return `TimedOut`.
107107
#[stable(feature = "socket_timeout", since = "1.4.0")]
108-
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
109-
self.0.set_read_timeout(dur)
108+
pub fn set_read_timeout<D>(&self, dur: D) -> io::Result<()>
109+
where D: Into<Option<Duration>> {
110+
self.0.set_read_timeout(dur.into())
110111
}
111112

112113
/// Sets the write timeout to the timeout specified.
@@ -121,8 +122,9 @@ impl UdpSocket {
121122
/// as a result of setting this option. For example Unix typically returns
122123
/// an error of the kind `WouldBlock`, but Windows may return `TimedOut`.
123124
#[stable(feature = "socket_timeout", since = "1.4.0")]
124-
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
125-
self.0.set_write_timeout(dur)
125+
pub fn set_write_timeout<D>(&self, dur: D) -> io::Result<()>
126+
where D: Into<Option<Duration>> {
127+
self.0.set_write_timeout(dur.into())
126128
}
127129

128130
/// Returns the read timeout of this socket.
@@ -567,12 +569,12 @@ mod tests {
567569

568570
assert_eq!(None, t!(stream.read_timeout()));
569571

570-
t!(stream.set_read_timeout(Some(dur)));
572+
t!(stream.set_read_timeout(dur));
571573
assert_eq!(Some(dur), t!(stream.read_timeout()));
572574

573575
assert_eq!(None, t!(stream.write_timeout()));
574576

575-
t!(stream.set_write_timeout(Some(dur)));
577+
t!(stream.set_write_timeout(dur));
576578
assert_eq!(Some(dur), t!(stream.write_timeout()));
577579

578580
t!(stream.set_read_timeout(None));
@@ -587,7 +589,7 @@ mod tests {
587589
let addr = next_test_ip4();
588590

589591
let stream = t!(UdpSocket::bind(&addr));
590-
t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
592+
t!(stream.set_read_timeout(Duration::from_millis(1000)));
591593

592594
let mut buf = [0; 10];
593595

@@ -602,7 +604,7 @@ mod tests {
602604
let addr = next_test_ip4();
603605

604606
let stream = t!(UdpSocket::bind(&addr));
605-
t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
607+
t!(stream.set_read_timeout(Duration::from_millis(1000)));
606608

607609
t!(stream.send_to(b"hello world", &addr));
608610

0 commit comments

Comments
 (0)