Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Option<Duration> for timeouts. #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ os:
- linux
- osx
rust:
- 1.16.0
- 1.19.0
- stable
- beta
- nightly
Expand Down
6 changes: 3 additions & 3 deletions serial-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn probe<P: SerialPort>(port: &mut P) -> io::Result<()> {
}));

// I/O
try!(port.set_timeout(Duration::from_millis(100)));
try!(port.set_timeout(Some(Duration::from_millis(100))));
try!(port.write(&buf[..]));
try!(port.read(&mut buf[..]));

Expand Down Expand Up @@ -174,8 +174,8 @@ impl serial::SerialDevice for CustomSerialPort {

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

fn set_rts(&mut self, level: bool) -> serial::Result<()> { ... }
fn set_dtr(&mut self, level: bool) -> serial::Result<()> { ... }
Expand Down
12 changes: 6 additions & 6 deletions serial-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,10 @@ pub trait SerialDevice: io::Read + io::Write {
fn write_settings(&mut self, settings: &Self::Settings) -> ::Result<()>;

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

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

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

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

/// Configures a serial port device.
///
Expand Down Expand Up @@ -576,11 +576,11 @@ pub trait SerialPort: io::Read + io::Write {
impl<T> SerialPort for T
where T: SerialDevice
{
fn timeout(&self) -> Duration {
fn timeout(&self) -> Option<Duration> {
T::timeout(self)
}

fn set_timeout(&mut self, timeout: Duration) -> ::Result<()> {
fn set_timeout(&mut self, timeout: Option<Duration>) -> ::Result<()> {
T::set_timeout(self, timeout)
}

Expand Down
45 changes: 29 additions & 16 deletions serial-unix/src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ const POLLERR: c_short = 0x0008;
const POLLHUP: c_short = 0x0010;
const POLLNVAL: c_short = 0x0020;

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

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

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

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

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

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

let mut timeout_ts = libc::timespec {
tv_sec: timeout.as_secs() as libc::time_t,
tv_nsec: timeout.subsec_nanos() as libc::c_long,
};
if let Some(timeout) = timeout {
let mut timeout_ts = libc::timespec {
tv_sec: timeout.as_secs() as libc::time_t,
tv_nsec: timeout.subsec_nanos() as libc::c_long,
};

unsafe {
ppoll((&mut fds[..]).as_mut_ptr(),
fds.len() as nfds_t,
&mut timeout_ts,
ptr::null())
unsafe {
ppoll((&mut fds[..]).as_mut_ptr(),
fds.len() as nfds_t,
&mut timeout_ts,
ptr::null())
}
} else {
unsafe {
ppoll((&mut fds[..]).as_mut_ptr(),
fds.len() as nfds_t,
ptr::null_mut(),
ptr::null())
}
}
}

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

let milliseconds = timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000;
let milliseconds = if let Some(timeout) = timeout {
(timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000) as c_int
} else {
-1
};

unsafe {
poll((&mut fds[..]).as_mut_ptr(),
fds.len() as nfds_t,
milliseconds as c_int)
milliseconds)
}
}
8 changes: 4 additions & 4 deletions serial-unix/src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const O_NOCTTY: c_int = 0;
/// The port will be closed when the value is dropped.
pub struct TTYPort {
fd: RawFd,
timeout: Duration,
timeout: Option<Duration>,
}

impl TTYPort {
Expand Down Expand Up @@ -65,7 +65,7 @@ impl TTYPort {

let mut port = TTYPort {
fd: fd,
timeout: Duration::from_millis(100),
timeout: None,
};

// get exclusive access to device
Expand Down Expand Up @@ -205,11 +205,11 @@ impl SerialDevice for TTYPort {
Ok(())
}

fn timeout(&self) -> Duration {
fn timeout(&self) -> Option<Duration> {
self.timeout
}

fn set_timeout(&mut self, timeout: Duration) -> core::Result<()> {
fn set_timeout(&mut self, timeout: Option<Duration>) -> core::Result<()> {
self.timeout = timeout;
Ok(())
}
Expand Down
38 changes: 21 additions & 17 deletions serial-windows/src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use ffi::*;
/// The port will be closed when the value is dropped.
pub struct COMPort {
handle: HANDLE,
timeout: Duration,
timeout: Option<Duration>,
}

unsafe impl Send for COMPort {}
Expand Down Expand Up @@ -50,15 +50,13 @@ impl COMPort {
CreateFileW(name.as_ptr(), GENERIC_READ | GENERIC_WRITE, 0, ptr::null_mut(), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 as HANDLE)
};

let timeout = Duration::from_millis(100);

if handle != INVALID_HANDLE_VALUE {
let mut port = COMPort {
handle: handle,
timeout: timeout,
timeout: None,
};

try!(port.set_timeout(timeout));
try!(port.set_timeout(None));
Ok(port)
}
else {
Expand Down Expand Up @@ -160,23 +158,29 @@ impl SerialDevice for COMPort {
}
}

fn timeout(&self) -> Duration {
fn timeout(&self) -> Option<Duration> {
self.timeout
}

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

let timeouts = COMMTIMEOUTS {
ReadIntervalTimeout: 0,
ReadTotalTimeoutMultiplier: 0,
ReadTotalTimeoutConstant: milliseconds as DWORD,
WriteTotalTimeoutMultiplier: 0,
WriteTotalTimeoutConstant: 0,
};
let timeouts = COMMTIMEOUTS {
ReadIntervalTimeout: 0,
ReadTotalTimeoutMultiplier: 0,
ReadTotalTimeoutConstant: milliseconds as DWORD,
WriteTotalTimeoutMultiplier: 0,
WriteTotalTimeoutConstant: 0,
};

if unsafe { SetCommTimeouts(self.handle, &timeouts) } == 0 {
return Err(error::last_os_error());
if unsafe { SetCommTimeouts(self.handle, &timeouts) } == 0 {
return Err(error::last_os_error());
}
} else {
if unsafe { SetCommTimeouts(self.handle, ptr::null()) } == 0 {
return Err(error::last_os_error());
}
}

self.timeout = timeout;
Expand Down
2 changes: 1 addition & 1 deletion serial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> {
Ok(())
}));

try!(port.set_timeout(Duration::from_millis(1000)));
try!(port.set_timeout(Some(Duration::from_millis(1000))));

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

Expand Down
2 changes: 1 addition & 1 deletion serial/examples/probe_pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {

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

try!(port.set_rts(false));
try!(port.set_dtr(false));
Expand Down
2 changes: 1 addition & 1 deletion serial/examples/read_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() {

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

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

Expand Down