Skip to content

Fix error types #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ readme = "README.md"
repository = "https://github.com/japaric/embedded-hal"
version = "0.2.3"

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

[dependencies.nb]
version = "0.1.1"

Expand Down
3 changes: 2 additions & 1 deletion src/digital/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ pub trait ToggleableOutputPin {
/// ```
/// use embedded_hal::digital::v2::{OutputPin, StatefulOutputPin, ToggleableOutputPin};
/// use embedded_hal::digital::v2::toggleable;
/// use std::convert::Infallible;
///
/// /// A virtual output pin that exists purely in software
/// struct MyPin {
/// state: bool
/// }
///
/// impl OutputPin for MyPin {
/// type Error = void::Void;
/// type Error = Infallible;
///
/// fn set_low(&mut self) -> Result<(), Self::Error> {
/// self.state = false;
Expand Down
8 changes: 4 additions & 4 deletions src/digital/v2_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
//! ```
//!

use core::convert::Infallible;

#[allow(deprecated)]
use super::v1;
use super::v2;
Expand All @@ -42,8 +44,7 @@ impl <T> v2::OutputPin for T
where
T: v1::OutputPin,
{
// TODO: update to ! when never_type is stabilized
type Error = ();
type Error = Infallible;

fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
Expand Down Expand Up @@ -81,8 +82,7 @@ impl <T> v2::InputPin for T
where
T: v1::InputPin
{
// TODO: update to ! when never_type is stabilized
type Error = ();
type Error = Infallible;

fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
Expand Down
71 changes: 32 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@
//! [`WouldBlock`]: https://docs.rs/nb/0.1.0/nb/enum.Error.html
//!
//! Some traits, like the one shown below, may expose possibly blocking APIs that can't fail. In
//! those cases `nb::Result<_, Void>` is used.
//! those cases `nb::Result<_, Infallible>` is used.
//!
//! ```
//! extern crate nb;
//! extern crate void;
//!
//! use void::Void;
//! # use std as core;
//! use core::convert::Infallible;
//!
//! /// A count down timer
//! pub trait CountDown {
//! // ..
//!
//! /// "waits" until the count down is over
//! fn wait(&mut self) -> nb::Result<(), Void>;
//! fn wait(&mut self) -> nb::Result<(), Infallible>;
//! }
//!
//! # fn main() {}
Expand Down Expand Up @@ -223,11 +223,10 @@
//! # }
//!
//! # mod stm32f30x_hal {
//! # extern crate void;
//! # use self::void::Void;
//! # use std::convert::Infallible;
//! # pub struct Serial1;
//! # impl Serial1 {
//! # pub fn write(&mut self, _: u8) -> ::nb::Result<(), Void> {
//! # pub fn write(&mut self, _: u8) -> ::nb::Result<(), Infallible> {
//! # Ok(())
//! # }
//! # }
Expand All @@ -242,7 +241,6 @@
//! ```
//! extern crate embedded_hal as hal;
//! extern crate futures;
//! extern crate void;
//!
//! #[macro_use(try_nb)]
//! extern crate nb;
Expand All @@ -255,12 +253,12 @@
//! };
//! use futures::future::Loop;
//! use stm32f30x_hal::{Led, Serial1, Timer6};
//! use void::Void;
//! use std::convert::Infallible;
//!
//! /// `futures` version of `CountDown.wait`
//! ///
//! /// This returns a future that must be polled to completion
//! fn wait<T>(mut timer: T) -> impl Future<Item = T, Error = Void>
//! fn wait<T>(mut timer: T) -> impl Future<Item = T, Error = Infallible>
//! where
//! T: hal::timer::CountDown,
//! {
Expand Down Expand Up @@ -342,32 +340,31 @@
//!
//! // Event loop
//! loop {
//! blinky.poll().unwrap(); // NOTE(unwrap) E = Void
//! blinky.poll().unwrap(); // NOTE(unwrap) E = Infallible
//! loopback.poll().unwrap();
//! # break;
//! }
//! }
//!
//! # mod stm32f30x_hal {
//! # extern crate void;
//! # use self::void::Void;
//! # use std::convert::Infallible;
//! # pub struct Timer6;
//! # impl ::hal::timer::CountDown for Timer6 {
//! # type Time = ();
//! #
//! # fn start<T>(&mut self, _: T) where T: Into<()> {}
//! # fn wait(&mut self) -> ::nb::Result<(), Void> { Err(::nb::Error::WouldBlock) }
//! # fn wait(&mut self) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
//! # }
//! #
//! # pub struct Serial1;
//! # impl ::hal::serial::Read<u8> for Serial1 {
//! # type Error = Void;
//! # fn read(&mut self) -> ::nb::Result<u8, Void> { Err(::nb::Error::WouldBlock) }
//! # type Error = Infallible;
//! # fn read(&mut self) -> ::nb::Result<u8, Infallible> { Err(::nb::Error::WouldBlock) }
//! # }
//! # impl ::hal::serial::Write<u8> for Serial1 {
//! # type Error = Void;
//! # fn flush(&mut self) -> ::nb::Result<(), Void> { Err(::nb::Error::WouldBlock) }
//! # fn write(&mut self, _: u8) -> ::nb::Result<(), Void> { Err(::nb::Error::WouldBlock) }
//! # type Error = Infallible;
//! # fn flush(&mut self) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
//! # fn write(&mut self, _: u8) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
//! # }
//! #
//! # pub struct Led;
Expand Down Expand Up @@ -418,7 +415,7 @@
//! loop {
//! // `await!` means "suspend / yield here" instead of "block until
//! // completion"
//! await!(timer.wait()).unwrap(); // NOTE(unwrap) E = Void
//! await!(timer.wait()).unwrap(); // NOTE(unwrap) E = Infallible
//!
//! state = !state;
//!
Expand Down Expand Up @@ -446,16 +443,15 @@
//! }
//!
//! # mod stm32f30x_hal {
//! # extern crate void;
//! # use self::void::Void;
//! # use std::convert::Infallible;
//! # pub struct Serial1;
//! # impl Serial1 {
//! # pub fn read(&mut self) -> ::nb::Result<u8, Void> { Err(::nb::Error::WouldBlock) }
//! # pub fn write(&mut self, _: u8) -> ::nb::Result<(), Void> { Err(::nb::Error::WouldBlock) }
//! # pub fn read(&mut self) -> ::nb::Result<u8, Infallible> { Err(::nb::Error::WouldBlock) }
//! # pub fn write(&mut self, _: u8) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
//! # }
//! # pub struct Timer6;
//! # impl Timer6 {
//! # pub fn wait(&mut self) -> ::nb::Result<(), Void> { Err(::nb::Error::WouldBlock) }
//! # pub fn wait(&mut self) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
//! # }
//! # pub struct Led;
//! # impl Led {
Expand Down Expand Up @@ -594,16 +590,16 @@
//! - Buffered serial interface with periodic flushing in interrupt handler
//!
//! ```
//! # use std as core;
//! extern crate embedded_hal as hal;
//! extern crate nb;
//! extern crate void;
//!
//! use hal::prelude::*;
//! use void::Void;
//! use core::convert::Infallible;
//!
//! fn flush<S>(serial: &mut S, cb: &mut CircularBuffer)
//! where
//! S: hal::serial::Write<u8, Error = Void>,
//! S: hal::serial::Write<u8, Error = Infallible>,
//! {
//! loop {
//! if let Some(byte) = cb.peek() {
Expand Down Expand Up @@ -668,9 +664,9 @@
//! # }
//! # struct Serial1;
//! # impl ::hal::serial::Write<u8> for Serial1 {
//! # type Error = Void;
//! # fn write(&mut self, _: u8) -> nb::Result<(), Void> { Err(::nb::Error::WouldBlock) }
//! # fn flush(&mut self) -> nb::Result<(), Void> { Err(::nb::Error::WouldBlock) }
//! # type Error = Infallible;
//! # fn write(&mut self, _: u8) -> nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
//! # fn flush(&mut self) -> nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
//! # }
//! # struct CircularBuffer;
//! # impl CircularBuffer {
Expand All @@ -687,7 +683,6 @@

#[macro_use]
extern crate nb;
extern crate void;

pub mod adc;
pub mod blocking;
Expand Down Expand Up @@ -731,8 +726,7 @@ pub mod watchdog;
/// println!("Period: {} ms", period);
/// }
///
/// # extern crate void;
/// # use void::Void;
/// # use std::convert::Infallible;
/// # struct MilliSeconds(u32);
/// # trait U32Ext { fn ms(self) -> MilliSeconds; }
/// # impl U32Ext for u32 { fn ms(self) -> MilliSeconds { MilliSeconds(self) } }
Expand All @@ -741,9 +735,9 @@ pub mod watchdog;
/// # impl hal::Capture for Capture1 {
/// # type Capture = u16;
/// # type Channel = Channel;
/// # type Error = Void;
/// # type Error = Infallible;
/// # type Time = MilliSeconds;
/// # fn capture(&mut self, _: Channel) -> ::nb::Result<u16, Void> { Ok(0) }
/// # fn capture(&mut self, _: Channel) -> ::nb::Result<u16, Infallible> { Ok(0) }
/// # fn disable(&mut self, _: Channel) { unimplemented!() }
/// # fn enable(&mut self, _: Channel) { unimplemented!() }
/// # fn get_resolution(&self) -> MilliSeconds { unimplemented!() }
Expand Down Expand Up @@ -948,8 +942,7 @@ pub trait PwmPin {
/// println!("Speed: {} pulses per second", speed);
/// }
///
/// # extern crate void;
/// # use void::Void;
/// # use std::convert::Infallible;
/// # struct Seconds(u32);
/// # trait U32Ext { fn s(self) -> Seconds; }
/// # impl U32Ext for u32 { fn s(self) -> Seconds { Seconds(self) } }
Expand All @@ -963,7 +956,7 @@ pub trait PwmPin {
/// # impl hal::timer::CountDown for Timer6 {
/// # type Time = Seconds;
/// # fn start<T>(&mut self, _: T) where T: Into<Seconds> {}
/// # fn wait(&mut self) -> ::nb::Result<(), Void> { Ok(()) }
/// # fn wait(&mut self) -> ::nb::Result<(), Infallible> { Ok(()) }
/// # }
/// ```
#[cfg(feature = "unproven")]
Expand Down
9 changes: 4 additions & 5 deletions src/timer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Timers

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

/// A count down timer
///
Expand Down Expand Up @@ -40,8 +40,7 @@ use void::Void;
/// Led.off();
/// }
///
/// # extern crate void;
/// # use void::Void;
/// # use std::convert::Infallible;
/// # struct Seconds(u32);
/// # trait U32Ext { fn s(self) -> Seconds; }
/// # impl U32Ext for u32 { fn s(self) -> Seconds { Seconds(self) } }
Expand All @@ -54,7 +53,7 @@ use void::Void;
/// # impl hal::timer::CountDown for Timer6 {
/// # type Time = Seconds;
/// # fn start<T>(&mut self, _: T) where T: Into<Seconds> {}
/// # fn wait(&mut self) -> ::nb::Result<(), Void> { Ok(()) }
/// # fn wait(&mut self) -> ::nb::Result<(), Infallible> { Ok(()) }
/// # }
/// ```
pub trait CountDown {
Expand All @@ -74,7 +73,7 @@ pub trait CountDown {
/// finishes.
/// - Otherwise the behavior of calling `wait` after the last call returned `Ok` is UNSPECIFIED.
/// Implementers are suggested to panic on this scenario to signal a programmer error.
fn wait(&mut self) -> nb::Result<(), Void>;
fn wait(&mut self) -> nb::Result<(), Infallible>;
}

/// Marker trait that indicates that a timer is periodic
Expand Down