Skip to content

Commit 34ae853

Browse files
bors[bot]Dirbaio
andauthored
Merge #337
337: serial: enforce all traits have the same Error type. r=therealprof a=Dirbaio Equivalent of #331 for serial. Co-authored-by: Dario Nieuwenhuis <[email protected]>
2 parents bae5bcb + 66b27e4 commit 34ae853

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1818
### Changed
1919
- `digital`: traits now enforce all impls on the same struct have the same `Error` type.
2020

21+
### Changed
22+
- `serial`: traits now enforce all impls on the same struct have the same `Error` type.
23+
2124
## [v1.0.0-alpha.6] - 2021-11-19
2225

2326
*** This is (also) an alpha release with breaking changes (sorry) ***

src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@
143143
//! // convenience type alias
144144
//! pub type Serial1 = Serial<USART1>;
145145
//!
146-
//! impl hal::serial::nb::Read<u8> for Serial<USART1> {
146+
//! impl hal::serial::ErrorType for Serial<USART1> {
147147
//! type Error = hal::serial::ErrorKind;
148+
//! }
148149
//!
150+
//! impl hal::serial::nb::Read<u8> for Serial<USART1> {
149151
//! fn read(&mut self) -> nb::Result<u8, Self::Error> {
150152
//! // read the status register
151153
//! let isr = self.usart.sr.read();
@@ -166,8 +168,6 @@
166168
//! }
167169
//!
168170
//! impl hal::serial::nb::Write<u8> for Serial<USART1> {
169-
//! type Error = hal::serial::ErrorKind;
170-
//!
171171
//! fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
172172
//! // Similar to the `read` implementation
173173
//! # Ok(())
@@ -387,8 +387,10 @@
387387
//! # fn deref_mut(&mut self) -> &mut T { self.0 }
388388
//! # }
389389
//! # struct Serial1;
390-
//! # impl hal::serial::nb::Write<u8> for Serial1 {
390+
//! # impl hal::serial::ErrorType for Serial1 {
391391
//! # type Error = ErrorKind;
392+
//! # }
393+
//! # impl hal::serial::nb::Write<u8> for Serial1 {
392394
//! # fn write(&mut self, _: u8) -> nb::Result<(), Self::Error> { Err(::nb::Error::WouldBlock) }
393395
//! # fn flush(&mut self) -> nb::Result<(), Self::Error> { Err(::nb::Error::WouldBlock) }
394396
//! # }

src/serial/blocking.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//! Blocking serial API
22
33
/// Write half of a serial interface (blocking variant)
4-
pub trait Write<Word: Copy = u8> {
5-
/// The type of error that can occur when writing
6-
type Error: crate::serial::Error;
7-
4+
pub trait Write<Word: Copy = u8>: super::ErrorType {
85
/// Writes a slice, blocking until everything has been written
96
///
107
/// An implementation can choose to buffer the write, returning `Ok(())`
@@ -20,8 +17,6 @@ pub trait Write<Word: Copy = u8> {
2017
}
2118

2219
impl<T: Write<Word>, Word: Copy> Write<Word> for &mut T {
23-
type Error = T::Error;
24-
2520
fn write(&mut self, buffer: &[Word]) -> Result<(), Self::Error> {
2621
T::write(self, buffer)
2722
}

src/serial/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ impl core::fmt::Display for ErrorKind {
6363
}
6464
}
6565
}
66+
67+
/// Serial error type trait
68+
///
69+
/// This just defines the error type, to be used by the other traits.
70+
pub trait ErrorType {
71+
/// Error type
72+
type Error: Error;
73+
}
74+
75+
impl<T: ErrorType> ErrorType for &mut T {
76+
type Error = T::Error;
77+
}

src/serial/nb.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,19 @@
44
///
55
/// Some serial interfaces support different data sizes (8 bits, 9 bits, etc.);
66
/// This can be encoded in this trait via the `Word` type parameter.
7-
pub trait Read<Word: Copy = u8> {
8-
/// Read error
9-
type Error: crate::serial::Error;
10-
7+
pub trait Read<Word: Copy = u8>: super::ErrorType {
118
/// Reads a single word from the serial interface
129
fn read(&mut self) -> nb::Result<Word, Self::Error>;
1310
}
1411

1512
impl<T: Read<Word>, Word: Copy> Read<Word> for &mut T {
16-
type Error = T::Error;
17-
1813
fn read(&mut self) -> nb::Result<Word, Self::Error> {
1914
T::read(self)
2015
}
2116
}
2217

2318
/// Write half of a serial interface
24-
pub trait Write<Word: Copy = u8> {
25-
/// Write error
26-
type Error: crate::serial::Error;
27-
19+
pub trait Write<Word: Copy = u8>: super::ErrorType {
2820
/// Writes a single word to the serial interface
2921
fn write(&mut self, word: Word) -> nb::Result<(), Self::Error>;
3022

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

3527
impl<T: Write<Word>, Word: Copy> Write<Word> for &mut T {
36-
type Error = T::Error;
37-
3828
fn write(&mut self, word: Word) -> nb::Result<(), Self::Error> {
3929
T::write(self, word)
4030
}

0 commit comments

Comments
 (0)