Skip to content

Commit

Permalink
delay: make infallible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Feb 28, 2023
1 parent ef20420 commit d3f8fe6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
3 changes: 3 additions & 0 deletions embedded-hal-async/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
- delay: make infallible.

## [v0.2.0-alpha.0] - 2022-11-23

- Switch all traits to use [`async_fn_in_trait`](https://blog.rust-lang.org/inside-rust/2022/11/17/async-fn-in-trait-nightly.html) (AFIT). Requires `nightly-2022-11-22` or newer.
Expand Down
13 changes: 4 additions & 9 deletions embedded-hal-async/src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@

/// Microsecond delay
pub trait DelayUs {
/// Enumeration of errors
type Error: core::fmt::Debug;

/// Pauses execution for at minimum `us` microseconds. Pause can be longer
/// if the implementation requires it due to precision/timing issues.
async fn delay_us(&mut self, us: u32) -> Result<(), Self::Error>;
async fn delay_us(&mut self, us: u32);

/// Pauses execution for at minimum `ms` milliseconds. Pause can be longer
/// if the implementation requires it due to precision/timing issues.
async fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error>;
async fn delay_ms(&mut self, ms: u32);
}

impl<T> DelayUs for &mut T
where
T: DelayUs,
{
type Error = T::Error;

async fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
async fn delay_us(&mut self, us: u32) {
T::delay_us(self, us).await
}

async fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
async fn delay_ms(&mut self, ms: u32) {
T::delay_ms(self, ms).await
}
}
1 change: 1 addition & 0 deletions embedded-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed
- gpio: add `ErrorKind` enum for consistency with other traits and for future extensibility. No kinds are defined for now.
- delay: make infallible.

## [v1.0.0-alpha.9] - 2022-09-28

Expand Down
17 changes: 5 additions & 12 deletions embedded-hal/src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,28 @@
/// Microsecond delay
///
pub trait DelayUs {
/// Enumeration of `DelayUs` errors
type Error: core::fmt::Debug;

/// Pauses execution for at minimum `us` microseconds. Pause can be longer
/// if the implementation requires it due to precision/timing issues.
fn delay_us(&mut self, us: u32) -> Result<(), Self::Error>;
fn delay_us(&mut self, us: u32);

/// Pauses execution for at minimum `ms` milliseconds. Pause can be longer
/// if the implementation requires it due to precision/timing issues.
fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
fn delay_ms(&mut self, ms: u32) {
for _ in 0..ms {
self.delay_us(1000)?;
self.delay_us(1000);
}

Ok(())
}
}

impl<T> DelayUs for &mut T
where
T: DelayUs,
{
type Error = T::Error;

fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
fn delay_us(&mut self, us: u32) {
T::delay_us(self, us)
}

fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
fn delay_ms(&mut self, ms: u32) {
T::delay_ms(self, ms)
}
}

0 comments on commit d3f8fe6

Please sign in to comment.