Skip to main content

dope_runtime/
coordinate.rs

1use std::{marker, time};
2
3use dope_core::driver::{self, schedule};
4use o3::cell::region;
5
6/// Whether a bounded application coordinator has another transition ready.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum Flow {
9    Idle,
10    More,
11}
12
13/// One linear, turn-bounded application coordination step.
14/// It consumes one coordination credit and borrows the driver region only for
15/// this step, preventing projected controls from escaping the callback.
16#[must_use = "dropping a coordinate step consumes its admitted application work"]
17pub struct Step<'step, 'turn, 'd> {
18    region: &'step mut region::Token<'d>,
19    now: time::Instant,
20    _turn: marker::PhantomData<&'turn mut ()>,
21    _driver: marker::PhantomData<fn(&'d ()) -> &'d ()>,
22}
23
24impl<'step, 'turn, 'd> Step<'step, 'turn, 'd> {
25    #[doc(hidden)]
26    pub fn try_new(
27        budget: &mut schedule::Coordination<'turn, 'd>,
28        driver: &'step mut driver::Context<'_, 'd>,
29    ) -> Option<Self> {
30        if !budget.take() {
31            return None;
32        }
33        let now = driver.turn_now();
34        let region = driver.region_token();
35        Some(Self {
36            region,
37            now,
38            _turn: marker::PhantomData,
39            _driver: marker::PhantomData,
40        })
41    }
42
43    /// Monotonic time captured once at the beginning of the driver turn.
44    pub const fn now(&self) -> time::Instant {
45        self.now
46    }
47
48    /// Region authority for this admitted step only.
49    pub fn region(&mut self) -> &mut region::Token<'d> {
50        self.region
51    }
52}