Skip to content

Commit 9c17bca

Browse files
bors[bot]Dirbaio
andauthored
Merge #344
344: async: add DelayUs r=eldruin a=Dirbaio Add async DelayUs, mirroring the blocking one. Unlike blocking, `delay_ms` has no default impl because it's not possible to have default impls in GAT-based async traits. Co-authored-by: Dario Nieuwenhuis <[email protected]>
2 parents baaaaa1 + 7b911a6 commit 9c17bca

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

.github/workflows/clippy-async.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
on:
2+
push:
3+
branches: [ staging, trying, master ]
4+
pull_request:
5+
6+
name: Clippy check
7+
jobs:
8+
clippy_check-async:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions-rs/toolchain@v1
13+
with:
14+
profile: minimal
15+
toolchain: nightly
16+
override: true
17+
components: clippy
18+
- run: cargo clippy
19+
working-directory: embedded-hal-async

.github/workflows/clippy.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,3 @@ jobs:
1818
- uses: actions-rs/clippy-check@v1
1919
with:
2020
token: ${{ secrets.GITHUB_TOKEN }}
21-
- run: cargo clippy
22-
working-directory: embedded-hal-async

embedded-hal-async/src/delay.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! Delays
2+
3+
use core::future::Future;
4+
5+
/// Microsecond delay
6+
pub trait DelayUs {
7+
/// Enumeration of errors
8+
type Error: core::fmt::Debug;
9+
10+
/// The future returned by the `delay_us` function.
11+
type DelayUsFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
12+
where
13+
Self: 'a;
14+
15+
/// Pauses execution for at minimum `us` microseconds. Pause can be longer
16+
/// if the implementation requires it due to precision/timing issues.
17+
fn delay_us(&mut self, us: u32) -> Self::DelayUsFuture<'_>;
18+
19+
/// The future returned by the `delay_ms` function.
20+
type DelayMsFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
21+
where
22+
Self: 'a;
23+
24+
/// Pauses execution for at minimum `ms` milliseconds. Pause can be longer
25+
/// if the implementation requires it due to precision/timing issues.
26+
fn delay_ms(&mut self, ms: u32) -> Self::DelayMsFuture<'_>;
27+
}
28+
29+
impl<T> DelayUs for &mut T
30+
where
31+
T: DelayUs,
32+
{
33+
type Error = T::Error;
34+
35+
type DelayUsFuture<'a>
36+
where
37+
Self: 'a,
38+
= T::DelayUsFuture<'a>;
39+
40+
fn delay_us(&mut self, us: u32) -> Self::DelayUsFuture<'_> {
41+
T::delay_us(self, us)
42+
}
43+
44+
type DelayMsFuture<'a>
45+
where
46+
Self: 'a,
47+
= T::DelayMsFuture<'a>;
48+
49+
fn delay_ms(&mut self, ms: u32) -> Self::DelayMsFuture<'_> {
50+
T::delay_ms(self, ms)
51+
}
52+
}

embedded-hal-async/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99
1010
#![deny(missing_docs)]
1111
#![no_std]
12+
#![feature(generic_associated_types)]
13+
14+
pub mod delay;

0 commit comments

Comments
 (0)