diff --git a/embedded-hal-async/src/lib.rs b/embedded-hal-async/src/lib.rs index 14748349c..cae21624a 100644 --- a/embedded-hal-async/src/lib.rs +++ b/embedded-hal-async/src/lib.rs @@ -15,4 +15,5 @@ pub mod delay; pub mod digital; pub mod i2c; +pub mod serial; pub mod spi; diff --git a/embedded-hal-async/src/serial.rs b/embedded-hal-async/src/serial.rs new file mode 100644 index 000000000..60ee1bc05 --- /dev/null +++ b/embedded-hal-async/src/serial.rs @@ -0,0 +1,27 @@ +//! Serial interface + +pub use embedded_hal::serial::{Error, ErrorKind, ErrorType}; + +/// Write half of a serial interface +pub trait Write: ErrorType { + /// Writes a slice, blocking until everything has been written. + /// + /// An implementation can choose to buffer the write, returning `Ok(())` + /// after the complete slice has been written to a buffer, but before all + /// words have been sent via the serial interface. To make sure that + /// everything has been sent, call [`flush`](Write::flush) after this function returns. + async fn write(&mut self, words: &[Word]) -> Result<(), Self::Error>; + + /// Ensures that none of the previously written data is still buffered + async fn flush(&mut self) -> Result<(), Self::Error>; +} + +impl, Word: 'static + Copy> Write for &mut T { + async fn write(&mut self, words: &[Word]) -> Result<(), Self::Error> { + T::write(self, words).await + } + + async fn flush(&mut self) -> Result<(), Self::Error> { + T::flush(self).await + } +} diff --git a/embedded-hal/src/serial.rs b/embedded-hal/src/serial.rs index f0c05f98b..ab9be722b 100644 --- a/embedded-hal/src/serial.rs +++ b/embedded-hal/src/serial.rs @@ -80,9 +80,7 @@ pub trait Write: ErrorType { /// An implementation can choose to buffer the write, returning `Ok(())` /// after the complete slice has been written to a buffer, but before all /// words have been sent via the serial interface. To make sure that - /// everything has been sent, call [`flush`] after this function returns. - /// - /// [`flush`]: #tymethod.flush + /// everything has been sent, call [`flush`](Write::flush) after this function returns. fn write(&mut self, buffer: &[Word]) -> Result<(), Self::Error>; /// Block until the serial interface has sent all buffered words