From bc481d1468f5a09de8b4683d8e2cbed36b17b26c Mon Sep 17 00:00:00 2001 From: Shane Date: Sun, 10 Mar 2024 00:15:11 -0800 Subject: [PATCH] Allow constructing a SerialStream from a TTYPort There was already an implementation in mio_serial that allowed fallible conversions from a TTYPort, this just exposes that in tokio-serial. --- Cargo.toml | 4 ++++ src/lib.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index ec7270a..ab050cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,10 @@ version = "0.4" [dependencies.cfg-if] version = "1" +[dependencies.serialport] +version = "4" +default-features = false + [dev-dependencies.tokio] version = "^1.8" features = ["macros", "rt", "process", "time", "fs", "io-util"] diff --git a/src/lib.rs b/src/lib.rs index 82ce330..8031e9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ pub use mio_serial::{ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; +use std::convert::TryFrom; use std::io::{Read, Result as IoResult, Write}; use std::pin::Pin; use std::task::{Context, Poll}; @@ -488,6 +489,18 @@ impl Write for SerialStream { } } +#[cfg(unix)] +impl TryFrom for SerialStream { + type Error = Error; + + fn try_from(value: serialport::TTYPort) -> std::result::Result { + let port = mio_serial::SerialStream::try_from(value)?; + Ok(Self { + inner: AsyncFd::new(port)?, + }) + } +} + #[cfg(unix)] mod sys { use super::SerialStream;