rtic_time/
lib.rs

1//! Time-related traits & structs.
2//!
3//! This crate contains basic definitions and utilities that can be used
4//! to keep track of time.
5
6#![no_std]
7#![deny(missing_docs)]
8#![allow(async_fn_in_trait)]
9
10pub mod half_period_counter;
11mod linked_list;
12pub mod monotonic;
13pub mod timer_queue;
14
15/// This indicates that there was a timeout.
16pub struct TimeoutError;
17
18/// Re-export for macros
19pub use embedded_hal;
20/// Re-export for macros
21pub use embedded_hal_async;
22
23/// # A monotonic clock / counter definition.
24///
25/// ## Correctness
26///
27/// The trait enforces that proper time-math is implemented between `Instant` and `Duration`. This
28/// is a requirement on the time library that the user chooses to use.
29pub trait Monotonic {
30    /// The type for instant, defining an instant in time.
31    ///
32    /// **Note:** In all APIs in RTIC that use instants from this monotonic, this type will be used.
33    type Instant: Ord
34        + Copy
35        + core::ops::Add<Self::Duration, Output = Self::Instant>
36        + core::ops::Sub<Self::Duration, Output = Self::Instant>
37        + core::ops::Sub<Self::Instant, Output = Self::Duration>;
38
39    /// The type for duration, defining a duration of time.
40    ///
41    /// **Note:** In all APIs in RTIC that use duration from this monotonic, this type will be used.
42    type Duration: Copy;
43
44    /// Get the current time.
45    fn now() -> Self::Instant;
46
47    /// Delay for some duration of time.
48    async fn delay(duration: Self::Duration);
49
50    /// Delay to some specific time instant.
51    async fn delay_until(instant: Self::Instant);
52
53    /// Timeout at a specific time.
54    async fn timeout_at<F: core::future::Future>(
55        instant: Self::Instant,
56        future: F,
57    ) -> Result<F::Output, TimeoutError>;
58
59    /// Timeout after a specific duration.
60    async fn timeout_after<F: core::future::Future>(
61        duration: Self::Duration,
62        future: F,
63    ) -> Result<F::Output, TimeoutError>;
64}