Skip to content

Consider impl Sync for SerialPort trait #232

@unlimitedsola

Description

@unlimitedsola

I would like to propose implementing the Sync trait for the SerialPort trait. The primary blocker for this seems like COMPort does not automatically implement Sync due to the HANDLE type using a raw pointer. TTYPort already implements Sync because it uses RawFd which is simply a primitive type.

Upon investigation, I found the Rust standard library contains the following statement about Windows HANDLE types:

// The Windows [`HANDLE`] type may be transferred across and shared between
// thread boundaries (despite containing a `*mut void`, which in general isn't
// `Send` or `Sync`).
//
// [`HANDLE`]: std::os::windows::raw::HANDLE
#[stable(feature = "io_safety", since = "1.63.0")]
unsafe impl Send for OwnedHandle {}
// ...
#[stable(feature = "io_safety", since = "1.63.0")]
unsafe impl Sync for OwnedHandle {}

Maybe we could do the same for the COMPort type by implementing the Sync trait similarly. This would allow SerialPort to also implement Sync, enabling usage in multi-threaded contexts. I believe this is sound because IO-related operations still require exclusive access to &mut Self to perform.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions