Skip to content

Commit

Permalink
Add try_clone() function for SerialPort.
Browse files Browse the repository at this point in the history
  • Loading branch information
de-vri-es committed Dec 27, 2023
1 parent d278d91 commit d0e2104
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
- [add][minor] Add `SerialPort::try_clone()`.

# Version 0.2.14 - 2023-12-16
- [fix][minor] Make `TryFromError` public.

Expand Down
12 changes: 12 additions & 0 deletions src/serial_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ impl SerialPort {
})
}

/// Try to clone the serial port handle.
///
/// The cloned object refers to the same serial port.
///
/// Mixing reads and writes on different handles to the same serial port from different threads may lead to unexpect results.
/// The data may end up interleaved in unpredictable ways.
pub fn try_clone(&self) -> std::io::Result<Self> {
Ok(Self {
inner: self.inner.try_clone()?,
})
}

/// Read bytes from the serial port.
///
/// This is identical to [`std::io::Read::read()`], except that this function takes a const reference `&self`.
Expand Down
8 changes: 8 additions & 0 deletions src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ impl SerialPort {
}
}

pub fn try_clone(&self) -> std::io::Result<Self> {
Ok(Self {
file: self.file.try_clone()?,
read_timeout_ms: self.read_timeout_ms,
write_timeout_ms: self.write_timeout_ms,
})
}

pub fn get_configuration(&self) -> std::io::Result<Settings> {
Settings::get_from_file(&self.file)
}
Expand Down
6 changes: 6 additions & 0 deletions src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ impl SerialPort {
Self { file }
}

pub fn try_clone(&self) -> std::io::Result<Self> {
Ok(Self {
file: self.file.try_clone()?,
})
}

pub fn get_configuration(&self) -> std::io::Result<Settings> {
unsafe {
let mut dcb: winbase::DCB = std::mem::zeroed();
Expand Down

0 comments on commit d0e2104

Please sign in to comment.