Skip to content

Commit

Permalink
remove void dep, use core::convert::Infallible instead
Browse files Browse the repository at this point in the history
there's no need for the `void` dependency anymore. accordingly it can be
removed. this will also help with implementing `embedded-hal` v1 traits
as some of them rely on (or at least have better support for)
`core::convert::Infallible`.

this is a breaking change for consumers as they have to e.g. replace
`void_unwrap()` calls with `unwrap()`.
  • Loading branch information
rursprung authored and Rahix committed Jan 13, 2024
1 parent 92c32eb commit 411b110
Show file tree
Hide file tree
Showing 47 changed files with 175 additions and 230 deletions.
4 changes: 0 additions & 4 deletions arduino-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ ufmt = "0.2.0"
version = "0.2.3"
package = "embedded-hal"

[dependencies.void]
version = "1.0.2"
default-features = false

[dependencies.avr-hal-generic]
path = "../avr-hal-generic/"

Expand Down
4 changes: 0 additions & 4 deletions avr-hal-generic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,5 @@ version = "0.2.3"
package = "embedded-hal"
features = ["unproven"]

[dependencies.void]
version = "1.0.2"
default-features = false

[build-dependencies]
rustversion = "1.0"
4 changes: 2 additions & 2 deletions avr-hal-generic/src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<H, ADC: AdcOps<H>> AdcChannel<H, ADC> for Channel<H, ADC> {
/// let voltage = adc.read_blocking(&a0);
///
/// // alternatively, a non-blocking interface exists
/// let voltage = nb::block!(adc.read_nonblocking(&a0)).void_unwrap();
/// let voltage = nb::block!(adc.read_nonblocking(&a0)).unwrap();
/// ```
pub struct Adc<H, ADC: AdcOps<H>, CLOCK> {
p: ADC,
Expand Down Expand Up @@ -177,7 +177,7 @@ where
pub fn read_nonblocking<PIN: AdcChannel<H, ADC>>(
&mut self,
pin: &PIN,
) -> nb::Result<u16, void::Void> {
) -> nb::Result<u16, core::convert::Infallible> {
match (&self.reading_channel, self.p.raw_is_converting()) {
// Measurement on current pin is ongoing
(Some(channel), true) if *channel == pin.channel() => Err(nb::Error::WouldBlock),
Expand Down
2 changes: 1 addition & 1 deletion avr-hal-generic/src/eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
pub fn new(p: EEPROM) -> Self {
Self {
p,
_h: ::core::marker::PhantomData,
_h: marker::PhantomData,
}
}
#[inline]
Expand Down
4 changes: 0 additions & 4 deletions avr-hal-generic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ pub extern crate nb;
pub extern crate paste;
#[doc(hidden)]
pub extern crate ufmt;
#[doc(hidden)]
pub extern crate void;

pub mod adc;
pub mod clock;
Expand All @@ -30,8 +28,6 @@ pub mod wdt;
pub mod prelude {
pub use hal::prelude::*;
pub use ufmt::uWrite as _ufmt_uWrite;
pub use void::ResultVoidErrExt as _void_ResultVoidErrExt;
pub use void::ResultVoidExt as _void_ResultVoidExt;
}

// For making certain traits unimplementable from outside this crate.
Expand Down
6 changes: 3 additions & 3 deletions avr-hal-generic/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ where
}

/// Reconfigure the SPI peripheral after initializing
pub fn reconfigure(&mut self, settings: Settings) -> nb::Result<(), crate::void::Void> {
pub fn reconfigure(&mut self, settings: Settings) -> nb::Result<(), core::convert::Infallible> {
// wait for any in-flight writes to complete
self.flush()?;
self.p.raw_setup(&settings);
Expand All @@ -217,7 +217,7 @@ where
(self.p, self.sclk, self.mosi, self.miso, cs.0)
}

fn flush(&mut self) -> nb::Result<(), void::Void> {
fn flush(&mut self) -> nb::Result<(), core::convert::Infallible> {
if self.write_in_progress {
if self.p.raw_check_iflag() {
self.write_in_progress = false;
Expand Down Expand Up @@ -250,7 +250,7 @@ where
MISOPIN: port::PinOps,
CSPIN: port::PinOps,
{
type Error = void::Void;
type Error = core::convert::Infallible;

/// Sets up the device for transmission and sends the data
fn send(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
Expand Down
43 changes: 21 additions & 22 deletions avr-hal-generic/src/usart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use core::cmp::Ordering;
use core::marker;
use void::ResultVoidExt;

use crate::port;

Expand Down Expand Up @@ -62,7 +61,7 @@ impl<CLOCK: crate::clock::Clock> Baudrate<CLOCK> {
Baudrate {
ubrr: ubrr as u16,
u2x,
_clock: ::core::marker::PhantomData,
_clock: marker::PhantomData,
}
}

Expand All @@ -73,7 +72,7 @@ impl<CLOCK: crate::clock::Clock> Baudrate<CLOCK> {
Baudrate {
ubrr,
u2x,
_clock: ::core::marker::PhantomData,
_clock: marker::PhantomData,
}
}

Expand Down Expand Up @@ -184,21 +183,21 @@ pub trait UsartOps<H, RX, TX> {
/// was flushed yet.
///
/// **Warning**: This is a low-level method and should not be called directly from user code.
fn raw_flush(&mut self) -> nb::Result<(), void::Void>;
fn raw_flush(&mut self) -> nb::Result<(), core::convert::Infallible>;
/// Write a byte to the TX buffer.
///
/// This operation must be non-blocking and return [`nb::Error::WouldBlock`] until the byte is
/// enqueued. The operation should not wait for the byte to have actually been sent.
///
/// **Warning**: This is a low-level method and should not be called directly from user code.
fn raw_write(&mut self, byte: u8) -> nb::Result<(), void::Void>;
fn raw_write(&mut self, byte: u8) -> nb::Result<(), core::convert::Infallible>;
/// Read a byte from the RX buffer.
///
/// This operation must be non-blocking and return [`nb::Error::WouldBlock`] if no incoming
/// byte is available.
///
/// **Warning**: This is a low-level method and should not be called directly from user code.
fn raw_read(&mut self) -> nb::Result<u8, void::Void>;
fn raw_read(&mut self) -> nb::Result<u8, core::convert::Infallible>;

/// Enable/Disable a certain interrupt.
///
Expand All @@ -220,11 +219,11 @@ pub trait UsartOps<H, RX, TX> {
/// 57600.into_baudrate(),
/// );
///
/// ufmt::uwriteln!(&mut serial, "Hello from Arduino!\r").void_unwrap();
/// ufmt::uwriteln!(&mut serial, "Hello from Arduino!\r").unwrap();
///
/// loop {
/// let b = nb::block!(serial.read()).void_unwrap();
/// ufmt::uwriteln!(&mut serial, "Got {}!\r", b).void_unwrap();
/// let b = nb::block!(serial.read()).unwrap();
/// ufmt::uwriteln!(&mut serial, "Got {}!\r", b).unwrap();
/// }
/// ```
pub struct Usart<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> {
Expand Down Expand Up @@ -279,22 +278,22 @@ impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> Usart<H, USART, RX, TX, CLOCK

/// Block until all remaining data has been transmitted.
pub fn flush(&mut self) {
nb::block!(self.p.raw_flush()).void_unwrap()
nb::block!(self.p.raw_flush()).unwrap()
}

/// Transmit a byte.
///
/// This method will block until the byte has been enqueued for transmission but **not** until
/// it was entirely sent.
pub fn write_byte(&mut self, byte: u8) {
nb::block!(self.p.raw_write(byte)).void_unwrap()
nb::block!(self.p.raw_write(byte)).unwrap()
}

/// Receive a byte.
///
/// This method will block until a byte could be received.
pub fn read_byte(&mut self) -> u8 {
nb::block!(self.p.raw_read()).void_unwrap()
nb::block!(self.p.raw_read()).unwrap()
}

/// Enable the interrupt for [`Event`].
Expand Down Expand Up @@ -336,7 +335,7 @@ impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> Usart<H, USART, RX, TX, CLOCK
}

impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> ufmt::uWrite for Usart<H, USART, RX, TX, CLOCK> {
type Error = void::Void;
type Error = core::convert::Infallible;

fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
for b in s.as_bytes().iter() {
Expand All @@ -349,7 +348,7 @@ impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> ufmt::uWrite for Usart<H, USA
impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> hal::serial::Write<u8>
for Usart<H, USART, RX, TX, CLOCK>
{
type Error = void::Void;
type Error = core::convert::Infallible;

fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
self.p.raw_write(byte)
Expand All @@ -363,7 +362,7 @@ impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> hal::serial::Write<u8>
impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> hal::serial::Read<u8>
for Usart<H, USART, RX, TX, CLOCK>
{
type Error = void::Void;
type Error = core::convert::Infallible;

fn read(&mut self) -> nb::Result<u8, Self::Error> {
self.p.raw_read()
Expand Down Expand Up @@ -434,11 +433,11 @@ impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> UsartReader<H, USART, RX, TX,
impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> ufmt::uWrite
for UsartWriter<H, USART, RX, TX, CLOCK>
{
type Error = void::Void;
type Error = core::convert::Infallible;

fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
for b in s.as_bytes().iter() {
nb::block!(self.p.raw_write(*b)).void_unwrap()
nb::block!(self.p.raw_write(*b)).unwrap()
}
Ok(())
}
Expand All @@ -447,7 +446,7 @@ impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> ufmt::uWrite
impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> hal::serial::Write<u8>
for UsartWriter<H, USART, RX, TX, CLOCK>
{
type Error = void::Void;
type Error = core::convert::Infallible;

fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
self.p.raw_write(byte)
Expand All @@ -461,7 +460,7 @@ impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> hal::serial::Write<u8>
impl<H, USART: UsartOps<H, RX, TX>, RX, TX, CLOCK> hal::serial::Read<u8>
for UsartReader<H, USART, RX, TX, CLOCK>
{
type Error = void::Void;
type Error = core::convert::Infallible;

fn read(&mut self) -> nb::Result<u8, Self::Error> {
self.p.raw_read()
Expand Down Expand Up @@ -509,23 +508,23 @@ macro_rules! impl_usart_traditional {
self.[<ucsr $n b>].reset();
}

fn raw_flush(&mut self) -> $crate::nb::Result<(), $crate::void::Void> {
fn raw_flush(&mut self) -> $crate::nb::Result<(), core::convert::Infallible> {
if self.[<ucsr $n a>].read().[<udre $n>]().bit_is_clear() {
Err($crate::nb::Error::WouldBlock)
} else {
Ok(())
}
}

fn raw_write(&mut self, byte: u8) -> $crate::nb::Result<(), $crate::void::Void> {
fn raw_write(&mut self, byte: u8) -> $crate::nb::Result<(), core::convert::Infallible> {
// Call flush to make sure the data-register is empty
self.raw_flush()?;

self.[<udr $n>].write(|w| unsafe { w.bits(byte) });
Ok(())
}

fn raw_read(&mut self) -> $crate::nb::Result<u8, $crate::void::Void> {
fn raw_read(&mut self) -> $crate::nb::Result<u8, core::convert::Infallible> {
if self.[<ucsr $n a>].read().[<rxc $n>]().bit_is_clear() {
return Err($crate::nb::Error::WouldBlock);
}
Expand Down
9 changes: 4 additions & 5 deletions examples/arduino-diecimila/src/bin/diecimila-adc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_std]
#![no_main]

use arduino_hal::prelude::*;
use panic_halt as _;

use arduino_hal::adc;
Expand All @@ -18,8 +17,8 @@ fn main() -> ! {
adc.read_blocking(&adc::channel::Vbg),
adc.read_blocking(&adc::channel::Gnd),
);
ufmt::uwriteln!(&mut serial, "Vbandgap: {}", vbg).void_unwrap();
ufmt::uwriteln!(&mut serial, "Ground: {}", gnd).void_unwrap();
ufmt::uwriteln!(&mut serial, "Vbandgap: {}", vbg).unwrap();
ufmt::uwriteln!(&mut serial, "Ground: {}", gnd).unwrap();

// To store multiple channels in an array, we use the `into_channel()` method.
let channels: [adc::Channel; 6] = [
Expand All @@ -34,10 +33,10 @@ fn main() -> ! {
loop {
for (i, ch) in channels.iter().enumerate() {
let v = adc.read_blocking(ch);
ufmt::uwrite!(&mut serial, "A{}: {} ", i, v).void_unwrap();
ufmt::uwrite!(&mut serial, "A{}: {} ", i, v).unwrap();
}

ufmt::uwriteln!(&mut serial, "").void_unwrap();
ufmt::uwriteln!(&mut serial, "").unwrap();
arduino_hal::delay_ms(1000);
}
}
9 changes: 4 additions & 5 deletions examples/arduino-diecimila/src/bin/diecimila-i2cdetect.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_std]
#![no_main]

use arduino_hal::prelude::*;
use panic_halt as _;

#[arduino_hal::entry]
Expand All @@ -17,12 +16,12 @@ fn main() -> ! {
50000,
);

ufmt::uwriteln!(&mut serial, "Write direction test:\r").void_unwrap();
ufmt::uwriteln!(&mut serial, "Write direction test:\r").unwrap();
i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write)
.void_unwrap();
ufmt::uwriteln!(&mut serial, "\r\nRead direction test:\r").void_unwrap();
.unwrap();
ufmt::uwriteln!(&mut serial, "\r\nRead direction test:\r").unwrap();
i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read)
.void_unwrap();
.unwrap();

loop {}
}
7 changes: 3 additions & 4 deletions examples/arduino-diecimila/src/bin/diecimila-spi-feedback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#![no_std]
#![no_main]

use arduino_hal::prelude::*;
use arduino_hal::spi;
use embedded_hal_v0::spi::FullDuplex;
use panic_halt as _;
Expand All @@ -40,11 +39,11 @@ fn main() -> ! {

loop {
// Send a byte
nb::block!(spi.send(0b00001111)).void_unwrap();
nb::block!(spi.send(0b00001111)).unwrap();
// Because MISO is connected to MOSI, the read data should be the same
let data = nb::block!(spi.read()).void_unwrap();
let data = nb::block!(spi.read()).unwrap();

ufmt::uwriteln!(&mut serial, "data: {}\r", data).void_unwrap();
ufmt::uwriteln!(&mut serial, "data: {}\r", data).unwrap();
arduino_hal::delay_ms(1000);
}
}
7 changes: 3 additions & 4 deletions examples/arduino-diecimila/src/bin/diecimila-usart.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_std]
#![no_main]

use arduino_hal::prelude::*;
use panic_halt as _;

use embedded_hal_v0::serial::Read;
Expand All @@ -12,13 +11,13 @@ fn main() -> ! {
let pins = arduino_hal::pins!(dp);
let mut serial = arduino_hal::default_serial!(dp, pins, 57600);

ufmt::uwriteln!(&mut serial, "Hello from Arduino!\r").void_unwrap();
ufmt::uwriteln!(&mut serial, "Hello from Arduino!\r").unwrap();

loop {
// Read a byte from the serial connection default
let b = nb::block!(serial.read()).void_unwrap();
let b = nb::block!(serial.read()).unwrap();

// Answer
ufmt::uwriteln!(&mut serial, "Got {}!\r", b).void_unwrap();
ufmt::uwriteln!(&mut serial, "Got {}!\r", b).unwrap();
}
}
Loading

0 comments on commit 411b110

Please sign in to comment.