From 6d982c1ddf0735d19887d608ed68b63df7ef25bc Mon Sep 17 00:00:00 2001 From: Sola Date: Thu, 12 Dec 2024 20:54:03 +0800 Subject: [PATCH 1/2] impl `Sync` for `SerialPort` trait --- src/lib.rs | 2 +- src/windows/com.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index fbc7ab8..fc40627 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -447,7 +447,7 @@ impl SerialPortBuilder { /// /// This trait is all that's necessary to implement a new serial port driver /// for a new platform. -pub trait SerialPort: Send + io::Read + io::Write { +pub trait SerialPort: Send + Sync + io::Read + io::Write { // Port settings getters /// Returns the name of this port if it exists. diff --git a/src/windows/com.rs b/src/windows/com.rs index 1bf368f..7457271 100644 --- a/src/windows/com.rs +++ b/src/windows/com.rs @@ -32,7 +32,17 @@ pub struct COMPort { port_name: Option, } +// The Windows [`HANDLE`] type is considered safe according to the standard library. +// See explanation on [`OwnedHandle`] in stdlib for more information. +// +// [`HANDLE`]: std::os::windows::raw::HANDLE +// [`OwnedHandle`]: std::os::windows::io::OwnedHandle +// +// In the future we might want to consider using `OwnedHandle` instead of `HANDLE` directly. +// At the time of writing, such work is blocked by this crate's MSRV (1.59.0) policy as +// `OwnedHandle` was stabilized in 1.63.0. unsafe impl Send for COMPort {} +unsafe impl Sync for COMPort {} impl COMPort { /// Opens a COM port as a serial device. From 81dfac315b5b9aa31625068449bd307e1ce34308 Mon Sep 17 00:00:00 2001 From: Sola Date: Fri, 13 Dec 2024 18:11:04 +0800 Subject: [PATCH 2/2] Provide a permalink to the explanation in stdlib --- src/windows/com.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/windows/com.rs b/src/windows/com.rs index 7457271..f415d85 100644 --- a/src/windows/com.rs +++ b/src/windows/com.rs @@ -32,11 +32,9 @@ pub struct COMPort { port_name: Option, } -// The Windows [`HANDLE`] type is considered safe according to the standard library. -// See explanation on [`OwnedHandle`] in stdlib for more information. -// -// [`HANDLE`]: std::os::windows::raw::HANDLE -// [`OwnedHandle`]: std::os::windows::io::OwnedHandle +// The Windows `HANDLE` type is considered safe according to the standard library. +// See the explanation below in stdlib for more information: +// https://github.com/rust-lang/rust/blob/f4f0fafd0c7849e162eddbc69fa5fe82dbec28c7/library/std/src/os/windows/io/handle.rs#L111-L115 // // In the future we might want to consider using `OwnedHandle` instead of `HANDLE` directly. // At the time of writing, such work is blocked by this crate's MSRV (1.59.0) policy as