Skip to content

Commit

Permalink
Merge pull request rust-embedded#43 from RandomInsano/updating_try_macro
Browse files Browse the repository at this point in the history
Move from `try!` to `?`
  • Loading branch information
posborne authored Sep 29, 2018
2 parents 1645280 + d7c8aaa commit e2dd1bf
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 70 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ const NUNCHUCK_SLAVE_ADDR: u16 = 0x52;
// real code should probably not use unwrap()
fn i2cfun() -> Result<(), LinuxI2CError> {
let mut dev = try!(LinuxI2CDevice::new("/dev/i2c-1", NUNCHUCK_SLAVE_ADDR));
let mut dev = LinuxI2CDevice::new("/dev/i2c-1", NUNCHUCK_SLAVE_ADDR)?;
// init sequence
try!(dev.smbus_write_byte_data(0xF0, 0x55));
try!(dev.smbus_write_byte_data(0xFB, 0x00));
dev.smbus_write_byte_data(0xF0, 0x55)?;
dev.smbus_write_byte_data(0xFB, 0x00)?;
thread::sleep(Duration::from_millis(100));
loop {
Expand Down
8 changes: 4 additions & 4 deletions examples/nunchuck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mod nunchuck {
/// the future.
pub fn new(i2cdev: T) -> Result<Nunchuck<T>, T::Error> {
let mut nunchuck = Nunchuck { i2cdev: i2cdev };
try!(nunchuck.init());
nunchuck.init()?;
Ok(nunchuck)
}

Expand All @@ -121,8 +121,8 @@ mod nunchuck {
// These registers must be written; the documentation is a bit
// lacking but it appears this is some kind of handshake to
// perform unencrypted data tranfers
try!(self.i2cdev.smbus_write_byte_data(0xF0, 0x55));
try!(self.i2cdev.smbus_write_byte_data(0xFB, 0x00));
self.i2cdev.smbus_write_byte_data(0xF0, 0x55)?;
self.i2cdev.smbus_write_byte_data(0xFB, 0x00)?;
Ok(())
}

Expand All @@ -138,7 +138,7 @@ mod nunchuck {

// now, read it!
thread::sleep(Duration::from_millis(10));
try!(self.i2cdev.read(&mut buf).map_err(NunchuckError::Error));
self.i2cdev.read(&mut buf).map_err(NunchuckError::Error)?;
NunchuckReading::from_data(&buf).ok_or(NunchuckError::ParseError)
}
}
Expand Down
34 changes: 17 additions & 17 deletions examples/sensors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mod sensors {
type Error = <Self as Barometer>::Error;

fn altitude_meters(&mut self, sea_level_kpa: f32) -> Result<f32, Self::Error> {
let pressure = try!(self.pressure_kpa()) * 1000.;
let pressure = self.pressure_kpa()? * 1000.;
let sea_level_pa = sea_level_kpa * 1000.;

let altitude = 44330. * (1. - (pressure / sea_level_pa).powf(0.1903));
Expand Down Expand Up @@ -170,7 +170,7 @@ mod sensors {
/// address (dependent on `ALT ADDRESS` pin)
pub fn new(mut i2cdev: T) -> Result<ADXL345Accelerometer<T>, T::Error> {
// setup standy mode to configure
try!(i2cdev.smbus_write_byte_data(REGISTER_POWER_CTL, 0x00));
i2cdev.smbus_write_byte_data(REGISTER_POWER_CTL, 0x00)?;

// configure some defaults
try!(
Expand All @@ -179,13 +179,13 @@ mod sensors {
ADXL345DataRate::RATE_1600HZ as u8
)
);
try!(i2cdev.smbus_write_byte_data(REGISTER_DATA_FORMAT, 0x08));
try!(i2cdev.smbus_write_byte_data(REGISTER_OFSX, 0xFD));
try!(i2cdev.smbus_write_byte_data(REGISTER_OFSY, 0x03));
try!(i2cdev.smbus_write_byte_data(REGISTER_OFSZ, 0xFE));
i2cdev.smbus_write_byte_data(REGISTER_DATA_FORMAT, 0x08)?;
i2cdev.smbus_write_byte_data(REGISTER_OFSX, 0xFD)?;
i2cdev.smbus_write_byte_data(REGISTER_OFSY, 0x03)?;
i2cdev.smbus_write_byte_data(REGISTER_OFSZ, 0xFE)?;

// put device in measurement mode
try!(i2cdev.smbus_write_byte_data(REGISTER_POWER_CTL, 0x08));
i2cdev.smbus_write_byte_data(REGISTER_POWER_CTL, 0x08)?;

Ok(ADXL345Accelerometer { i2cdev: i2cdev })
}
Expand All @@ -209,8 +209,8 @@ mod sensors {
// datasheet recommends multi-byte read to avoid reading
// an inconsistent set of data
let mut buf: [u8; 6] = [0u8; 6];
try!(self.i2cdev.write(&[REGISTER_X0]));
try!(self.i2cdev.read(&mut buf));
self.i2cdev.write(&[REGISTER_X0])?;
self.i2cdev.read(&mut buf)?;

let x: i16 = LittleEndian::read_i16(&[buf[0], buf[1]]);
let y: i16 = LittleEndian::read_i16(&[buf[2], buf[3]]);
Expand Down Expand Up @@ -304,8 +304,8 @@ mod sensors {
i2cdev: &mut I2CDevice<Error = E>,
) -> Result<MPL115A2Coefficients, E> {
let mut buf: [u8; 8] = [0; 8];
try!(i2cdev.write(&[REGISTER_ADDR_A0]));
try!(i2cdev.read(&mut buf));
i2cdev.write(&[REGISTER_ADDR_A0])?;
i2cdev.read(&mut buf)?;
Ok(MPL115A2Coefficients {
a0: calc_coefficient(buf[0], buf[1], 12, 3, 0),
b1: calc_coefficient(buf[2], buf[3], 2, 13, 0),
Expand All @@ -321,16 +321,16 @@ mod sensors {
i2cdev: &mut I2CDevice<Error = E>,
) -> Result<MPL115A2RawReading, E> {
// tell the chip to do an ADC read so we can get updated values
try!(i2cdev.smbus_write_byte_data(REGISTER_ADDR_START_CONVERSION, 0x00));
i2cdev.smbus_write_byte_data(REGISTER_ADDR_START_CONVERSION, 0x00)?;

// maximum conversion time is 3ms
thread::sleep(Duration::from_millis(3));

// The SMBus functions read word values as little endian but that is not
// what we want
let mut buf = [0_u8; 4];
try!(i2cdev.write(&[REGISTER_ADDR_PADC]));
try!(i2cdev.read(&mut buf));
i2cdev.write(&[REGISTER_ADDR_PADC])?;
i2cdev.read(&mut buf)?;
let padc: u16 = BigEndian::read_u16(&buf) >> 6;
let tadc: u16 = BigEndian::read_u16(&buf[2..]) >> 6;
Ok(MPL115A2RawReading {
Expand Down Expand Up @@ -364,7 +364,7 @@ mod sensors {
{
/// Create sensor accessor for MPL115A2 on the provided i2c bus path
pub fn new(mut i2cdev: T) -> Result<MPL115A2BarometerThermometer<T>, T::Error> {
let coeff = try!(MPL115A2Coefficients::new(&mut i2cdev));
let coeff = MPL115A2Coefficients::new(&mut i2cdev)?;
Ok(MPL115A2BarometerThermometer {
i2cdev: i2cdev,
coeff: coeff,
Expand All @@ -379,7 +379,7 @@ mod sensors {
type Error = T::Error;

fn pressure_kpa(&mut self) -> Result<f32, T::Error> {
let reading = try!(MPL115A2RawReading::new(&mut self.i2cdev));
let reading = MPL115A2RawReading::new(&mut self.i2cdev)?;
Ok(reading.pressure_kpa(&self.coeff))
}
}
Expand All @@ -391,7 +391,7 @@ mod sensors {
type Error = T::Error;

fn temperature_celsius(&mut self) -> Result<f32, T::Error> {
let reading = try!(MPL115A2RawReading::new(&mut self.i2cdev));
let reading = MPL115A2RawReading::new(&mut self.i2cdev)?;
Ok(reading.temperature_celsius())
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait I2CDevice {
/// the previous SMBus command.
fn smbus_read_byte(&mut self) -> Result<u8, Self::Error> {
let mut buf = [0_u8];
try!(self.read(&mut buf));
self.read(&mut buf)?;
Ok(buf[0])
}

Expand All @@ -49,7 +49,7 @@ pub trait I2CDevice {
///
/// The register is specified through the Comm byte.
fn smbus_read_byte_data(&mut self, register: u8) -> Result<u8, Self::Error> {
try!(self.smbus_write_byte(register));
self.smbus_write_byte(register)?;
self.smbus_read_byte()
}

Expand All @@ -63,8 +63,8 @@ pub trait I2CDevice {
/// Read 2 bytes from a given register on a device (lsb first)
fn smbus_read_word_data(&mut self, register: u8) -> Result<u16, Self::Error> {
let mut buf: [u8; 2] = [0x00; 2];
try!(self.smbus_write_byte(register));
try!(self.read(&mut buf));
self.smbus_write_byte(register)?;
self.read(&mut buf)?;
Ok(LittleEndian::read_u16(&buf))
}

Expand All @@ -78,8 +78,8 @@ pub trait I2CDevice {
/// Select a register, send 16 bits of data to it, and read 16 bits of data
fn smbus_process_word(&mut self, register: u8, value: u16) -> Result<u16, Self::Error> {
let mut buf: [u8; 2] = [0x00; 2];
try!(self.smbus_write_word_data(register, value));
try!(self.read(&mut buf));
self.smbus_write_word_data(register, value)?;
self.read(&mut buf)?;
Ok(LittleEndian::read_u16(&buf))
}

Expand Down
72 changes: 36 additions & 36 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ mod ioctl {


pub fn i2c_set_slave_address(fd: RawFd, slave_address: u16) -> Result<(), nix::Error> {
try!(unsafe {
ioctl::set_i2c_slave_address(fd, slave_address as i32)
});
unsafe {
ioctl::set_i2c_slave_address(fd, slave_address as i32)?;
}
Ok(())
}

Expand Down Expand Up @@ -222,13 +222,13 @@ pub fn i2c_smbus_write_quick(fd: RawFd, bit: bool) -> Result<(), I2CError> {
#[inline]
pub fn i2c_smbus_read_byte(fd: RawFd) -> Result<u8, I2CError> {
let mut data = i2c_smbus_data::empty();
try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_READ,
0,
I2CSMBusSize::I2C_SMBUS_BYTE,
&mut data)
});
&mut data)?
}
Ok(data.block[0])
}

Expand All @@ -246,40 +246,40 @@ pub fn i2c_smbus_write_byte(fd: RawFd, value: u8) -> Result<(), I2CError> {
#[inline]
pub fn i2c_smbus_read_byte_data(fd: RawFd, register: u8) -> Result<u8, I2CError> {
let mut data = i2c_smbus_data::empty();
try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_READ,
register,
I2CSMBusSize::I2C_SMBUS_BYTE_DATA,
&mut data)
});
&mut data)?;
}
Ok(data.block[0])
}

#[inline]
pub fn i2c_smbus_write_byte_data(fd: RawFd, register: u8, value: u8) -> Result<(), I2CError> {
let mut data = i2c_smbus_data::empty();
data.block[0] = value;
try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_WRITE,
register,
I2CSMBusSize::I2C_SMBUS_BYTE_DATA,
&mut data)
});
&mut data)?;
}
Ok(())
}

#[inline]
pub fn i2c_smbus_read_word_data(fd: RawFd, register: u8) -> Result<u16, I2CError> {
let mut data = i2c_smbus_data::empty();
try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_READ,
register,
I2CSMBusSize::I2C_SMBUS_WORD_DATA,
&mut data)
});
&mut data)?;
};

Ok(Cursor::new(&data.block[..])
.read_u16::<NativeEndian>()
Expand All @@ -294,13 +294,13 @@ pub fn i2c_smbus_write_word_data(fd: RawFd, register: u8, value: u16) -> Result<
.write_u16::<NativeEndian>(value)
.unwrap();

try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_WRITE,
register,
I2CSMBusSize::I2C_SMBUS_WORD_DATA,
&mut data)
});
&mut data)?;
};
Ok(())
}

Expand All @@ -311,13 +311,13 @@ pub fn i2c_smbus_process_call(fd: RawFd, register: u8, value: u16) -> Result<u16
.write_u16::<NativeEndian>(value)
.unwrap();

try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_WRITE,
register,
I2CSMBusSize::I2C_SMBUS_PROC_CALL,
&mut data)
});
&mut data)?;
}
Ok(Cursor::new(&data.block[..])
.read_u16::<NativeEndian>()
.unwrap())
Expand All @@ -326,13 +326,13 @@ pub fn i2c_smbus_process_call(fd: RawFd, register: u8, value: u16) -> Result<u16
#[inline]
pub fn i2c_smbus_read_block_data(fd: RawFd, register: u8) -> Result<Vec<u8>, I2CError> {
let mut data = i2c_smbus_data::empty();
try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_READ,
register,
I2CSMBusSize::I2C_SMBUS_BLOCK_DATA,
&mut data)
});
&mut data)?;
}

