Skip to content

Commit b4239f1

Browse files
committed
Wrap TTYPort::open
This allows a user to directly use Serial::open without needing to now about serial_unix::TTYPort.
1 parent 1fe6b63 commit b4239f1

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ i2cdev = "0.4.1"
1414
spidev = "0.3.0"
1515
sysfs_gpio = "0.5.1"
1616
serial-unix = "0.4.0"
17+
serial-core = "0.4.0"
1718
nb = "0.1.1"
1819

1920
[dev-dependencies]

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub extern crate i2cdev;
1818
pub extern crate spidev;
1919
pub extern crate sysfs_gpio;
2020
pub extern crate serial_unix;
21+
pub extern crate serial_core;
2122
pub extern crate nb;
2223

2324
use std::io::{self, Write};

src/serial.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
//! Implementation of [`Serial`](https://docs.rs/embedded-hal/0.2.1/embedded_hal/serial/index.html)
22
33
use std::io::{ErrorKind as IoErrorKind, Read, Write};
4+
use std::path::Path;
45

56
use nb;
67

78
use hal;
9+
use serial_core;
810
use serial_unix::TTYPort;
911

1012
/// Newtype around [`serial_unix::TTYPort`] that implements
1113
/// the `embedded-hal` traits.
1214
pub struct Serial(pub TTYPort);
1315

16+
impl Serial {
17+
/// Wrapper for `serial_unix::TTYPort::open`
18+
pub fn open(path: &Path) -> Result<Serial, serial_core::Error> {
19+
Ok(Serial(TTYPort::open(path)?))
20+
}
21+
}
22+
1423
/// Helper to convert std::io::Error to the nb::Error
1524
fn translate_io_errors(err: std::io::Error) -> nb::Error<IoErrorKind> {
1625
match err.kind() {
@@ -60,8 +69,7 @@ mod test {
6069
fn create_pty_and_serial() -> (std::fs::File, Serial) {
6170
let (master, _slave, name) =
6271
openpty::openpty(None, None, None).expect("Creating pty failed");
63-
let port = TTYPort::open(Path::new(&name)).expect("Creating TTYPort failed");
64-
let serial = Serial(port);
72+
let serial = Serial::open(Path::new(&name)).expect("Creating TTYPort failed");
6573
(master, serial)
6674
}
6775

0 commit comments

Comments
 (0)