Skip to content

Commit b7a6b86

Browse files
committed
async: add Serial
1 parent 9c17bca commit b7a6b86

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

embedded-hal-async/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
#![feature(generic_associated_types)]
1313

1414
pub mod delay;
15+
pub mod serial;

embedded-hal-async/src/serial.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! Serial interface
2+
3+
use core::future::Future;
4+
pub use embedded_hal::serial::{Error, ErrorKind, ErrorType};
5+
6+
/// Read half of a serial interface
7+
///
8+
/// Some serial interfaces support different data sizes (8 bits, 9 bits, etc.);
9+
/// This can be encoded in this trait via the `Word` type parameter.
10+
pub trait Read<Word: 'static + Copy = u8>: ErrorType {
11+
/// Future returned by the `read` method.
12+
type ReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
13+
where
14+
Self: 'a;
15+
16+
/// Reads words from the serial interface into the supplied slice.
17+
fn read<'a>(&'a mut self, read: &'a mut [Word]) -> Self::ReadFuture<'a>;
18+
}
19+
20+
impl<T: Read<Word>, Word: 'static + Copy> Read<Word> for &mut T {
21+
type ReadFuture<'a>
22+
where
23+
Self: 'a,
24+
= T::ReadFuture<'a>;
25+
26+
fn read<'a>(&'a mut self, read: &'a mut [Word]) -> Self::ReadFuture<'a> {
27+
T::read(self, read)
28+
}
29+
}
30+
31+
/// Write half of a serial interface
32+
pub trait Write<Word: 'static + Copy = u8>: ErrorType {
33+
/// Future returned by the `write` method.
34+
type WriteFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
35+
where
36+
Self: 'a;
37+
38+
/// Writes a single word to the serial interface
39+
fn write<'a>(&'a mut self, words: &'a [Word]) -> Self::WriteFuture<'a>;
40+
41+
/// Future returned by the `flush` method.
42+
type FlushFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
43+
where
44+
Self: 'a;
45+
46+
/// Ensures that none of the previously written words are still buffered
47+
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a>;
48+
}
49+
50+
impl<T: Write<Word>, Word: 'static + Copy> Write<Word> for &mut T {
51+
type WriteFuture<'a>
52+
where
53+
Self: 'a,
54+
= T::WriteFuture<'a>;
55+
56+
fn write<'a>(&'a mut self, words: &'a [Word]) -> Self::WriteFuture<'a> {
57+
T::write(self, words)
58+
}
59+
60+
type FlushFuture<'a>
61+
where
62+
Self: 'a,
63+
= T::FlushFuture<'a>;
64+
65+
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
66+
T::flush(self)
67+
}
68+
}

0 commit comments

Comments
 (0)