Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
std: Make the TCP/UDP connect_timeout methods take Duration
[breaking-change]
  • Loading branch information
brson committed Aug 13, 2014
commit 9fdcddb3178d4705db4aee5ee12c05796203658c
11 changes: 9 additions & 2 deletions src/libstd/io/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use boxed::Box;
use rt::rtio::{IoFactory, LocalIo, RtioSocket, RtioTcpListener};
use rt::rtio::{RtioTcpAcceptor, RtioTcpStream};
use rt::rtio;
use time::Duration;

/// A structure which represents a TCP stream between a local socket and a
/// remote socket.
Expand Down Expand Up @@ -102,11 +103,11 @@ impl TcpStream {
/// and port, similar to the API seen in `connect`.
#[experimental = "the timeout argument may eventually change types"]
pub fn connect_timeout(addr: SocketAddr,
timeout_ms: u64) -> IoResult<TcpStream> {
timeout: Duration) -> IoResult<TcpStream> {
let SocketAddr { ip, port } = addr;
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
LocalIo::maybe_raise(|io| {
io.tcp_connect(addr, Some(timeout_ms)).map(TcpStream::new)
io.tcp_connect(addr, Some(in_ms_u64(timeout))).map(TcpStream::new)
}).map_err(IoError::from_rtio_error)
}

Expand Down Expand Up @@ -443,6 +444,12 @@ impl Acceptor<TcpStream> for TcpAcceptor {
}
}

fn in_ms_u64(d: Duration) -> u64 {
let ms = d.num_milliseconds();
if ms < 0 { return 0 };
return ms as u64;
}

#[cfg(test)]
#[allow(experimental)]
mod test {
Expand Down
9 changes: 5 additions & 4 deletions src/libstd/io/net/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use kinds::Send;
use boxed::Box;
use rt::rtio::{IoFactory, LocalIo, RtioUnixListener};
use rt::rtio::{RtioUnixAcceptor, RtioPipe};
use time::Duration;

/// A stream which communicates over a named pipe.
pub struct UnixStream {
Expand Down Expand Up @@ -68,9 +69,9 @@ impl UnixStream {
/// elapses the function will return an error of kind `TimedOut`.
#[experimental = "the timeout argument is likely to change types"]
pub fn connect_timeout<P: ToCStr>(path: &P,
timeout_ms: u64) -> IoResult<UnixStream> {
timeout: Duration) -> IoResult<UnixStream> {
LocalIo::maybe_raise(|io| {
let s = io.unix_connect(&path.to_c_str(), Some(timeout_ms));
let s = io.unix_connect(&path.to_c_str(), Some(in_ms_u64(timeout)));
s.map(|p| UnixStream { obj: p })
}).map_err(IoError::from_rtio_error)
}
Expand Down Expand Up @@ -499,13 +500,13 @@ mod tests {

iotest!(fn connect_timeout_error() {
let addr = next_test_unix();
assert!(UnixStream::connect_timeout(&addr, 100).is_err());
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_err());
})

iotest!(fn connect_timeout_success() {
let addr = next_test_unix();
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
assert!(UnixStream::connect_timeout(&addr, 100).is_ok());
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_ok());
})

iotest!(fn close_readwrite_smoke() {
Expand Down
20 changes: 10 additions & 10 deletions src/libstd/io/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ pub struct Timer {

struct TimerCallback { tx: Sender<()> }

fn in_ms(d: Duration) -> u64 {
let ms = d.num_milliseconds();
if ms < 0 { return 0 };
return ms as u64;
}

/// Sleep the current task for the specified duration.
///
/// When provided a zero or negative `duration`, the function will
Expand Down Expand Up @@ -108,7 +102,7 @@ impl Timer {
/// return immediately.
pub fn sleep(&mut self, duration: Duration) {
// Short-circuit the timer backend for 0 duration
let ms = in_ms(duration);
let ms = in_ms_u64(duration);
if ms == 0 { return }
self.obj.sleep(ms);
}
Expand Down Expand Up @@ -153,8 +147,8 @@ impl Timer {
pub fn oneshot(&mut self, duration: Duration) -> Receiver<()> {
let (tx, rx) = channel();
// Short-circuit the timer backend for 0 duration
if in_ms(duration) != 0 {
self.obj.oneshot(in_ms(duration), box TimerCallback { tx: tx });
if in_ms_u64(duration) != 0 {
self.obj.oneshot(in_ms_u64(duration), box TimerCallback { tx: tx });
} else {
tx.send(());
}
Expand Down Expand Up @@ -207,7 +201,7 @@ impl Timer {
/// When provided a zero or negative `duration`, the messages will
/// be sent without delay.
pub fn periodic(&mut self, duration: Duration) -> Receiver<()> {
let ms = in_ms(duration);
let ms = in_ms_u64(duration);
// FIXME: The backend implementations don't ever send a message
// if given a 0 ms duration. Temporarily using 1ms. It's
// not clear what use a 0ms period is anyway...
Expand All @@ -224,6 +218,12 @@ impl Callback for TimerCallback {
}
}

fn in_ms_u64(d: Duration) -> u64 {
let ms = d.num_milliseconds();
if ms < 0 { return 0 };
return ms as u64;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just ms.to_u64().unwrap_or(0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code works fine.

}

#[cfg(test)]
mod test {
iotest!(fn test_io_timer_sleep_simple() {
Expand Down