// create a vector from the data in the block starting at byte
// 1 and ending after count bytes after that
Expand All @@ -343,13 +343,13 @@ pub fn i2c_smbus_read_block_data(fd: RawFd, register: u8) -> Result<Vec<u8>, I2C
pub fn i2c_smbus_read_i2c_block_data(fd: RawFd, register: u8, len: u8) -> Result<Vec<u8>, I2CError> {
let mut data = i2c_smbus_data::empty();
data.block[0] = len;
try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_READ,
register,
I2CSMBusSize::I2C_SMBUS_I2C_BLOCK_DATA,
&mut data)
});
&mut data)?;
}

// create a vector from the data in the block starting at byte
// 1 and ending after count bytes after that
Expand All @@ -369,13 +369,13 @@ pub fn i2c_smbus_write_block_data(fd: RawFd, register: u8, values: &[u8]) -> Res
for i in 1..(len + 1) {
data.block[i] = values[i - 1];
}
try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_WRITE,
register,
I2CSMBusSize::I2C_SMBUS_BLOCK_DATA,
&mut data)
});
&mut data)?;
}
Ok(())
}

Expand All @@ -394,13 +394,13 @@ pub fn i2c_smbus_write_i2c_block_data(fd: RawFd,
for i in 1..(len + 1) {
data.block[i] = values[i - 1];
}
try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_WRITE,
register,
I2CSMBusSize::I2C_SMBUS_I2C_BLOCK_DATA,
&mut data)
});
&mut data)?;
}
Ok(())
}

Expand All @@ -416,13 +416,13 @@ pub fn i2c_smbus_process_call_block(fd: RawFd, register: u8, values: &[u8]) -> R
for i in 1..(len + 1) {
data.block[i] = values[i - 1];
}
try!(unsafe {
unsafe {
i2c_smbus_access(fd,
I2CSMBusReadWrite::I2C_SMBUS_WRITE,
register,
I2CSMBusSize::I2C_SMBUS_BLOCK_PROC_CALL,
&mut data)
});
&mut data)?;
};

// create a vector from the data in the block starting at byte
// 1 and ending after count bytes after that
Expand Down
Loading

0 comments on commit e2dd1bf

Please sign in to comment.