Skip to content

async/serial: add Write. #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions embedded-hal-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
pub mod delay;
pub mod digital;
pub mod i2c;
pub mod serial;
pub mod spi;
27 changes: 27 additions & 0 deletions embedded-hal-async/src/serial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Serial interface

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

/// Write half of a serial interface
pub trait Write<Word: 'static + Copy = u8>: 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<T: Write<Word>, Word: 'static + Copy> Write<Word> 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
}
}
4 changes: 1 addition & 3 deletions embedded-hal/src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ pub trait Write<Word: Copy = u8>: 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
Expand Down