Skip to content

Commit

Permalink
Wrap TTYPort::open
Browse files Browse the repository at this point in the history
This allows a user to directly use Serial::open without needing to now
about serial_unix::TTYPort.
  • Loading branch information
rnestler committed Jun 8, 2019
1 parent ba9b608 commit 7e5a1bd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -14,6 +14,7 @@ i2cdev = "0.4.1"
spidev = "0.3.0"
sysfs_gpio = "0.5.1"
serial-unix = "0.4.0"
serial-core = "0.4.0"
nb = "0.1.1"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -18,6 +18,7 @@ pub extern crate i2cdev;
pub extern crate spidev;
pub extern crate sysfs_gpio;
pub extern crate serial_unix;
pub extern crate serial_core;
pub extern crate nb;

use std::io::{self, Write};
Expand Down
12 changes: 10 additions & 2 deletions src/serial.rs
@@ -1,16 +1,25 @@
//! Implementation of [`Serial`](https://docs.rs/embedded-hal/0.2.1/embedded_hal/serial/index.html)

use std::io::{ErrorKind as IoErrorKind, Read, Write};
use std::path::Path;

use nb;

use hal;
use serial_core;
use serial_unix::TTYPort;

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

impl Serial {
/// Wrapper for `serial_unix::TTYPort::open`
pub fn open(path: &Path) -> Result<Serial, serial_core::Error> {
Ok(Serial(TTYPort::open(path)?))
}
}

/// Helper to convert std::io::Error to the nb::Error
fn translate_io_errors(err: std::io::Error) -> nb::Error<IoErrorKind> {
match err.kind() {
Expand Down Expand Up @@ -60,8 +69,7 @@ mod test {
fn create_pty_and_serial() -> (std::fs::File, Serial) {
let (master, _slave, name) =
openpty::openpty(None, None, None).expect("Creating pty failed");
let port = TTYPort::open(Path::new(&name)).expect("Creating TTYPort failed");
let serial = Serial(port);
let serial = Serial::open(Path::new(&name)).expect("Creating TTYPort failed");
(master, serial)
}

Expand Down

0 comments on commit 7e5a1bd

Please sign in to comment.