4
4
5
5
use std:: io;
6
6
7
- use embedded_hal_nb :: serial;
7
+ use embedded_hal :: serial:: { ErrorType , ErrorKind } ;
8
8
9
9
use crate :: SerialPort ;
10
10
@@ -13,47 +13,75 @@ pub struct SerialError {
13
13
kind : io:: ErrorKind ,
14
14
}
15
15
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 {
19
18
#[ allow( clippy:: match_single_binding) ]
20
19
match self . kind {
21
- _other => serial :: ErrorKind :: Other ,
20
+ _ => ErrorKind :: Other ,
22
21
}
23
22
}
24
23
}
25
24
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
+ }
30
30
}
31
31
}
32
32
33
- impl serial :: ErrorType for Box < dyn SerialPort > {
33
+ impl ErrorType for Box < dyn SerialPort > {
34
34
type Error = SerialError ;
35
35
}
36
36
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 } ) ,
45
46
}
46
47
}
47
- }
48
48
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
+ }
54
59
}
55
60
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
+ }
58
86
}
59
87
}
0 commit comments