Skip to content

Commit fb9c1bc

Browse files
AerialXhomu
authored andcommitted
core: Error associated type bounds
Pull request: #11 Approved by: posborne
1 parent cb450c2 commit fb9c1bc

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

src/core.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
// except according to those terms.
88

99
use byteorder::{ByteOrder, LittleEndian};
10+
use std::error::Error;
1011

1112
/// Interface to an I2C Slave Device from an I2C Master
1213
///
1314
/// Typical implementations will store state with references to the bus
1415
/// in use and the address of the slave device. The trait is based on the
1516
/// Linux i2cdev interface.
1617
pub trait I2CDevice {
17-
type Error;
18+
type Error: Error;
1819

1920
/// Read data from the device to fill the provided slice
2021
fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error>;

src/linux.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use ffi;
1010
use core::I2CDevice;
11+
use std::error::Error;
1112
use std::path::Path;
1213
use std::fs::File;
1314
use std::fmt;
@@ -22,6 +23,7 @@ pub struct LinuxI2CDevice {
2223
slave_address: u16,
2324
}
2425

26+
#[derive(Debug)]
2527
pub enum LinuxI2CError {
2628
Nix(nix::Error),
2729
Io(io::Error),
@@ -55,11 +57,27 @@ impl From<LinuxI2CError> for io::Error {
5557
}
5658
}
5759

58-
impl fmt::Debug for LinuxI2CError {
60+
impl fmt::Display for LinuxI2CError {
5961
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6062
match *self {
61-
LinuxI2CError::Nix(ref e) => fmt::Debug::fmt(e, f),
62-
LinuxI2CError::Io(ref e) => fmt::Debug::fmt(e, f),
63+
LinuxI2CError::Nix(ref e) => fmt::Display::fmt(e, f),
64+
LinuxI2CError::Io(ref e) => fmt::Display::fmt(e, f),
65+
}
66+
}
67+
}
68+
69+
impl Error for LinuxI2CError {
70+
fn description(&self) -> &str {
71+
match *self {
72+
LinuxI2CError::Io(ref e) => e.description(),
73+
LinuxI2CError::Nix(ref e) => e.description(),
74+
}
75+
}
76+
77+
fn cause(&self) -> Option<&Error> {
78+
match *self {
79+
LinuxI2CError::Io(ref e) => Some(e),
80+
LinuxI2CError::Nix(ref e) => Some(e),
6381
}
6482
}
6583
}

src/sensors/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9+
use std::error::Error;
10+
911
pub mod adxl345_accelerometer;
1012
pub mod mpl115a2_barometer;
1113
pub mod nunchuck;
@@ -23,15 +25,15 @@ pub struct AccelerometerSample {
2325

2426
/// Trait for sensors that provide access to accelerometer readings (3-axis)
2527
pub trait Accelerometer {
26-
type Error;
28+
type Error: Error;
2729

2830
/// Grab an accelerometer sample from the device
2931
fn accelerometer_sample(&mut self) -> Result<AccelerometerSample, Self::Error>;
3032
}
3133

3234
/// Trait for sensors that provide access to temperature readings
3335
pub trait Thermometer {
34-
type Error;
36+
type Error: Error;
3537

3638
/// Get na temperature from the sensor in degrees celsisus
3739
///
@@ -42,7 +44,7 @@ pub trait Thermometer {
4244

4345
/// Trait for sensors that provide access to pressure readings
4446
pub trait Barometer {
45-
type Error;
47+
type Error: Error;
4648

4749
/// Get a pressure reading from the sensor in kPa
4850
///

src/sensors/mpl115a2_barometer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl MPL115A2Coefficients {
8181
/// This should be built from a read of registers 0x04-0x0B in
8282
/// order. This gets the raw, unconverted value of each
8383
/// coefficient.
84-
pub fn new<E>(i2cdev: &mut I2CDevice<Error = E>) -> Result<MPL115A2Coefficients, E> {
84+
pub fn new<E: Error>(i2cdev: &mut I2CDevice<Error=E>) -> Result<MPL115A2Coefficients, E> {
8585
let mut buf: [u8; 8] = [0; 8];
8686
try!(i2cdev.write(&[REGISTER_ADDR_A0]));
8787
try!(i2cdev.read(&mut buf));
@@ -97,7 +97,7 @@ impl MPL115A2Coefficients {
9797

9898
impl MPL115A2RawReading {
9999
/// Create a new reading from the provided I2C Device
100-
pub fn new<E>(i2cdev: &mut I2CDevice<Error = E>) -> Result<MPL115A2RawReading, E> {
100+
pub fn new<E: Error>(i2cdev: &mut I2CDevice<Error=E>) -> Result<MPL115A2RawReading, E> {
101101
// tell the chip to do an ADC read so we can get updated values
102102
try!(i2cdev.smbus_write_byte_data(REGISTER_ADDR_START_CONVERSION, 0x00));
103103

src/sensors/nunchuck.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
// Reads data from Wii Nunchuck
1010

1111
use std::io::prelude::*;
12+
use std::error::Error;
1213
use std::thread;
14+
use std::fmt;
1315

1416
use core::I2CDevice;
1517

@@ -21,6 +23,31 @@ pub enum NunchuckError<E> {
2123
ParseError,
2224
}
2325

26+
impl<E: Error> fmt::Display for NunchuckError<E> {
27+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28+
match *self {
29+
NunchuckError::Error(ref e) => fmt::Display::fmt(e, f),
30+
NunchuckError::ParseError => fmt::Display::fmt(self.description(), f),
31+
}
32+
}
33+
}
34+
35+
impl<E: Error> Error for NunchuckError<E> {
36+
fn description(&self) -> &str {
37+
match *self {
38+
NunchuckError::Error(ref e) => e.description(),
39+
NunchuckError::ParseError => "Unable to Parse Data",
40+
}
41+
}
42+
43+
fn cause(&self) -> Option<&Error> {
44+
match *self {
45+
NunchuckError::Error(ref e) => Some(e),
46+
NunchuckError::ParseError => None,
47+
}
48+
}
49+
}
50+
2451
// TODO: Move Nunchuck code out to be an actual sensor and add tests
2552

2653
#[derive(Debug)]

0 commit comments

Comments
 (0)