Skip to content

Commit

Permalink
Replace void with Infallible (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull authored and TheZoq2 committed Nov 8, 2019
1 parent 420243a commit 9ac0bb4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Breaking changes

- `void::Void` replaced with `Infallible` where it is possible
- Change timer/pwm init API
- Remove `set_low` and `set_high` for pins in Alternate output mode
- Renames `set_seconds` and `seconds` methods on RTC to `set_time` and `current_time`, respectively
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,15 @@ fn main() -> ! {
// in order to configure the port. For pins 0-7, crl should be passed instead.
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
// Configure the syst timer to trigger an update every second
let mut timer = Timer::syst(cp.SYST, 1.hz(), clocks);
let mut timer = Timer::syst(cp.SYST, clocks)
.start_count_down(1.hz());

// Wait for the timer to trigger an update and change the state of the LED
loop {
block!(timer.wait()).unwrap();
led.set_high();
led.set_high().unwrap();
block!(timer.wait()).unwrap();
led.set_low();
led.set_low().unwrap();
}
}
```
Expand Down
28 changes: 14 additions & 14 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ macro_rules! gpio {
]) => {
/// GPIO
pub mod $gpiox {
use void::Void;
use core::convert::Infallible;
use core::marker::PhantomData;

use crate::hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, toggleable};
Expand Down Expand Up @@ -164,7 +164,7 @@ macro_rules! gpio {
}

impl<MODE> OutputPin for Generic<Output<MODE>> {
type Error = Void;
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
// NOTE(unsafe) atomic write to a stateless register
Ok(unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << self.i)) })
Expand All @@ -177,7 +177,7 @@ macro_rules! gpio {
}

impl<MODE> InputPin for Generic<Input<MODE>> {
type Error = Void;
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
self.is_low().map(|b| !b)
}
Expand All @@ -202,7 +202,7 @@ macro_rules! gpio {
impl <MODE> toggleable::Default for Generic<Output<MODE>> {}

impl InputPin for Generic<Output<OpenDrain>> {
type Error = Void;
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
self.is_low().map(|b| !b)
}
Expand Down Expand Up @@ -470,7 +470,7 @@ macro_rules! gpio {
}

impl<MODE> OutputPin for $PXi<Output<MODE>> {
type Error = Void;
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
// NOTE(unsafe) atomic write to a stateless register
Ok(unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) })
Expand All @@ -496,7 +496,7 @@ macro_rules! gpio {
impl<MODE> toggleable::Default for $PXi<Output<MODE>> {}

impl<MODE> InputPin for $PXi<Input<MODE>> {
type Error = Void;
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
self.is_low().map(|b| !b)
}
Expand All @@ -508,7 +508,7 @@ macro_rules! gpio {
}

impl InputPin for $PXi<Output<OpenDrain>> {
type Error = Void;
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
self.is_low().map(|b| !b)
}
Expand All @@ -525,8 +525,8 @@ macro_rules! gpio {

macro_rules! impl_pxx {
($(($port:ident :: $pin:ident)),*) => {
use void::Void;
use embedded_hal::digital::v2::{InputPin, StatefulOutputPin, OutputPin};
use core::convert::Infallible;

pub enum Pxx<MODE> {
$(
Expand All @@ -535,14 +535,14 @@ macro_rules! impl_pxx {
}

impl<MODE> OutputPin for Pxx<Output<MODE>> {
type Error = Void;
fn set_high(&mut self) -> Result<(), Void> {
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Infallible> {
match self {
$(Pxx::$pin(pin) => pin.set_high()),*
}
}

fn set_low(&mut self) -> Result<(), Void> {
fn set_low(&mut self) -> Result<(), Infallible> {
match self {
$(Pxx::$pin(pin) => pin.set_low()),*
}
Expand All @@ -564,14 +564,14 @@ macro_rules! impl_pxx {
}

impl<MODE> InputPin for Pxx<Input<MODE>> {
type Error = Void;
fn is_high(&self) -> Result<bool, Void> {
type Error = Infallible;
fn is_high(&self) -> Result<bool, Infallible> {
match self {
$(Pxx::$pin(pin) => pin.is_high()),*
}
}

fn is_low(&self) -> Result<bool, Void> {
fn is_low(&self) -> Result<bool, Infallible> {
match self {
$(Pxx::$pin(pin) => pin.is_low()),*
}
Expand Down
6 changes: 3 additions & 3 deletions src/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::backup_domain::BackupDomain;
use crate::time::Hertz;

use nb;
use void::Void;
use core::convert::Infallible;

// The LSE runs at at 32 768 hertz unless an external clock is provided
const LSE_HERTZ: u32 = 32_768;
Expand Down Expand Up @@ -170,11 +170,11 @@ impl Rtc {
use nb::block;
rtc.set_alarm(rtc.read_counts() + 5);
// NOTE: Safe unwrap because Void can't be returned
// NOTE: Safe unwrap because Infallible can't be returned
block!(rtc.wait_alarm()).unwrap();
```
*/
pub fn wait_alarm(&mut self) -> nb::Result<(), Void> {
pub fn wait_alarm(&mut self) -> nb::Result<(), Infallible> {
if self.regs.crl.read().alrf().bit() == true {
self.regs.crl.modify(|_, w| w.alrf().clear_bit());
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use core::sync::atomic::{self, Ordering};

use nb;
use crate::pac::{USART1, USART2, USART3};
use void::Void;
use core::convert::Infallible;
use embedded_hal::serial::Write;

use crate::afio::MAPR;
Expand Down Expand Up @@ -388,7 +388,7 @@ macro_rules! hal {
}

impl crate::hal::serial::Write<u8> for Tx<$USARTX> {
type Error = Void;
type Error = Infallible;

fn flush(&mut self) -> nb::Result<(), Self::Error> {
// NOTE(unsafe) atomic read with no side effects
Expand Down

0 comments on commit 9ac0bb4

Please sign in to comment.