Skip to content

unify DelayUs and DelayMs to DelayUs for v1.0 #312

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 1 commit into from
Oct 5, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Removed
- Removed `DelayMs` in favor of `DelayUs` with `u32` as type for clarity.

## [v1.0.0-alpha.5] - 2021-09-11

Expand Down
44 changes: 18 additions & 26 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,34 @@

/// Blocking delay traits
pub mod blocking {
/// Millisecond delay
/// Microsecond delay
///
/// `UXX` denotes the range type of the delay time. `UXX` can be `u8`, `u16`, etc. A single type can
/// implement this trait for different types of `UXX`.
pub trait DelayMs<UXX> {
/// Enumeration of `DelayMs` errors
pub trait DelayUs {
/// Enumeration of `DelayUs` errors
type Error: core::fmt::Debug;

/// Pauses execution for `ms` milliseconds
fn delay_ms(&mut self, ms: UXX) -> Result<(), Self::Error>;
}
/// 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>;

impl<UXX, T: DelayMs<UXX>> DelayMs<UXX> for &mut T {
type Error = T::Error;
/// 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> {
for _ in 0..ms {
self.delay_us(1000)?;
}

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

/// Microsecond delay
///
/// `UXX` denotes the range type of the delay time. `UXX` can be `u8`, `u16`, etc. A single type can
/// implement this trait for different types of `UXX`.
pub trait DelayUs<UXX> {
/// Enumeration of `DelayMs` errors
type Error: core::fmt::Debug;

/// Pauses execution for `us` microseconds
fn delay_us(&mut self, us: UXX) -> Result<(), Self::Error>;
}

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

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