22
22
//! Here is an example of an embedded-hal implementation of the `Write` trait
23
23
//! for both modes:
24
24
//! ```
25
- //! # use embedded_hal::i2c::{ErrorKind, SevenBitAddress, TenBitAddress, blocking::Write};
25
+ //! # use embedded_hal::i2c::{ErrorKind, ErrorType, SevenBitAddress, TenBitAddress, blocking::Write};
26
26
//! /// I2C0 hardware peripheral which supports both 7-bit and 10-bit addressing.
27
27
//! pub struct I2c0;
28
28
//!
29
+ //! # impl ErrorType for I2c0 { type Error = ErrorKind; }
29
30
//! impl Write<SevenBitAddress> for I2c0
30
31
//! {
31
- //! # type Error = ErrorKind;
32
- //! #
33
32
//! fn write(&mut self, addr: u8, output: &[u8]) -> Result<(), Self::Error> {
34
33
//! // ...
35
34
//! # Ok(())
38
37
//!
39
38
//! impl Write<TenBitAddress> for I2c0
40
39
//! {
41
- //! # type Error = ErrorKind;
42
- //! #
43
40
//! fn write(&mut self, addr: u16, output: &[u8]) -> Result<(), Self::Error> {
44
41
//! // ...
45
42
//! # Ok(())
@@ -184,6 +181,18 @@ impl core::fmt::Display for NoAcknowledgeSource {
184
181
}
185
182
}
186
183
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
+
187
196
/// Address mode (7-bit / 10-bit)
188
197
///
189
198
/// Note: This trait is sealed and should not be implemented outside of this crate.
@@ -202,13 +211,10 @@ impl AddressMode for TenBitAddress {}
202
211
/// Blocking I2C traits
203
212
pub mod blocking {
204
213
205
- use super :: { AddressMode , Error , SevenBitAddress } ;
214
+ use super :: { AddressMode , ErrorType , SevenBitAddress } ;
206
215
207
216
/// 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 {
212
218
/// Reads enough bytes from slave with `address` to fill `buffer`
213
219
///
214
220
/// # I2C Events (contract)
@@ -231,18 +237,13 @@ pub mod blocking {
231
237
}
232
238
233
239
impl < A : AddressMode , T : Read < A > > Read < A > for & mut T {
234
- type Error = T :: Error ;
235
-
236
240
fn read ( & mut self , address : A , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
237
241
T :: read ( self , address, buffer)
238
242
}
239
243
}
240
244
241
245
/// 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 {
246
247
/// Writes bytes to slave with address `address`
247
248
///
248
249
/// # I2C Events (contract)
@@ -263,18 +264,13 @@ pub mod blocking {
263
264
}
264
265
265
266
impl < A : AddressMode , T : Write < A > > Write < A > for & mut T {
266
- type Error = T :: Error ;
267
-
268
267
fn write ( & mut self , address : A , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
269
268
T :: write ( self , address, bytes)
270
269
}
271
270
}
272
271
273
272
/// 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 {
278
274
/// Writes bytes to slave with address `address`
279
275
///
280
276
/// # I2C Events (contract)
@@ -286,8 +282,6 @@ pub mod blocking {
286
282
}
287
283
288
284
impl < A : AddressMode , T : WriteIter < A > > WriteIter < A > for & mut T {
289
- type Error = T :: Error ;
290
-
291
285
fn write_iter < B > ( & mut self , address : A , bytes : B ) -> Result < ( ) , Self :: Error >
292
286
where
293
287
B : IntoIterator < Item = u8 > ,
@@ -297,10 +291,7 @@ pub mod blocking {
297
291
}
298
292
299
293
/// 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 {
304
295
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
305
296
/// single transaction*
306
297
///
@@ -332,8 +323,6 @@ pub mod blocking {
332
323
}
333
324
334
325
impl < A : AddressMode , T : WriteRead < A > > WriteRead < A > for & mut T {
335
- type Error = T :: Error ;
336
-
337
326
fn write_read (
338
327
& mut self ,
339
328
address : A ,
@@ -345,10 +334,7 @@ pub mod blocking {
345
334
}
346
335
347
336
/// 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 {
352
338
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
353
339
/// single transaction*
354
340
///
@@ -366,8 +352,6 @@ pub mod blocking {
366
352
}
367
353
368
354
impl < A : AddressMode , T : WriteIterRead < A > > WriteIterRead < A > for & mut T {
369
- type Error = T :: Error ;
370
-
371
355
fn write_iter_read < B > (
372
356
& mut self ,
373
357
address : A ,
@@ -395,10 +379,7 @@ pub mod blocking {
395
379
/// Transactional I2C interface.
396
380
///
397
381
/// 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 {
402
383
/// Execute the provided operations on the I2C bus.
403
384
///
404
385
/// Transaction contract:
@@ -420,8 +401,6 @@ pub mod blocking {
420
401
}
421
402
422
403
impl < A : AddressMode , T : Transactional < A > > Transactional < A > for & mut T {
423
- type Error = T :: Error ;
424
-
425
404
fn exec < ' a > (
426
405
& mut self ,
427
406
address : A ,
@@ -434,10 +413,7 @@ pub mod blocking {
434
413
/// Transactional I2C interface (iterator version).
435
414
///
436
415
/// 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 {
441
417
/// Execute the provided operations on the I2C bus (iterator version).
442
418
///
443
419
/// Transaction contract:
@@ -457,8 +433,6 @@ pub mod blocking {
457
433
}
458
434
459
435
impl < A : AddressMode , T : TransactionalIter < A > > TransactionalIter < A > for & mut T {
460
- type Error = T :: Error ;
461
-
462
436
fn exec_iter < ' a , O > ( & mut self , address : A , operations : O ) -> Result < ( ) , Self :: Error >
463
437
where
464
438
O : IntoIterator < Item = Operation < ' a > > ,
0 commit comments