Skip to content

Commit baaaaa1

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

File tree

2 files changed

+25
-48
lines changed

2 files changed

+25
-48
lines changed

CHANGELOG.md

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

24+
### Changed
25+
- `i2c`: traits now enforce all impls on the same struct have the same `Error` type.
26+
2427
## [v1.0.0-alpha.6] - 2021-11-19
2528

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

src/i2c.rs

+22-48
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
//! Here is an example of an embedded-hal implementation of the `Write` trait
2323
//! for both modes:
2424
//! ```
25-
//! # use embedded_hal::i2c::{ErrorKind, SevenBitAddress, TenBitAddress, blocking::Write};
25+
//! # use embedded_hal::i2c::{ErrorKind, ErrorType, SevenBitAddress, TenBitAddress, blocking::Write};
2626
//! /// I2C0 hardware peripheral which supports both 7-bit and 10-bit addressing.
2727
//! pub struct I2c0;
2828
//!
29+
//! # impl ErrorType for I2c0 { type Error = ErrorKind; }
2930
//! impl Write<SevenBitAddress> for I2c0
3031
//! {
31-
//! # type Error = ErrorKind;
32-
//! #
3332
//! fn write(&mut self, addr: u8, output: &[u8]) -> Result<(), Self::Error> {
3433
//! // ...
3534
//! # Ok(())
@@ -38,8 +37,6 @@
3837
//!
3938
//! impl Write<TenBitAddress> for I2c0
4039
//! {
41-
//! # type Error = ErrorKind;
42-
//! #
4340
//! fn write(&mut self, addr: u16, output: &[u8]) -> Result<(), Self::Error> {
4441
//! // ...
4542
//! # Ok(())
@@ -184,6 +181,18 @@ impl core::fmt::Display for NoAcknowledgeSource {
184181
}
185182
}
186183

