Skip to content

Commit d872820

Browse files
committed
feat: allow blocking read
1 parent 83fd7fb commit d872820

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ pub struct SerialPortBuilder {
326326
/// Number of bits to use to signal the end of a character
327327
stop_bits: StopBits,
328328
/// Amount of time to wait to receive data before timing out
329-
timeout: Duration,
329+
timeout: Option<Duration>,
330330
}
331331

332332
impl SerialPortBuilder {
@@ -375,7 +375,7 @@ impl SerialPortBuilder {
375375
/// Set the amount of time to wait to receive data before timing out
376376
#[must_use]
377377
pub fn timeout(mut self, timeout: Duration) -> Self {
378-
self.timeout = timeout;
378+
self.timeout = Some(timeout);
379379
self
380380
}
381381

@@ -459,7 +459,7 @@ pub trait SerialPort: Send + io::Read + io::Write {
459459
fn stop_bits(&self) -> Result<StopBits>;
460460

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

464464
// Port settings setters
465465

@@ -485,7 +485,7 @@ pub trait SerialPort: Send + io::Read + io::Write {
485485
fn set_stop_bits(&mut self, stop_bits: StopBits) -> Result<()>;
486486

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

490490
// Functions for setting non-data control signal pins
491491

@@ -647,7 +647,7 @@ impl<T: SerialPort> SerialPort for &mut T {
647647
(**self).stop_bits()
648648
}
649649

650-
fn timeout(&self) -> Duration {
650+
fn timeout(&self) -> Option<Duration> {
651651
(**self).timeout()
652652
}
653653

@@ -671,7 +671,7 @@ impl<T: SerialPort> SerialPort for &mut T {
671671
(**self).set_stop_bits(stop_bits)
672672
}
673673

674-
fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
674+
fn set_timeout(&mut self, timeout: Option<Duration>) -> Result<()> {
675675
(**self).set_timeout(timeout)
676676
}
677677

@@ -812,7 +812,7 @@ pub fn new<'a>(path: impl Into<std::borrow::Cow<'a, str>>, baud_rate: u32) -> Se
812812
flow_control: FlowControl::None,
813813
parity: Parity::None,
814814
stop_bits: StopBits::One,
815-
timeout: Duration::from_millis(0),
815+
timeout: None,
816816
}
817817
}
818818

src/posix/poll.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,31 @@ use nix::sys::signal::SigSet;
1111
#[cfg(target_os = "linux")]
1212
use nix::sys::time::{TimeSpec, TimeValLike};
1313

14-
pub fn wait_read_fd(fd: RawFd, timeout: Duration) -> io::Result<()> {
14+
pub fn wait_read_fd(fd: RawFd, timeout: Option<Duration>) -> io::Result<()> {
1515
wait_fd(fd, PollFlags::POLLIN, timeout)
1616
}
1717

18-
pub fn wait_write_fd(fd: RawFd, timeout: Duration) -> io::Result<()> {
18+
pub fn wait_write_fd(fd: RawFd, timeout: Option<Duration>) -> io::Result<()> {
1919
wait_fd(fd, PollFlags::POLLOUT, timeout)
2020
}
2121

22-
fn wait_fd(fd: RawFd, events: PollFlags, timeout: Duration) -> io::Result<()> {
22+
fn wait_fd(fd: RawFd, events: PollFlags, timeout: Option<Duration>) -> io::Result<()> {
2323
use nix::errno::Errno::{EIO, EPIPE};
2424

2525
let mut fd = PollFd::new(fd, events);
2626

2727
let milliseconds =
28-
timeout.as_secs() as i64 * 1000 + i64::from(timeout.subsec_nanos()) / 1_000_000;
28+
timeout.map(|t| t.as_secs() as i64 * 1000 + i64::from(t.subsec_nanos()) / 1_000_000);
2929
#[cfg(target_os = "linux")]
3030
let wait_res = {
31-
let timespec = TimeSpec::milliseconds(milliseconds);
32-
nix::poll::ppoll(
33-
slice::from_mut(&mut fd),
34-
Some(timespec),
35-
Some(SigSet::empty()),
36-
)
31+
let timespec = milliseconds.map(TimeSpec::milliseconds);
32+
nix::poll::ppoll(slice::from_mut(&mut fd), timespec, Some(SigSet::empty()))
3733
};
3834
#[cfg(not(target_os = "linux"))]
39-
let wait_res = nix::poll::poll(slice::from_mut(&mut fd), milliseconds as nix::libc::c_int);
35+
let wait_res = nix::poll::poll(
36+
slice::from_mut(&mut fd),
37+
milliseconds.unwrap_or(-1) as nix::libc::c_int,
38+
);
4039

4140
let wait = match wait_res {
4241
Ok(r) => r,

src/posix/tty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn close(fd: RawFd) {
6060
#[derive(Debug)]
6161
pub struct TTYPort {
6262
fd: RawFd,
63-
timeout: Duration,
63+
timeout: Option<Duration>,
6464
exclusive: bool,
6565
port_name: Option<String>,
6666
#[cfg(any(target_os = "ios", target_os = "macos"))]
@@ -311,7 +311,7 @@ impl TTYPort {
311311

312312
let slave_tty = TTYPort {
313313
fd,
314-
timeout: Duration::from_millis(100),
314+
timeout: Some(Duration::from_millis(100)),
315315
exclusive: true,
316316
port_name: Some(ptty_name),
317317
#[cfg(any(target_os = "ios", target_os = "macos"))]
@@ -323,7 +323,7 @@ impl TTYPort {
323323
// BSDs when used on the master port.
324324
let master_tty = TTYPort {
325325
fd: next_pty_fd.into_raw_fd(),
326-
timeout: Duration::from_millis(100),
326+
timeout: Some(Duration::from_millis(100)),
327327
exclusive: true,
328328
port_name: None,
329329
#[cfg(any(target_os = "ios", target_os = "macos"))]
@@ -407,7 +407,7 @@ impl FromRawFd for TTYPort {
407407
unsafe fn from_raw_fd(fd: RawFd) -> Self {
408408
TTYPort {
409409
fd,
410-
timeout: Duration::from_millis(100),
410+
timeout: Some(Duration::from_millis(100)),
411411
exclusive: ioctl::tiocexcl(fd).is_ok(),
412412
// It is not trivial to get the file path corresponding to a file descriptor.
413413
// We'll punt on it and set it to `None` here.
@@ -616,7 +616,7 @@ impl SerialPort for TTYPort {
616616
}
617617
}
618618

619-
fn timeout(&self) -> Duration {
619+
fn timeout(&self) -> Option<Duration> {
620620
self.timeout
621621
}
622622

@@ -678,7 +678,7 @@ impl SerialPort for TTYPort {
678678
return termios::set_termios(self.fd, &termios);
679679
}
680680

681-
fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
681+
fn set_timeout(&mut self, timeout: Option<Duration>) -> Result<()> {
682682
self.timeout = timeout;
683683
Ok(())
684684
}

0 commit comments

Comments
 (0)