Skip to content

Commit

Permalink
async: add Serial
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Jan 12, 2022
1 parent 9c17bca commit b7a6b86
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions embedded-hal-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
#![feature(generic_associated_types)]

pub mod delay;
pub mod serial;
68 changes: 68 additions & 0 deletions embedded-hal-async/src/serial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! Serial interface

use core::future::Future;
pub use embedded_hal::serial::{Error, ErrorKind, ErrorType};

/// Read half of a serial interface
///
/// Some serial interfaces support different data sizes (8 bits, 9 bits, etc.);
/// This can be encoded in this trait via the `Word` type parameter.
pub trait Read<Word: 'static + Copy = u8>: ErrorType {
/// Future returned by the `read` method.
type ReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a;

/// Reads words from the serial interface into the supplied slice.
fn read<'a>(&'a mut self, read: &'a mut [Word]) -> Self::ReadFuture<'a>;
}

impl<T: Read<Word>, Word: 'static + Copy> Read<Word> for &mut T {
type ReadFuture<'a>
where
Self: 'a,
= T::ReadFuture<'a>;

fn read<'a>(&'a mut self, read: &'a mut [Word]) -> Self::ReadFuture<'a> {
T::read(self, read)
}
}

/// Write half of a serial interface
pub trait Write<Word: 'static + Copy = u8>: ErrorType {
/// Future returned by the `write` method.
type WriteFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a;

/// Writes a single word to the serial interface
fn write<'a>(&'a mut self, words: &'a [Word]) -> Self::WriteFuture<'a>;

/// Future returned by the `flush` method.
type FlushFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a;

/// Ensures that none of the previously written words are still buffered
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a>;
}

impl<T: Write<Word>, Word: 'static + Copy> Write<Word> for &mut T {
type WriteFuture<'a>
where
Self: 'a,
= T::WriteFuture<'a>;

fn write<'a>(&'a mut self, words: &'a [Word]) -> Self::WriteFuture<'a> {
T::write(self, words)
}

type FlushFuture<'a>
where
Self: 'a,
= T::FlushFuture<'a>;

fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
T::flush(self)
}
}

0 comments on commit b7a6b86

Please sign in to comment.