diff --git a/CHANGELOG.md b/CHANGELOG.md index 177bf3908..199957eb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) *** diff --git a/src/lib.rs b/src/lib.rs index 3e559ee13..f3fee473b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,9 +143,11 @@ //! // convenience type alias //! pub type Serial1 = Serial; //! -//! impl hal::serial::nb::Read for Serial { +//! impl hal::serial::ErrorType for Serial { //! type Error = hal::serial::ErrorKind; +//! } //! +//! impl hal::serial::nb::Read for Serial { //! fn read(&mut self) -> nb::Result { //! // read the status register //! let isr = self.usart.sr.read(); @@ -166,8 +168,6 @@ //! } //! //! impl hal::serial::nb::Write for Serial { -//! type Error = hal::serial::ErrorKind; -//! //! fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> { //! // Similar to the `read` implementation //! # Ok(()) @@ -387,8 +387,10 @@ //! # fn deref_mut(&mut self) -> &mut T { self.0 } //! # } //! # struct Serial1; -//! # impl hal::serial::nb::Write for Serial1 { +//! # impl hal::serial::ErrorType for Serial1 { //! # type Error = ErrorKind; +//! # } +//! # impl hal::serial::nb::Write 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) } //! # } diff --git a/src/serial/blocking.rs b/src/serial/blocking.rs index 7b2b8d752..c5cfad4f8 100644 --- a/src/serial/blocking.rs +++ b/src/serial/blocking.rs @@ -1,10 +1,7 @@ //! Blocking serial API /// Write half of a serial interface (blocking variant) -pub trait Write { - /// The type of error that can occur when writing - type Error: crate::serial::Error; - +pub trait Write: super::ErrorType { /// Writes a slice, blocking until everything has been written /// /// An implementation can choose to buffer the write, returning `Ok(())` @@ -20,8 +17,6 @@ pub trait Write { } impl, Word: Copy> Write for &mut T { - type Error = T::Error; - fn write(&mut self, buffer: &[Word]) -> Result<(), Self::Error> { T::write(self, buffer) } diff --git a/src/serial/mod.rs b/src/serial/mod.rs index c74964483..1dd4b23b5 100644 --- a/src/serial/mod.rs +++ b/src/serial/mod.rs @@ -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 ErrorType for &mut T { + type Error = T::Error; +} diff --git a/src/serial/nb.rs b/src/serial/nb.rs index df40f2bb5..dc2dde74e 100644 --- a/src/serial/nb.rs +++ b/src/serial/nb.rs @@ -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 { - /// Read error - type Error: crate::serial::Error; - +pub trait Read: super::ErrorType { /// Reads a single word from the serial interface fn read(&mut self) -> nb::Result; } impl, Word: Copy> Read for &mut T { - type Error = T::Error; - fn read(&mut self) -> nb::Result { T::read(self) } } /// Write half of a serial interface -pub trait Write { - /// Write error - type Error: crate::serial::Error; - +pub trait Write: super::ErrorType { /// Writes a single word to the serial interface fn write(&mut self, word: Word) -> nb::Result<(), Self::Error>; @@ -33,8 +25,6 @@ pub trait Write { } impl, Word: Copy> Write for &mut T { - type Error = T::Error; - fn write(&mut self, word: Word) -> nb::Result<(), Self::Error> { T::write(self, word) }