|
| 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> = T::ReadFuture<'a> where Self: 'a; |
| 22 | + |
| 23 | + fn read<'a>(&'a mut self, read: &'a mut [Word]) -> Self::ReadFuture<'a> { |
| 24 | + T::read(self, read) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/// Write half of a serial interface |
| 29 | +pub trait Write<Word: 'static + Copy = u8>: ErrorType { |
| 30 | + /// Future returned by the `write` method. |
| 31 | + type WriteFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a |
| 32 | + where |
| 33 | + Self: 'a; |
| 34 | + |
| 35 | + /// Writes a single word to the serial interface |
| 36 | + fn write<'a>(&'a mut self, words: &'a [Word]) -> Self::WriteFuture<'a>; |
| 37 | + |
| 38 | + /// Future returned by the `flush` method. |
| 39 | + type FlushFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a |
| 40 | + where |
| 41 | + Self: 'a; |
| 42 | + |
| 43 | + /// Ensures that none of the previously written words are still buffered |
| 44 | + fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a>; |
| 45 | +} |
| 46 | + |
| 47 | +impl<T: Write<Word>, Word: 'static + Copy> Write<Word> for &mut T { |
| 48 | + type WriteFuture<'a> = T::WriteFuture<'a> where Self: 'a; |
| 49 | + |
| 50 | + fn write<'a>(&'a mut self, words: &'a [Word]) -> Self::WriteFuture<'a> { |
| 51 | + T::write(self, words) |
| 52 | + } |
| 53 | + |
| 54 | + type FlushFuture<'a> = T::FlushFuture<'a> where Self: 'a; |
| 55 | + |
| 56 | + fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { |
| 57 | + T::flush(self) |
| 58 | + } |
| 59 | +} |
0 commit comments