Skip to content

Commit 27eabae

Browse files
committed
embedded: Implement both blocking and non-blocking traits
1 parent 32031cb commit 27eabae

File tree

2 files changed

+62
-31
lines changed

2 files changed

+62
-31
lines changed

examples/embedded_hal.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
//! instance can be passed to functions / drivers that expect a type that
33
//! implements the embedded-hal traits.
44
5-
use embedded_hal_nb::serial;
5+
fn take_nonblocking_reader<R: embedded_hal_nb::serial::Read<u8>>(_r: &R) {
6+
// do nothing, but things should typecheck
7+
}
68

7-
fn take_reader<R: serial::Read<u8>>(_r: &R) {
9+
fn take_nonblocking_writer<W: embedded_hal_nb::serial::Write<u8>>(_w: &W) {
810
// do nothing, but things should typecheck
911
}
1012

11-
fn take_writer<W: serial::Write<u8>>(_w: &W) {
13+
fn take_blocking_writer<W: embedded_hal::serial::Write<u8>>(_w: &W) {
1214
// do nothing, but things should typecheck
1315
}
1416

1517
fn main() {
1618
let port = serialport::new("/dev/null", 9600)
1719
.open()
1820
.expect("This example isn't meant for running. It just demonstrates compatibility with embedded-hal on a type level.");
19-
take_reader(&port);
20-
take_writer(&port);
21+
take_nonblocking_reader(&port);
22+
take_nonblocking_writer(&port);
23+
take_blocking_writer(&port);
2124
}

src/embedded.rs

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use std::io;
66

7-
use embedded_hal_nb::serial;
7+
use embedded_hal::serial::{ErrorType, ErrorKind};
88

99
use crate::SerialPort;
1010

@@ -13,47 +13,75 @@ pub struct SerialError {
1313
kind: io::ErrorKind,
1414
}
1515

16-
// Implement `serial::Error` for SerialError
17-
impl serial::Error for SerialError {
18-
fn kind(&self) -> serial::ErrorKind {
16+
impl embedded_hal::serial::Error for SerialError {
17+
fn kind(&self) -> ErrorKind {
1918
#[allow(clippy::match_single_binding)]
2019
match self.kind {
21-
_other => serial::ErrorKind::Other,
20+
_ => ErrorKind::Other,
2221
}
2322
}
2423
}
2524

26-
fn io_error_to_nb(err: io::Error) -> nb::Error<SerialError> {
27-
match err.kind() {
28-
io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted => nb::Error::WouldBlock,
29-
other => nb::Error::Other(SerialError { kind: other }),
25+
impl From<io::Error> for SerialError {
26+
fn from(e: io::Error) -> Self {
27+
SerialError {
28+
kind: e.kind(),
29+
}
3030
}
3131
}
3232

33-
impl serial::ErrorType for Box<dyn SerialPort> {
33+
impl ErrorType for Box<dyn SerialPort> {
3434
type Error = SerialError;
3535
}
3636

37-
impl serial::Read<u8> for Box<dyn SerialPort> {
38-
fn read(&mut self) -> nb::Result<u8, Self::Error> {
39-
let mut buffer = [0; 1];
40-
let bytes_read = io::Read::read(self, &mut buffer).map_err(io_error_to_nb)?;
41-
if bytes_read > 0 {
42-
Ok(buffer[0])
43-
} else {
44-
Err(nb::Error::WouldBlock)
37+
38+
mod nonblocking {
39+
use super::*;
40+
use embedded_hal_nb::serial;
41+
42+
fn io_error_to_nb(err: io::Error) -> nb::Error<SerialError> {
43+
match err.kind() {
44+
io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted => nb::Error::WouldBlock,
45+
other => nb::Error::Other(SerialError { kind: other }),
4546
}
4647
}
47-
}
4848

49-
impl serial::Write<u8> for Box<dyn SerialPort> {
50-
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
51-
io::Write::write(self, &[word])
52-
.map_err(io_error_to_nb)
53-
.map(|_| ())
49+
impl serial::Read<u8> for Box<dyn SerialPort> {
50+
fn read(&mut self) -> nb::Result<u8, Self::Error> {
51+
let mut buffer = [0; 1];
52+
let bytes_read = io::Read::read(self, &mut buffer).map_err(io_error_to_nb)?;
53+
if bytes_read > 0 {
54+
Ok(buffer[0])
55+
} else {
56+
Err(nb::Error::WouldBlock)
57+
}
58+
}
5459
}
5560

56-
fn flush(&mut self) -> nb::Result<(), Self::Error> {
57-
io::Write::flush(self).map_err(io_error_to_nb)
61+
impl serial::Write<u8> for Box<dyn SerialPort> {
62+
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
63+
io::Write::write(self, &[word])
64+
.map_err(io_error_to_nb)
65+
.map(|_| ())
66+
}
67+
68+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
69+
io::Write::flush(self).map_err(io_error_to_nb)
70+
}
71+
}
72+
}
73+
74+
mod blocking {
75+
use super::*;
76+
use embedded_hal::serial;
77+
78+
impl serial::Write<u8> for Box<dyn SerialPort> {
79+
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
80+
Ok(io::Write::write_all(self, buffer)?)
81+
}
82+
83+
fn flush(&mut self) -> Result<(), Self::Error> {
84+
Ok(io::Write::flush(self)?)
85+
}
5886
}
5987
}

0 commit comments

Comments
 (0)