Skip to content

Commit

Permalink
Change ! in associated error types to Infallible
Browse files Browse the repository at this point in the history
The `Never` helper still exists, because it is necessary to implement
traits such as Termination for `!`, but the public error types are
changed.

While this is a breaking change that was not preceded by a deprecation,
it is unlikely people match on the precise type (and all error
conversions should be implemented or implementable on both types).
  • Loading branch information
chrysn committed Aug 19, 2024
1 parent c75bd1c commit 2b73c1d
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 46 deletions.
6 changes: 3 additions & 3 deletions src/adc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Never;
use core::convert::Infallible;

pub struct ADCLine(riot_sys::adc_t);

Expand Down Expand Up @@ -43,9 +43,9 @@ impl embedded_hal_0_2::adc::Channel<ADC> for ADCLine {
}

impl embedded_hal_0_2::adc::OneShot<ADC, i32, ADCLine> for ADC {
type Error = Never;
type Error = Infallible;

fn read(&mut self, pin: &mut ADCLine) -> nb::Result<i32, Never> {
fn read(&mut self, pin: &mut ADCLine) -> nb::Result<i32, Infallible> {
// Sorry, blocking still
Ok(unsafe { riot_sys::adc_sample(pin.0, self.resolution) })
}
Expand Down
2 changes: 1 addition & 1 deletion src/gnrc/netreg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type PktsnipPort = crate::msg::v2::SendPort<
/// used once more (with the risk that messages from the old registration arrive in the new one,
/// which is wrong correctness-wise but safe because it'll still be a pointer to a pktsnip).
#[cfg(feature = "with_msg_v2")]
pub fn register_for_messages<F: FnOnce() -> crate::Never>(
pub fn register_for_messages<F: FnOnce() -> !>(
grant: PktsnipPort,
nettype: riot_sys::gnrc_nettype_t,
demux_ctx: u32,
Expand Down
32 changes: 16 additions & 16 deletions src/gpio/impl_0_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,70 @@ use super::*;
use embedded_hal_0_2::digital::v2::{InputPin, OutputPin, ToggleableOutputPin};

impl InputPin for InputGPIO {
type Error = Never;
type Error = Infallible;

fn is_high(&self) -> Result<bool, Never> {
fn is_high(&self) -> Result<bool, Infallible> {
Ok(unsafe { gpio_read(self.to_c()) } != 0)
}

fn is_low(&self) -> Result<bool, Never> {
fn is_low(&self) -> Result<bool, Infallible> {
Ok(unsafe { gpio_read(self.to_c()) } == 0)
}
}

impl OutputPin for OutputGPIO {
type Error = Never;
type Error = Infallible;

fn set_high(&mut self) -> Result<(), Never> {
fn set_high(&mut self) -> Result<(), Infallible> {
unsafe { gpio_set(self.to_c()) };
Ok(())
}

fn set_low(&mut self) -> Result<(), Never> {
fn set_low(&mut self) -> Result<(), Infallible> {
unsafe { gpio_clear(self.to_c()) };
Ok(())
}
}

impl ToggleableOutputPin for OutputGPIO {
type Error = Never;
type Error = Infallible;

fn toggle(&mut self) -> Result<(), Never> {
fn toggle(&mut self) -> Result<(), Infallible> {
unsafe { gpio_toggle(self.to_c()) };
Ok(())
}
}

impl InputPin for InOutGPIO {
type Error = Never;
type Error = Infallible;

fn is_high(&self) -> Result<bool, Never> {
fn is_high(&self) -> Result<bool, Infallible> {
Ok(unsafe { gpio_read(self.to_c()) } != 0)
}

fn is_low(&self) -> Result<bool, Never> {
fn is_low(&self) -> Result<bool, Infallible> {
Ok(unsafe { gpio_read(self.to_c()) } == 0)
}
}

impl OutputPin for InOutGPIO {
type Error = Never;
type Error = Infallible;

fn set_high(&mut self) -> Result<(), Never> {
fn set_high(&mut self) -> Result<(), Infallible> {
unsafe { gpio_set(self.to_c()) };
Ok(())
}

fn set_low(&mut self) -> Result<(), Never> {
fn set_low(&mut self) -> Result<(), Infallible> {
unsafe { gpio_clear(self.to_c()) };
Ok(())
}
}

impl ToggleableOutputPin for InOutGPIO {
type Error = Never;
type Error = Infallible;

fn toggle(&mut self) -> Result<(), Never> {
fn toggle(&mut self) -> Result<(), Infallible> {
unsafe { gpio_toggle(self.to_c()) };
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod impl_1;
use riot_sys::{gpio_clear, gpio_mode_t, gpio_read, gpio_set, gpio_t, gpio_toggle, gpio_write};

use crate::error::NegativeErrorExt;
use crate::Never;
use core::convert::Infallible;

/// A Rust representation of RIOT's gpio_t, representing a single pin in no particular
/// configuration.
Expand Down
16 changes: 8 additions & 8 deletions src/led.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Wrappers for the `LEDn_{ON,OFF,TOGGLE}` macros
use crate::Never;
use core::convert::Infallible;

/// The Ith LED (calling the `LED<I>_{ON,OFF,TOGGLE}` macros).
///
Expand All @@ -21,7 +21,7 @@ impl<const I: u8> LED<I> {
}

impl<const I: u8> switch_hal::OutputSwitch for LED<I> {
type Error = Never;
type Error = Infallible;

fn on(&mut self) -> Result<(), Self::Error> {
use embedded_hal_0_2::digital::v2::OutputPin;
Expand All @@ -35,17 +35,17 @@ impl<const I: u8> switch_hal::OutputSwitch for LED<I> {
}

impl<const I: u8> switch_hal::ToggleableOutputSwitch for LED<I> {
type Error = Never;
type Error = Infallible;

fn toggle(&mut self) -> Result<(), Self::Error> {
<Self as embedded_hal_0_2::digital::v2::ToggleableOutputPin>::toggle(self)
}
}

impl<const I: u8> embedded_hal_0_2::digital::v2::OutputPin for LED<I> {
type Error = Never;
type Error = Infallible;

fn set_high(&mut self) -> Result<(), Never> {
fn set_high(&mut self) -> Result<(), Infallible> {
// unsafe: RIOT's LED functions can be called any time (and no-op on undefined LEDs)
unsafe {
match I {
Expand All @@ -63,7 +63,7 @@ impl<const I: u8> embedded_hal_0_2::digital::v2::OutputPin for LED<I> {
Ok(())
}

fn set_low(&mut self) -> Result<(), Never> {
fn set_low(&mut self) -> Result<(), Infallible> {
// unsafe: RIOT's LED functions can be called any time (and no-op on undefined LEDs)
unsafe {
match I {
Expand All @@ -83,9 +83,9 @@ impl<const I: u8> embedded_hal_0_2::digital::v2::OutputPin for LED<I> {
}

impl<const I: u8> embedded_hal_0_2::digital::v2::ToggleableOutputPin for LED<I> {
type Error = Never;
type Error = Infallible;

fn toggle(&mut self) -> Result<(), Never> {
fn toggle(&mut self) -> Result<(), Infallible> {
// unsafe: RIOT's LED functions can be called any time (and no-op on undefined LEDs)
unsafe {
match I {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub mod error;

mod helpers;
mod never;
use never::Never;

/// The identifier of the RIOT board the program is being built for (`RIOT_BOARD` in C).
#[doc(alias = "RIOT_BOARD")]
Expand Down
10 changes: 5 additions & 5 deletions src/main_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ impl<F: Fn() -> T, T: Termination> UsableAsMain<[u8; 1]> for F {
}
}

impl<F: Fn(StartToken) -> crate::never::Never> Sealed<[u8; 2]> for F {}
impl<F: Fn(StartToken) -> !> Sealed<[u8; 2]> for F {}

impl<F: Fn(StartToken) -> crate::never::Never> UsableAsMain<[u8; 2]> for F {
impl<F: Fn(StartToken) -> !> UsableAsMain<[u8; 2]> for F {
unsafe fn call_main(&self) -> i32 {
// unsafe: By construction of the C main function this only happens at startup time
// with a thread that hasn't done anything relevant before.
Expand Down Expand Up @@ -144,12 +144,12 @@ impl<E: fmt::Debug> Termination for Result<(), E> {
fn report(self) -> i32 {
match self {
Ok(()) => ().report(),
Err(err) => Err::<crate::Never, _>(err).report(),
Err(err) => Err::<crate::never::Never, _>(err).report(),
}
}
}

impl Termination for crate::Never {
impl Termination for crate::never::Never {
fn report(self) -> i32 {
self
}
Expand All @@ -161,7 +161,7 @@ impl Termination for core::convert::Infallible {
}
}

impl<E: fmt::Debug> Termination for Result<crate::Never, E> {
impl<E: fmt::Debug> Termination for Result<crate::never::Never, E> {
fn report(self) -> i32 {
match self {
Err(err) => {
Expand Down
6 changes: 3 additions & 3 deletions src/microbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use embedded_graphics::{
drawable::Pixel, geometry::Point, geometry::Size, pixelcolor::BinaryColor, DrawTarget,
};

use crate::Never;
use core::convert::Infallible;

/// The 5x5 LED matrix of the micro:bit boards
///
Expand All @@ -24,9 +24,9 @@ impl LEDs {
}

impl DrawTarget<BinaryColor> for LEDs {
type Error = Never;
type Error = Infallible;

fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Never> {
fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Infallible> {
let Pixel(Point { x, y }, color) = pixel;

let setter = match color {
Expand Down
4 changes: 2 additions & 2 deletions src/saul/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use riot_sys::libc;

use super::{Class, Phydat};
use crate::error::NegativeErrorExt;
use crate::Never;
use core::convert::Infallible;

/// The single error read and write operations may produce; corresponds to an `-ECANCELED`.
/// (-ENOTSUP is expressed by not having support for the operation in the first place, indicated by
Expand Down Expand Up @@ -229,7 +229,7 @@ pub fn register_and_then<DEV, DRIV>(
driver: &Driver<DEV, DRIV>,
device: &DEV,
name: Option<&CStr>,
f: impl FnOnce() -> Never,
f: impl FnOnce() -> !,
) -> !
where
DEV: Sized + Sync + 'static,
Expand Down
4 changes: 2 additions & 2 deletions src/spi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Never;
use core::convert::Infallible;
use embedded_hal_0_2::blocking;
use riot_sys::{
spi_acquire, spi_clk_t, spi_cs_t, spi_mode_t, spi_release, spi_t, spi_transfer_bytes,
Expand Down Expand Up @@ -65,7 +65,7 @@ impl SPIDevice {
// }

impl<'a> blocking::spi::Transfer<u8> for AcquiredSPI<'a> {
type Error = Never;
type Error = Infallible;

fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
unsafe {
Expand Down
5 changes: 1 addition & 4 deletions src/thread/tokenparts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ impl<const MS: bool, const FS: bool> TokenParts<MS, true, FS> {
// unsound: The `true` MS would mean that the NoConfiguredMessages could be taken out again and
// used to configure semantics, and then all of a sudden the still-configured message queue
// would be sent to again.
pub fn with_message_queue<
const N: usize,
F: FnOnce(TokenParts<MS, false, FS>) -> crate::Never,
>(
pub fn with_message_queue<const N: usize, F: FnOnce(TokenParts<MS, false, FS>) -> !>(
self,
f: F,
) -> ! {
Expand Down

0 comments on commit 2b73c1d

Please sign in to comment.