Skip to content

Commit

Permalink
Remove try_ method prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Roff authored and MorganR committed May 16, 2021
1 parent 112aa9e commit 3e76395
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 47 deletions.
2 changes: 1 addition & 1 deletion examples/transactional-i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ where
I2cOperation::Write(&[0xAB]),
I2cOperation::Read(&mut read_buffer),
];
self.i2c.try_exec(ADDR, &mut ops).and(Ok(read_buffer[0]))
self.i2c.exec(ADDR, &mut ops).and(Ok(read_buffer[0]))
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/cdev_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,35 @@ impl CdevPin {
impl embedded_hal::digital::OutputPin for CdevPin {
type Error = gpio_cdev::errors::Error;

fn try_set_low(&mut self) -> Result<(), Self::Error> {
fn set_low(&mut self) -> Result<(), Self::Error> {
self.0.set_value(0)
}

fn try_set_high(&mut self) -> Result<(), Self::Error> {
fn set_high(&mut self) -> Result<(), Self::Error> {
self.0.set_value(1)
}
}

impl embedded_hal::digital::InputPin for CdevPin {
type Error = gpio_cdev::errors::Error;

fn try_is_high(&self) -> Result<bool, Self::Error> {
fn is_high(&self) -> Result<bool, Self::Error> {
if !self.1.is_active_low() {
self.0.get_value().map(|val| val != 0)
} else {
self.0.get_value().map(|val| val == 0)
}
}

fn try_is_low(&self) -> Result<bool, Self::Error> {
self.try_is_high().map(|val| !val)
fn is_low(&self) -> Result<bool, Self::Error> {
self.is_high().map(|val| !val)
}
}

impl embedded_hal::digital::IoPin<CdevPin, CdevPin> for CdevPin {
type Error = gpio_cdev::errors::Error;

fn try_into_input_pin(self) -> Result<CdevPin, Self::Error> {
fn into_input_pin(self) -> Result<CdevPin, Self::Error> {
if self.1.direction() == gpio_cdev::LineDirection::In {
return Ok(self);
}
Expand All @@ -78,7 +78,7 @@ impl embedded_hal::digital::IoPin<CdevPin, CdevPin> for CdevPin {
CdevPin::new(line.request(input_flags, 0, &consumer)?)
}

fn try_into_output_pin(
fn into_output_pin(
self,
state: embedded_hal::digital::PinState,
) -> Result<CdevPin, Self::Error> {
Expand Down
34 changes: 15 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct Delay;
impl embedded_hal::blocking::delay::DelayUs<u8> for Delay {
type Error = Infallible;

fn try_delay_us(&mut self, n: u8) -> Result<(), Self::Error> {
fn delay_us(&mut self, n: u8) -> Result<(), Self::Error> {
thread::sleep(Duration::new(0, u32(n) * 1000));
Ok(())
}
Expand All @@ -73,7 +73,7 @@ impl embedded_hal::blocking::delay::DelayUs<u8> for Delay {
impl embedded_hal::blocking::delay::DelayUs<u16> for Delay {
type Error = Infallible;

fn try_delay_us(&mut self, n: u16) -> Result<(), Self::Error> {
fn delay_us(&mut self, n: u16) -> Result<(), Self::Error> {
thread::sleep(Duration::new(0, u32(n) * 1000));
Ok(())
}
Expand All @@ -82,7 +82,7 @@ impl embedded_hal::blocking::delay::DelayUs<u16> for Delay {
impl embedded_hal::blocking::delay::DelayUs<u32> for Delay {
type Error = Infallible;

fn try_delay_us(&mut self, n: u32) -> Result<(), Self::Error> {
fn delay_us(&mut self, n: u32) -> Result<(), Self::Error> {
let secs = n / 1_000_000;
let nsecs = (n % 1_000_000) * 1_000;

Expand All @@ -94,7 +94,7 @@ impl embedded_hal::blocking::delay::DelayUs<u32> for Delay {
impl embedded_hal::blocking::delay::DelayUs<u64> for Delay {
type Error = Infallible;

fn try_delay_us(&mut self, n: u64) -> Result<(), Self::Error> {
fn delay_us(&mut self, n: u64) -> Result<(), Self::Error> {
let secs = n / 1_000_000;
let nsecs = ((n % 1_000_000) * 1_000) as u32;

Expand All @@ -106,7 +106,7 @@ impl embedded_hal::blocking::delay::DelayUs<u64> for Delay {
impl embedded_hal::blocking::delay::DelayMs<u8> for Delay {
type Error = Infallible;

fn try_delay_ms(&mut self, n: u8) -> Result<(), Self::Error> {
fn delay_ms(&mut self, n: u8) -> Result<(), Self::Error> {
thread::sleep(Duration::from_millis(u64(n)));
Ok(())
}
Expand All @@ -115,7 +115,7 @@ impl embedded_hal::blocking::delay::DelayMs<u8> for Delay {
impl embedded_hal::blocking::delay::DelayMs<u16> for Delay {
type Error = Infallible;

fn try_delay_ms(&mut self, n: u16) -> Result<(), Self::Error> {
fn delay_ms(&mut self, n: u16) -> Result<(), Self::Error> {
thread::sleep(Duration::from_millis(u64(n)));
Ok(())
}
Expand All @@ -124,7 +124,7 @@ impl embedded_hal::blocking::delay::DelayMs<u16> for Delay {
impl embedded_hal::blocking::delay::DelayMs<u32> for Delay {
type Error = Infallible;

fn try_delay_ms(&mut self, n: u32) -> Result<(), Self::Error> {
fn delay_ms(&mut self, n: u32) -> Result<(), Self::Error> {
thread::sleep(Duration::from_millis(u64(n)));
Ok(())
}
Expand All @@ -133,7 +133,7 @@ impl embedded_hal::blocking::delay::DelayMs<u32> for Delay {
impl embedded_hal::blocking::delay::DelayMs<u64> for Delay {
type Error = Infallible;

fn try_delay_ms(&mut self, n: u64) -> Result<(), Self::Error> {
fn delay_ms(&mut self, n: u64) -> Result<(), Self::Error> {
thread::sleep(Duration::from_millis(n));
Ok(())
}
Expand Down Expand Up @@ -176,7 +176,7 @@ impl I2cdev {
impl embedded_hal::blocking::i2c::Read for I2cdev {
type Error = i2cdev::linux::LinuxI2CError;

fn try_read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
self.set_address(address)?;
self.inner.read(buffer)
}
Expand All @@ -185,7 +185,7 @@ impl embedded_hal::blocking::i2c::Read for I2cdev {
impl embedded_hal::blocking::i2c::Write for I2cdev {
type Error = i2cdev::linux::LinuxI2CError;

fn try_write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
self.set_address(address)?;
self.inner.write(bytes)
}
Expand All @@ -194,7 +194,7 @@ impl embedded_hal::blocking::i2c::Write for I2cdev {
impl embedded_hal::blocking::i2c::WriteRead for I2cdev {
type Error = i2cdev::linux::LinuxI2CError;

fn try_write_read(
fn write_read(
&mut self,
address: u8,
bytes: &[u8],
Expand All @@ -209,11 +209,7 @@ impl embedded_hal::blocking::i2c::WriteRead for I2cdev {
impl embedded_hal::blocking::i2c::Transactional for I2cdev {
type Error = i2cdev::linux::LinuxI2CError;

fn try_exec(
&mut self,
address: u8,
operations: &mut [I2cOperation],
) -> Result<(), Self::Error> {
fn exec(&mut self, address: u8, operations: &mut [I2cOperation]) -> Result<(), Self::Error> {
// Map operations from generic to linux objects
let mut messages: Vec<_> = operations
.as_mut()
Expand Down Expand Up @@ -263,7 +259,7 @@ impl Spidev {
impl embedded_hal::blocking::spi::Transfer<u8> for Spidev {
type Error = io::Error;

fn try_transfer<'b>(&mut self, buffer: &'b mut [u8]) -> io::Result<&'b [u8]> {
fn transfer<'b>(&mut self, buffer: &'b mut [u8]) -> io::Result<&'b [u8]> {
let tx = buffer.to_owned();
self.0
.transfer(&mut SpidevTransfer::read_write(&tx, buffer))?;
Expand All @@ -274,7 +270,7 @@ impl embedded_hal::blocking::spi::Transfer<u8> for Spidev {
impl embedded_hal::blocking::spi::Write<u8> for Spidev {
type Error = io::Error;

fn try_write(&mut self, buffer: &[u8]) -> io::Result<()> {
fn write(&mut self, buffer: &[u8]) -> io::Result<()> {
self.0.write_all(buffer)
}
}
Expand All @@ -285,7 +281,7 @@ pub use embedded_hal::blocking::spi::Operation as SpiOperation;
impl embedded_hal::blocking::spi::Transactional<u8> for Spidev {
type Error = io::Error;

fn try_exec<'a>(&mut self, operations: &mut [SpiOperation<'a, u8>]) -> Result<(), Self::Error> {
fn exec<'a>(&mut self, operations: &mut [SpiOperation<'a, u8>]) -> Result<(), Self::Error> {
// Map types from generic to linux objects
let mut messages: Vec<_> = operations
.iter_mut()
Expand Down
12 changes: 6 additions & 6 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn translate_io_errors(err: std::io::Error) -> nb::Error<IoErrorKind> {
impl embedded_hal::serial::Read<u8> for Serial {
type Error = IoErrorKind;

fn try_read(&mut self) -> nb::Result<u8, Self::Error> {
fn read(&mut self) -> nb::Result<u8, Self::Error> {
let mut buffer = [0; 1];
let bytes_read = self.0.read(&mut buffer).map_err(translate_io_errors)?;
if bytes_read == 1 {
Expand All @@ -46,12 +46,12 @@ impl embedded_hal::serial::Read<u8> for Serial {
impl embedded_hal::serial::Write<u8> for Serial {
type Error = IoErrorKind;

fn try_write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
self.0.write(&[word]).map_err(translate_io_errors)?;
Ok(())
}

fn try_flush(&mut self) -> nb::Result<(), Self::Error> {
fn flush(&mut self) -> nb::Result<(), Self::Error> {
self.0.flush().map_err(translate_io_errors)
}
}
Expand All @@ -75,20 +75,20 @@ mod test {
#[test]
fn test_empty_read() {
let (mut _master, mut serial) = create_pty_and_serial();
assert_eq!(Err(nb::Error::WouldBlock), serial.try_read());
assert_eq!(Err(nb::Error::WouldBlock), serial.read());
}

#[test]
fn test_read() {
let (mut master, mut serial) = create_pty_and_serial();
master.write(&[1]).expect("Write failed");
assert_eq!(Ok(1), serial.try_read());
assert_eq!(Ok(1), serial.read());
}

#[test]
fn test_write() {
let (mut master, mut serial) = create_pty_and_serial();
serial.try_write(2).expect("Write failed");
serial.write(2).expect("Write failed");
let mut buf = [0; 2];
assert_eq!(1, master.read(&mut buf).unwrap());
assert_eq!(buf, [2, 0]);
Expand Down
14 changes: 7 additions & 7 deletions src/sysfs_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,40 @@ impl SysfsPin {
impl embedded_hal::digital::OutputPin for SysfsPin {
type Error = sysfs_gpio::Error;

fn try_set_low(&mut self) -> Result<(), Self::Error> {
fn set_low(&mut self) -> Result<(), Self::Error> {
self.0.set_value(0)
}

fn try_set_high(&mut self) -> Result<(), Self::Error> {
fn set_high(&mut self) -> Result<(), Self::Error> {
self.0.set_value(1)
}
}

impl embedded_hal::digital::InputPin for SysfsPin {
type Error = sysfs_gpio::Error;

fn try_is_high(&self) -> Result<bool, Self::Error> {
fn is_high(&self) -> Result<bool, Self::Error> {
if !self.0.get_active_low()? {
self.0.get_value().map(|val| val != 0)
} else {
self.0.get_value().map(|val| val == 0)
}
}

fn try_is_low(&self) -> Result<bool, Self::Error> {
self.try_is_high().map(|val| !val)
fn is_low(&self) -> Result<bool, Self::Error> {
self.is_high().map(|val| !val)
}
}

impl embedded_hal::digital::IoPin<SysfsPin, SysfsPin> for SysfsPin {
type Error = sysfs_gpio::Error;

fn try_into_input_pin(self) -> Result<SysfsPin, Self::Error> {
fn into_input_pin(self) -> Result<SysfsPin, Self::Error> {
self.set_direction(sysfs_gpio::Direction::In)?;
Ok(self)
}

fn try_into_output_pin(
fn into_output_pin(
self,
state: embedded_hal::digital::PinState,
) -> Result<SysfsPin, Self::Error> {
Expand Down
14 changes: 7 additions & 7 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl CountDown for SysTimer {
type Error = Infallible;
type Time = Duration;

fn try_start<T>(&mut self, count: T) -> Result<(), Self::Error>
fn start<T>(&mut self, count: T) -> Result<(), Self::Error>
where
T: Into<Self::Time>,
{
Expand All @@ -40,7 +40,7 @@ impl CountDown for SysTimer {
Ok(())
}

fn try_wait(&mut self) -> nb::Result<(), Self::Error> {
fn wait(&mut self) -> nb::Result<(), Self::Error> {
if (Instant::now() - self.start) >= self.duration {
// Restart the timer to fulfill the contract by `Periodic`
self.start = Instant::now();
Expand All @@ -63,8 +63,8 @@ mod tests {
fn test_delay() {
let mut timer = SysTimer::new();
let before = Instant::now();
timer.try_start(Duration::from_millis(100)).unwrap();
nb::block!(timer.try_wait()).unwrap();
timer.start(Duration::from_millis(100)).unwrap();
nb::block!(timer.wait()).unwrap();
let after = Instant::now();
let duration_ms = (after - before).as_millis();
assert!(duration_ms >= 100);
Expand All @@ -76,13 +76,13 @@ mod tests {
fn test_periodic() {
let mut timer = SysTimer::new();
let before = Instant::now();
timer.try_start(Duration::from_millis(100)).unwrap();
nb::block!(timer.try_wait()).unwrap();
timer.start(Duration::from_millis(100)).unwrap();
nb::block!(timer.wait()).unwrap();
let after1 = Instant::now();
let duration_ms_1 = (after1 - before).as_millis();
assert!(duration_ms_1 >= 100);
assert!(duration_ms_1 < 500);
nb::block!(timer.try_wait()).unwrap();
nb::block!(timer.wait()).unwrap();
let after2 = Instant::now();
let duration_ms_2 = (after2 - after1).as_millis();
assert!(duration_ms_2 >= 100);
Expand Down

0 comments on commit 3e76395

Please sign in to comment.