From 2b73c1d51c7b8f8606aad8dedd6fda64287b7fd0 Mon Sep 17 00:00:00 2001 From: chrysn Date: Mon, 19 Aug 2024 09:56:40 +0200 Subject: [PATCH] Change `!` in associated error types to Infallible 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). --- src/adc.rs | 6 +++--- src/gnrc/netreg.rs | 2 +- src/gpio/impl_0_2.rs | 32 ++++++++++++++++---------------- src/gpio/mod.rs | 2 +- src/led.rs | 16 ++++++++-------- src/lib.rs | 1 - src/main_module.rs | 10 +++++----- src/microbit.rs | 6 +++--- src/saul/registration.rs | 4 ++-- src/spi/mod.rs | 4 ++-- src/thread/tokenparts.rs | 5 +---- 11 files changed, 42 insertions(+), 46 deletions(-) diff --git a/src/adc.rs b/src/adc.rs index d3567917..febc92c5 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -1,4 +1,4 @@ -use crate::Never; +use core::convert::Infallible; pub struct ADCLine(riot_sys::adc_t); @@ -43,9 +43,9 @@ impl embedded_hal_0_2::adc::Channel for ADCLine { } impl embedded_hal_0_2::adc::OneShot for ADC { - type Error = Never; + type Error = Infallible; - fn read(&mut self, pin: &mut ADCLine) -> nb::Result { + fn read(&mut self, pin: &mut ADCLine) -> nb::Result { // Sorry, blocking still Ok(unsafe { riot_sys::adc_sample(pin.0, self.resolution) }) } diff --git a/src/gnrc/netreg.rs b/src/gnrc/netreg.rs index 573bf1b2..1d185bae 100644 --- a/src/gnrc/netreg.rs +++ b/src/gnrc/netreg.rs @@ -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 crate::Never>( +pub fn register_for_messages !>( grant: PktsnipPort, nettype: riot_sys::gnrc_nettype_t, demux_ctx: u32, diff --git a/src/gpio/impl_0_2.rs b/src/gpio/impl_0_2.rs index 0381aa35..ebf2e2c5 100644 --- a/src/gpio/impl_0_2.rs +++ b/src/gpio/impl_0_2.rs @@ -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 { + fn is_high(&self) -> Result { Ok(unsafe { gpio_read(self.to_c()) } != 0) } - fn is_low(&self) -> Result { + fn is_low(&self) -> Result { 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 { + fn is_high(&self) -> Result { Ok(unsafe { gpio_read(self.to_c()) } != 0) } - fn is_low(&self) -> Result { + fn is_low(&self) -> Result { 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(()) } diff --git a/src/gpio/mod.rs b/src/gpio/mod.rs index 41bf5e92..b6a2c971 100644 --- a/src/gpio/mod.rs +++ b/src/gpio/mod.rs @@ -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. diff --git a/src/led.rs b/src/led.rs index 4282ebb7..2f3cfb20 100644 --- a/src/led.rs +++ b/src/led.rs @@ -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_{ON,OFF,TOGGLE}` macros). /// @@ -21,7 +21,7 @@ impl LED { } impl switch_hal::OutputSwitch for LED { - type Error = Never; + type Error = Infallible; fn on(&mut self) -> Result<(), Self::Error> { use embedded_hal_0_2::digital::v2::OutputPin; @@ -35,7 +35,7 @@ impl switch_hal::OutputSwitch for LED { } impl switch_hal::ToggleableOutputSwitch for LED { - type Error = Never; + type Error = Infallible; fn toggle(&mut self) -> Result<(), Self::Error> { ::toggle(self) @@ -43,9 +43,9 @@ impl switch_hal::ToggleableOutputSwitch for LED { } impl embedded_hal_0_2::digital::v2::OutputPin for LED { - 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 { @@ -63,7 +63,7 @@ impl embedded_hal_0_2::digital::v2::OutputPin for LED { 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 { @@ -83,9 +83,9 @@ impl embedded_hal_0_2::digital::v2::OutputPin for LED { } impl embedded_hal_0_2::digital::v2::ToggleableOutputPin for LED { - 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 { diff --git a/src/lib.rs b/src/lib.rs index 1d8dd2de..a1616cbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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")] diff --git a/src/main_module.rs b/src/main_module.rs index 96dcea3b..99db8bd7 100644 --- a/src/main_module.rs +++ b/src/main_module.rs @@ -56,9 +56,9 @@ impl T, T: Termination> UsableAsMain<[u8; 1]> for F { } } -impl crate::never::Never> Sealed<[u8; 2]> for F {} +impl !> Sealed<[u8; 2]> for F {} -impl crate::never::Never> UsableAsMain<[u8; 2]> for F { +impl !> 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. @@ -144,12 +144,12 @@ impl Termination for Result<(), E> { fn report(self) -> i32 { match self { Ok(()) => ().report(), - Err(err) => Err::(err).report(), + Err(err) => Err::(err).report(), } } } -impl Termination for crate::Never { +impl Termination for crate::never::Never { fn report(self) -> i32 { self } @@ -161,7 +161,7 @@ impl Termination for core::convert::Infallible { } } -impl Termination for Result { +impl Termination for Result { fn report(self) -> i32 { match self { Err(err) => { diff --git a/src/microbit.rs b/src/microbit.rs index f451917e..a0e4a924 100644 --- a/src/microbit.rs +++ b/src/microbit.rs @@ -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 /// @@ -24,9 +24,9 @@ impl LEDs { } impl DrawTarget for LEDs { - type Error = Never; + type Error = Infallible; - fn draw_pixel(&mut self, pixel: Pixel) -> Result<(), Never> { + fn draw_pixel(&mut self, pixel: Pixel) -> Result<(), Infallible> { let Pixel(Point { x, y }, color) = pixel; let setter = match color { diff --git a/src/saul/registration.rs b/src/saul/registration.rs index 74c264cd..a0c87926 100644 --- a/src/saul/registration.rs +++ b/src/saul/registration.rs @@ -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 @@ -229,7 +229,7 @@ pub fn register_and_then( driver: &Driver, device: &DEV, name: Option<&CStr>, - f: impl FnOnce() -> Never, + f: impl FnOnce() -> !, ) -> ! where DEV: Sized + Sync + 'static, diff --git a/src/spi/mod.rs b/src/spi/mod.rs index 53db151f..b3740b84 100644 --- a/src/spi/mod.rs +++ b/src/spi/mod.rs @@ -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, @@ -65,7 +65,7 @@ impl SPIDevice { // } impl<'a> blocking::spi::Transfer for AcquiredSPI<'a> { - type Error = Never; + type Error = Infallible; fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { unsafe { diff --git a/src/thread/tokenparts.rs b/src/thread/tokenparts.rs index a3644c74..f03311a7 100644 --- a/src/thread/tokenparts.rs +++ b/src/thread/tokenparts.rs @@ -147,10 +147,7 @@ impl TokenParts { // 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) -> crate::Never, - >( + pub fn with_message_queue) -> !>( self, f: F, ) -> ! {