Skip to content

Commit 1bc266a

Browse files
committed
Use Option<Duration> for timeouts.
1 parent cb28b14 commit 1bc266a

File tree

9 files changed

+64
-45
lines changed

9 files changed

+64
-45
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ os:
33
- linux
44
- osx
55
rust:
6-
- 1.16.0
6+
- 1.19.0
77
- stable
88
- beta
99
- nightly

serial-core/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn probe<P: SerialPort>(port: &mut P) -> io::Result<()> {
5252
}));
5353

5454
// I/O
55-
try!(port.set_timeout(Duration::from_millis(100)));
55+
try!(port.set_timeout(Some(Duration::from_millis(100))));
5656
try!(port.write(&buf[..]));
5757
try!(port.read(&mut buf[..]));
5858

@@ -174,8 +174,8 @@ impl serial::SerialDevice for CustomSerialPort {
174174

175175
fn read_settings(&self) -> serial::Result<Self::Settings> { ... }
176176
fn write_settings(&mut self, settings: &Self::Settings) -> serial::Result<()> { ... }
177-
fn timeout(&self) -> Duration { ... }
178-
fn set_timeout(&mut self, timeout: Duration) -> serial::Result<()> { ... }
177+
fn timeout(&self) -> Option<Duration> { ... }
178+
fn set_timeout(&mut self, timeout: Option<Duration>) -> serial::Result<()> { ... }
179179

180180
fn set_rts(&mut self, level: bool) -> serial::Result<()> { ... }
181181
fn set_dtr(&mut self, level: bool) -> serial::Result<()> { ... }

serial-core/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,10 @@ pub trait SerialDevice: io::Read + io::Write {
341341
fn write_settings(&mut self, settings: &Self::Settings) -> ::Result<()>;
342342

343343
/// Returns the current timeout.
344-
fn timeout(&self) -> Duration;
344+
fn timeout(&self) -> Option<Duration>;
345345

346346
/// Sets the timeout for future I/O operations.
347-
fn set_timeout(&mut self, timeout: Duration) -> ::Result<()>;
347+
fn set_timeout(&mut self, timeout: Option<Duration>) -> ::Result<()>;
348348

349349
/// Sets the state of the RTS (Request To Send) control signal.
350350
///
@@ -437,10 +437,10 @@ pub trait SerialDevice: io::Read + io::Write {
437437
/// The serial port will be closed when the value is dropped.
438438
pub trait SerialPort: io::Read + io::Write {
439439
/// Returns the current timeout.
440-
fn timeout(&self) -> Duration;
440+
fn timeout(&self) -> Option<Duration>;
441441

442442
/// Sets the timeout for future I/O operations.
443-
fn set_timeout(&mut self, timeout: Duration) -> ::Result<()>;
443+
fn set_timeout(&mut self, timeout: Option<Duration>) -> ::Result<()>;
444444

445445
/// Configures a serial port device.
446446
///
@@ -576,11 +576,11 @@ pub trait SerialPort: io::Read + io::Write {
576576
impl<T> SerialPort for T
577577
where T: SerialDevice
578578
{
579-
fn timeout(&self) -> Duration {
579+
fn timeout(&self) -> Option<Duration> {
580580
T::timeout(self)
581581
}
582582

583-
fn set_timeout(&mut self, timeout: Duration) -> ::Result<()> {
583+
fn set_timeout(&mut self, timeout: Option<Duration>) -> ::Result<()> {
584584
T::set_timeout(self, timeout)
585585
}
586586

serial-unix/src/poll.rs

+29-16
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ const POLLERR: c_short = 0x0008;
2929
const POLLHUP: c_short = 0x0010;
3030
const POLLNVAL: c_short = 0x0020;
3131

32-
pub fn wait_read_fd(fd: c_int, timeout: Duration) -> io::Result<()> {
32+
pub fn wait_read_fd(fd: c_int, timeout: Option<Duration>) -> io::Result<()> {
3333
wait_fd(fd, POLLIN, timeout)
3434
}
3535

36-
pub fn wait_write_fd(fd: c_int, timeout: Duration) -> io::Result<()> {
36+
pub fn wait_write_fd(fd: c_int, timeout: Option<Duration>) -> io::Result<()> {
3737
wait_fd(fd, POLLOUT, timeout)
3838
}
3939

40-
fn wait_fd(fd: c_int, events: c_short, timeout: Duration) -> io::Result<()> {
40+
fn wait_fd(fd: c_int, events: c_short, timeout: Option<Duration>) -> io::Result<()> {
4141
use libc::{EINTR, EPIPE, EIO};
4242

4343
let mut fds = vec!(pollfd { fd: fd, events: events, revents: 0 });
@@ -72,7 +72,7 @@ fn wait_fd(fd: c_int, events: c_short, timeout: Duration) -> io::Result<()> {
7272

7373
#[cfg(target_os = "linux")]
7474
#[inline]
75-
fn do_poll(fds: &mut Vec<pollfd>, timeout: Duration) -> c_int {
75+
fn do_poll(fds: &mut Vec<pollfd>, timeout: Option<Duration>) -> c_int {
7676
use std::ptr;
7777

7878
use libc::c_void;
@@ -86,31 +86,44 @@ fn do_poll(fds: &mut Vec<pollfd>, timeout: Duration) -> c_int {
8686
fn ppoll(fds: *mut pollfd, nfds: nfds_t, timeout_ts: *mut libc::timespec, sigmask: *const sigset_t) -> c_int;
8787
}
8888

89-
let mut timeout_ts = libc::timespec {
90-
tv_sec: timeout.as_secs() as libc::time_t,
91-
tv_nsec: timeout.subsec_nanos() as libc::c_long,
92-
};
89+
if let Some(timeout) = timeout {
90+
let mut timeout_ts = libc::timespec {
91+
tv_sec: timeout.as_secs() as libc::time_t,
92+
tv_nsec: timeout.subsec_nanos() as libc::c_long,
93+
};
9394

94-
unsafe {
95-
ppoll((&mut fds[..]).as_mut_ptr(),
96-
fds.len() as nfds_t,
97-
&mut timeout_ts,
98-
ptr::null())
95+
unsafe {
96+
ppoll((&mut fds[..]).as_mut_ptr(),
97+
fds.len() as nfds_t,
98+
&mut timeout_ts,
99+
ptr::null())
100+
}
101+
} else {
102+
unsafe {
103+
ppoll((&mut fds[..]).as_mut_ptr(),
104+
fds.len() as nfds_t,
105+
ptr::null_mut(),
106+
ptr::null())
107+
}
99108
}
100109
}
101110

102111
#[cfg(not(target_os = "linux"))]
103112
#[inline]
104-
fn do_poll(fds: &mut Vec<pollfd>, timeout: Duration) -> c_int {
113+
fn do_poll(fds: &mut Vec<pollfd>, timeout: Option<Duration>) -> c_int {
105114
extern "C" {
106115
fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int;
107116
}
108117

109-
let milliseconds = timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000;
118+
let milliseconds = if let Some(timeout) = timeout {
119+
(timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000) as c_int
120+
} else {
121+
-1
122+
};
110123

111124
unsafe {
112125
poll((&mut fds[..]).as_mut_ptr(),
113126
fds.len() as nfds_t,
114-
milliseconds as c_int)
127+
milliseconds)
115128
}
116129
}

serial-unix/src/tty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const O_NOCTTY: c_int = 0;
3030
/// The port will be closed when the value is dropped.
3131
pub struct TTYPort {
3232
fd: RawFd,
33-
timeout: Duration,
33+
timeout: Option<Duration>,
3434
}
3535

3636
impl TTYPort {
@@ -65,7 +65,7 @@ impl TTYPort {
6565

6666
let mut port = TTYPort {
6767
fd: fd,
68-
timeout: Duration::from_millis(100),
68+
timeout: None,
6969
};
7070

7171
// get exclusive access to device
@@ -205,11 +205,11 @@ impl SerialDevice for TTYPort {
205205
Ok(())
206206
}
207207

208-
fn timeout(&self) -> Duration {
208+
fn timeout(&self) -> Option<Duration> {
209209
self.timeout
210210
}
211211

212-
fn set_timeout(&mut self, timeout: Duration) -> core::Result<()> {
212+
fn set_timeout(&mut self, timeout: Option<Duration>) -> core::Result<()> {
213213
self.timeout = timeout;
214214
Ok(())
215215
}

serial-windows/src/com.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,29 @@ impl SerialDevice for COMPort {
160160
}
161161
}
162162

163-
fn timeout(&self) -> Duration {
163+
fn timeout(&self) -> Option<Duration> {
164164
self.timeout
165165
}
166166

167-
fn set_timeout(&mut self, timeout: Duration) -> core::Result<()> {
168-
let milliseconds = timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000;
167+
fn set_timeout(&mut self, timeout: Option<Duration>) -> core::Result<()> {
168+
if let Some(timeout) = timeout {
169+
let milliseconds = timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000;
169170

170-
let timeouts = COMMTIMEOUTS {
171-
ReadIntervalTimeout: 0,
172-
ReadTotalTimeoutMultiplier: 0,
173-
ReadTotalTimeoutConstant: milliseconds as DWORD,
174-
WriteTotalTimeoutMultiplier: 0,
175-
WriteTotalTimeoutConstant: 0,
176-
};
171+
let timeouts = COMMTIMEOUTS {
172+
ReadIntervalTimeout: 0,
173+
ReadTotalTimeoutMultiplier: 0,
174+
ReadTotalTimeoutConstant: milliseconds as DWORD,
175+
WriteTotalTimeoutMultiplier: 0,
176+
WriteTotalTimeoutConstant: 0,
177+
};
177178

178-
if unsafe { SetCommTimeouts(self.handle, &timeouts) } == 0 {
179-
return Err(error::last_os_error());
179+
if unsafe { SetCommTimeouts(self.handle, &timeouts) } == 0 {
180+
return Err(error::last_os_error());
181+
}
182+
} else {
183+
if unsafe { SetCommTimeouts(self.handle, ptr::null()) } == 0 {
184+
return Err(error::last_os_error());
185+
}
180186
}
181187

182188
self.timeout = timeout;

serial/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> {
5959
Ok(())
6060
}));
6161

62-
try!(port.set_timeout(Duration::from_millis(1000)));
62+
try!(port.set_timeout(Some(Duration::from_millis(1000))));
6363

6464
let mut buf: Vec<u8> = (0..255).collect();
6565

serial/examples/probe_pins.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn main() {
2424

2525
fn probe_pins<T: SerialPort>(port: &mut T) -> serial::Result<()> {
2626
try!(port.configure(&SETTINGS));
27-
try!(port.set_timeout(Duration::from_millis(100)));
27+
try!(port.set_timeout(Some(Duration::from_millis(100))));
2828

2929
try!(port.set_rts(false));
3030
try!(port.set_dtr(false));

serial/examples/read_write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn main() {
2525

2626
fn interact<T: SerialPort>(port: &mut T) -> serial::Result<()> {
2727
try!(port.configure(&SETTINGS));
28-
try!(port.set_timeout(Duration::from_secs(1)));
28+
try!(port.set_timeout(Some(Duration::from_secs(1))));
2929

3030
let mut buf: Vec<u8> = (0..255).collect();
3131

0 commit comments

Comments
 (0)