diff --git a/src/lib.rs b/src/lib.rs index f01363dc..0b0a7671 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -326,7 +326,7 @@ pub struct SerialPortBuilder { /// Number of bits to use to signal the end of a character stop_bits: StopBits, /// Amount of time to wait to receive data before timing out - timeout: Duration, + timeout: Option, } impl SerialPortBuilder { @@ -375,7 +375,7 @@ impl SerialPortBuilder { /// Set the amount of time to wait to receive data before timing out #[must_use] pub fn timeout(mut self, timeout: Duration) -> Self { - self.timeout = timeout; + self.timeout = Some(timeout); self } @@ -459,7 +459,7 @@ pub trait SerialPort: Send + io::Read + io::Write { fn stop_bits(&self) -> Result; /// Returns the current timeout. - fn timeout(&self) -> Duration; + fn timeout(&self) -> Option; // Port settings setters @@ -485,7 +485,7 @@ pub trait SerialPort: Send + io::Read + io::Write { fn set_stop_bits(&mut self, stop_bits: StopBits) -> Result<()>; /// Sets the timeout for future I/O operations. - fn set_timeout(&mut self, timeout: Duration) -> Result<()>; + fn set_timeout(&mut self, timeout: Option) -> Result<()>; // Functions for setting non-data control signal pins @@ -647,7 +647,7 @@ impl SerialPort for &mut T { (**self).stop_bits() } - fn timeout(&self) -> Duration { + fn timeout(&self) -> Option { (**self).timeout() } @@ -671,7 +671,7 @@ impl SerialPort for &mut T { (**self).set_stop_bits(stop_bits) } - fn set_timeout(&mut self, timeout: Duration) -> Result<()> { + fn set_timeout(&mut self, timeout: Option) -> Result<()> { (**self).set_timeout(timeout) } @@ -812,7 +812,7 @@ pub fn new<'a>(path: impl Into>, baud_rate: u32) -> Se flow_control: FlowControl::None, parity: Parity::None, stop_bits: StopBits::One, - timeout: Duration::from_millis(0), + timeout: None, } } diff --git a/src/posix/poll.rs b/src/posix/poll.rs index e45226ea..4cf0c656 100644 --- a/src/posix/poll.rs +++ b/src/posix/poll.rs @@ -11,32 +11,33 @@ use nix::sys::signal::SigSet; #[cfg(target_os = "linux")] use nix::sys::time::{TimeSpec, TimeValLike}; -pub fn wait_read_fd(fd: RawFd, timeout: Duration) -> io::Result<()> { +pub fn wait_read_fd(fd: RawFd, timeout: Option) -> io::Result<()> { wait_fd(fd, PollFlags::POLLIN, timeout) } -pub fn wait_write_fd(fd: RawFd, timeout: Duration) -> io::Result<()> { +pub fn wait_write_fd(fd: RawFd, timeout: Option) -> io::Result<()> { wait_fd(fd, PollFlags::POLLOUT, timeout) } -fn wait_fd(fd: RawFd, events: PollFlags, timeout: Duration) -> io::Result<()> { +fn wait_fd(fd: RawFd, events: PollFlags, timeout: Option) -> io::Result<()> { use nix::errno::Errno::{EIO, EPIPE}; let mut fd = PollFd::new(fd, events); - let milliseconds = - timeout.as_secs() as i64 * 1000 + i64::from(timeout.subsec_nanos()) / 1_000_000; + let milliseconds = timeout.map(|t| t.as_secs() as i64 * 1000 + + i64::from(t.subsec_nanos()) / 1_000_000); #[cfg(target_os = "linux")] let wait_res = { - let timespec = TimeSpec::milliseconds(milliseconds); + let timespec = milliseconds.map(TimeSpec::milliseconds); nix::poll::ppoll( slice::from_mut(&mut fd), - Some(timespec), + timespec, Some(SigSet::empty()), ) }; #[cfg(not(target_os = "linux"))] - let wait_res = nix::poll::poll(slice::from_mut(&mut fd), milliseconds as nix::libc::c_int); + let wait_res = + nix::poll::poll(slice::from_mut(&mut fd), milliseconds.unwrap_or(-1) as nix::libc::c_int); let wait = match wait_res { Ok(r) => r, diff --git a/src/posix/tty.rs b/src/posix/tty.rs index b1d68cff..2beb1d17 100644 --- a/src/posix/tty.rs +++ b/src/posix/tty.rs @@ -60,7 +60,7 @@ fn close(fd: RawFd) { #[derive(Debug)] pub struct TTYPort { fd: RawFd, - timeout: Duration, + timeout: Option, exclusive: bool, port_name: Option, #[cfg(any(target_os = "ios", target_os = "macos"))] @@ -311,7 +311,7 @@ impl TTYPort { let slave_tty = TTYPort { fd, - timeout: Duration::from_millis(100), + timeout: Some(Duration::from_millis(100)), exclusive: true, port_name: Some(ptty_name), #[cfg(any(target_os = "ios", target_os = "macos"))] @@ -323,7 +323,7 @@ impl TTYPort { // BSDs when used on the master port. let master_tty = TTYPort { fd: next_pty_fd.into_raw_fd(), - timeout: Duration::from_millis(100), + timeout: Some(Duration::from_millis(100)), exclusive: true, port_name: None, #[cfg(any(target_os = "ios", target_os = "macos"))] @@ -407,7 +407,7 @@ impl FromRawFd for TTYPort { unsafe fn from_raw_fd(fd: RawFd) -> Self { TTYPort { fd, - timeout: Duration::from_millis(100), + timeout: Some(Duration::from_millis(100)), exclusive: ioctl::tiocexcl(fd).is_ok(), // It is not trivial to get the file path corresponding to a file descriptor. // We'll punt on it and set it to `None` here. @@ -616,7 +616,7 @@ impl SerialPort for TTYPort { } } - fn timeout(&self) -> Duration { + fn timeout(&self) -> Option { self.timeout } @@ -678,7 +678,7 @@ impl SerialPort for TTYPort { return termios::set_termios(self.fd, &termios); } - fn set_timeout(&mut self, timeout: Duration) -> Result<()> { + fn set_timeout(&mut self, timeout: Option) -> Result<()> { self.timeout = timeout; Ok(()) }