184+
/// I2C error type trait
185+
///
186+
/// This just defines the error type, to be used by the other traits.
187+
pub trait ErrorType {
188+
/// Error type
189+
type Error: Error;
190+
}
191+
192+
impl<T: ErrorType> ErrorType for &mut T {
193+
type Error = T::Error;
194+
}
195+
187196
/// Address mode (7-bit / 10-bit)
188197
///
189198
/// Note: This trait is sealed and should not be implemented outside of this crate.
@@ -202,13 +211,10 @@ impl AddressMode for TenBitAddress {}
202211
/// Blocking I2C traits
203212
pub mod blocking {
204213

205-
use super::{AddressMode, Error, SevenBitAddress};
214+
use super::{AddressMode, ErrorType, SevenBitAddress};
206215

207216
/// Blocking read
208-
pub trait Read<A: AddressMode = SevenBitAddress> {
209-
/// Error type
210-
type Error: Error;
211-
217+
pub trait Read<A: AddressMode = SevenBitAddress>: ErrorType {
212218
/// Reads enough bytes from slave with `address` to fill `buffer`
213219
///
214220
/// # I2C Events (contract)
@@ -231,18 +237,13 @@ pub mod blocking {
231237
}
232238

233239
impl<A: AddressMode, T: Read<A>> Read<A> for &mut T {
234-
type Error = T::Error;
235-
236240
fn read(&mut self, address: A, buffer: &mut [u8]) -> Result<(), Self::Error> {
237241
T::read(self, address, buffer)
238242
}
239243
}
240244

241245
/// Blocking write
242-
pub trait Write<A: AddressMode = SevenBitAddress> {
243-
/// Error type
244-
type Error: Error;
245-
246+
pub trait Write<A: AddressMode = SevenBitAddress>: ErrorType {
246247
/// Writes bytes to slave with address `address`
247248
///
248249
/// # I2C Events (contract)
@@ -263,18 +264,13 @@ pub mod blocking {
263264
}
264265

265266
impl<A: AddressMode, T: Write<A>> Write<A> for &mut T {
266-
type Error = T::Error;
267-
268267
fn write(&mut self, address: A, bytes: &[u8]) -> Result<(), Self::Error> {
269268
T::write(self, address, bytes)
270269
}
271270
}
272271

273272
/// Blocking write (iterator version)
274-
pub trait WriteIter<A: AddressMode = SevenBitAddress> {
275-
/// Error type
276-
type Error: Error;
277-
273+
pub trait WriteIter<A: AddressMode = SevenBitAddress>: ErrorType {
278274
/// Writes bytes to slave with address `address`
279275
///
280276
/// # I2C Events (contract)
@@ -286,8 +282,6 @@ pub mod blocking {
286282
}
287283

288284
impl<A: AddressMode, T: WriteIter<A>> WriteIter<A> for &mut T {
289-
type Error = T::Error;
290-
291285
fn write_iter<B>(&mut self, address: A, bytes: B) -> Result<(), Self::Error>
292286
where
293287
B: IntoIterator<Item = u8>,
@@ -297,10 +291,7 @@ pub mod blocking {
297291
}
298292

299293
/// Blocking write + read
300-
pub trait WriteRead<A: AddressMode = SevenBitAddress> {
301-
/// Error type
302-
type Error: Error;
303-
294+
pub trait WriteRead<A: AddressMode = SevenBitAddress>: ErrorType {
304295
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
305296
/// single transaction*
306297
///
@@ -332,8 +323,6 @@ pub mod blocking {
332323
}
333324

334325
impl<A: AddressMode, T: WriteRead<A>> WriteRead<A> for &mut T {
335-
type Error = T::Error;
336-
337326
fn write_read(
338327
&mut self,
339328
address: A,
@@ -345,10 +334,7 @@ pub mod blocking {
345334
}
346335

347336
/// Blocking write (iterator version) + read
348-
pub trait WriteIterRead<A: AddressMode = SevenBitAddress> {
349-
/// Error type
350-
type Error: Error;
351-
337+
pub trait WriteIterRead<A: AddressMode = SevenBitAddress>: ErrorType {
352338
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
353339
/// single transaction*
354340
///
@@ -366,8 +352,6 @@ pub mod blocking {
366352
}
367353

368354
impl<A: AddressMode, T: WriteIterRead<A>> WriteIterRead<A> for &mut T {
369-
type Error = T::Error;
370-
371355
fn write_iter_read<B>(
372356
&mut self,
373357
address: A,
@@ -395,10 +379,7 @@ pub mod blocking {
395379
/// Transactional I2C interface.
396380
///
397381
/// This allows combining operations within an I2C transaction.
398-
pub trait Transactional<A: AddressMode = SevenBitAddress> {
399-
/// Error type
400-
type Error: Error;
401-
382+
pub trait Transactional<A: AddressMode = SevenBitAddress>: ErrorType {
402383
/// Execute the provided operations on the I2C bus.
403384
///
404385
/// Transaction contract:
@@ -420,8 +401,6 @@ pub mod blocking {
420401
}
421402

422403
impl<A: AddressMode, T: Transactional<A>> Transactional<A> for &mut T {
423-
type Error = T::Error;
424-
425404
fn exec<'a>(
426405
&mut self,
427406
address: A,
@@ -434,10 +413,7 @@ pub mod blocking {
434413
/// Transactional I2C interface (iterator version).
435414
///
436415
/// This allows combining operation within an I2C transaction.
437-
pub trait TransactionalIter<A: AddressMode = SevenBitAddress> {
438-
/// Error type
439-
type Error: Error;
440-
416+
pub trait TransactionalIter<A: AddressMode = SevenBitAddress>: ErrorType {
441417
/// Execute the provided operations on the I2C bus (iterator version).
442418
///
443419
/// Transaction contract:
@@ -457,8 +433,6 @@ pub mod blocking {
457433
}
458434

459435
impl<A: AddressMode, T: TransactionalIter<A>> TransactionalIter<A> for &mut T {
460-
type Error = T::Error;
461-
462436
fn exec_iter<'a, O>(&mut self, address: A, operations: O) -> Result<(), Self::Error>
463437
where
464438
O: IntoIterator<Item = Operation<'a>>,

0 commit comments

Comments
 (0)