Skip to content

serial: enforce all traits have the same Error type. #337

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
Jan 12, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- Fixed blanket impl of `DelayUs` not covering the `delay_ms` method.

### Changed
- `serial`: traits now enforce all impls on the same struct have the same `Error` type.

## [v1.0.0-alpha.6] - 2021-11-19

*** This is (also) an alpha release with breaking changes (sorry) ***
Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@
//! // convenience type alias
//! pub type Serial1 = Serial<USART1>;
//!
//! impl hal::serial::nb::Read<u8> for Serial<USART1> {
//! impl hal::serial::ErrorType for Serial<USART1> {
//! type Error = hal::serial::ErrorKind;
//! }
//!
//! impl hal::serial::nb::Read<u8> for Serial<USART1> {
//! fn read(&mut self) -> nb::Result<u8, Self::Error> {
//! // read the status register
//! let isr = self.usart.sr.read();
Expand All @@ -166,8 +168,6 @@
//! }
//!
//! impl hal::serial::nb::Write<u8> for Serial<USART1> {
//! type Error = hal::serial::ErrorKind;
//!
//! fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
//! // Similar to the `read` implementation
//! # Ok(())
Expand Down Expand Up @@ -387,8 +387,10 @@
//! # fn deref_mut(&mut self) -> &mut T { self.0 }
//! # }
//! # struct Serial1;
//! # impl hal::serial::nb::Write<u8> for Serial1 {
//! # impl hal::serial::ErrorType for Serial1 {
//! # type Error = ErrorKind;
//! # }
//! # impl hal::serial::nb::Write<u8> for Serial1 {
//! # fn write(&mut self, _: u8) -> nb::Result<(), Self::Error> { Err(::nb::Error::WouldBlock) }
//! # fn flush(&mut self) -> nb::Result<(), Self::Error> { Err(::nb::Error::WouldBlock) }
//! # }
Expand Down
7 changes: 1 addition & 6 deletions src/serial/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Blocking serial API

/// Write half of a serial interface (blocking variant)
pub trait Write<Word: Copy = u8> {
/// The type of error that can occur when writing
type Error: crate::serial::Error;

pub trait Write<Word: Copy = u8>: super::ErrorType {
/// Writes a slice, blocking until everything has been written
///
/// An implementation can choose to buffer the write, returning `Ok(())`
Expand All @@ -20,8 +17,6 @@ pub trait Write<Word: Copy = u8> {
}

impl<T: Write<Word>, Word: Copy> Write<Word> for &mut T {
type Error = T::Error;

fn write(&mut self, buffer: &[Word]) -> Result<(), Self::Error> {
T::write(self, buffer)
}
Expand Down
12 changes: 12 additions & 0 deletions src/serial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,15 @@ impl core::fmt::Display for ErrorKind {
}
}
}

/// Serial error type trait
///
/// This just defines the error type, to be used by the other traits.
pub trait ErrorType {
/// Error type
type Error: Error;
}

impl<T: ErrorType> ErrorType for &mut T {
type Error = T::Error;
}
14 changes: 2 additions & 12 deletions src/serial/nb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,19 @@
///
/// 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: Copy = u8> {
/// Read error
type Error: crate::serial::Error;

pub trait Read<Word: Copy = u8>: super::ErrorType {
/// Reads a single word from the serial interface
fn read(&mut self) -> nb::Result<Word, Self::Error>;
}

impl<T: Read<Word>, Word: Copy> Read<Word> for &mut T {
type Error = T::Error;

fn read(&mut self) -> nb::Result<Word, Self::Error> {
T::read(self)
}
}

/// Write half of a serial interface
pub trait Write<Word: Copy = u8> {
/// Write error
type Error: crate::serial::Error;

pub trait Write<Word: Copy = u8>: super::ErrorType {
/// Writes a single word to the serial interface
fn write(&mut self, word: Word) -> nb::Result<(), Self::Error>;

Expand All @@ -33,8 +25,6 @@ pub trait Write<Word: Copy = u8> {
}

impl<T: Write<Word>, Word: Copy> Write<Word> for &mut T {
type Error = T::Error;

fn write(&mut self, word: Word) -> nb::Result<(), Self::Error> {
T::write(self, word)
}
Expand Down