Skip to main content

embassy_time_queue_utils/
lib.rs

1#![no_std]
2#![doc = include_str!("../README.md")]
3#![warn(missing_docs)]
4
5use core::task::Waker;
6
7pub mod queue_generic;
8
9// Not explicitly enabled - acts as default, but cannot be relied upon as an implementation detail.
10#[cfg(all(not(feature = "integrated-timers"), not(feature = "_generic-queue")))]
11mod queue_integrated;
12#[cfg(feature = "integrated-timers")]
13pub mod queue_integrated;
14
15#[cfg(all(not(feature = "integrated-timers"), feature = "_generic-queue"))]
16type QueueImpl = queue_generic::Queue;
17#[cfg(any(feature = "integrated-timers", not(feature = "_generic-queue")))]
18type QueueImpl = queue_integrated::Queue;
19
20/// The default timer queue, configured by the crate's features.
21///
22/// If any of the `generic-queue-X` features are enabled, this implements a generic
23/// timer queue of capacity X. Otherwise, it implements an integrated timer queue.
24#[derive(Debug)]
25pub struct Queue {
26    queue: QueueImpl,
27}
28
29impl Queue {
30    /// Creates a new timer queue.
31    pub const fn new() -> Self {
32        Self {
33            queue: QueueImpl::new(),
34        }
35    }
36
37    /// Schedules a task to run at a specific time, and returns whether any changes were made.
38    ///
39    /// If this function returns `true`, the caller should find the next expiration time and set
40    /// a new alarm for that time.
41    pub fn schedule_wake(&mut self, at: u64, waker: &Waker) -> bool {
42        self.queue.schedule_wake(at, waker)
43    }
44
45    /// Dequeues expired timers and returns the next alarm time.
46    pub fn next_expiration(&mut self, now: u64) -> u64 {
47        self.queue.next_expiration(now)
48    }
49}