44
55use std:: io;
66
7- use embedded_hal_nb :: serial;
7+ use embedded_hal :: serial:: { ErrorType , ErrorKind } ;
88
99use 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