Skip to main content

execution_policy/core/
mod.rs

1//! Runtime abstraction: clock, sleeping, and RNG behind one object-safe trait.
2
3use std::future::Future;
4use std::pin::Pin;
5use std::time::{Duration, Instant};
6
7#[cfg(any(feature = "tokio", feature = "test-util"))]
8pub(crate) mod rng;
9
10#[cfg(feature = "tokio")]
11mod tokio;
12#[cfg(feature = "tokio")]
13pub use tokio::TokioCore;
14
15#[cfg(feature = "test-util")]
16mod test;
17#[cfg(feature = "test-util")]
18pub use test::{ManualClock, TestCore};
19
20/// A boxed future. [`Core::sleep`] returns this so the trait stays object-safe
21/// (`Arc<dyn Core>` works); the box is on the cold backoff path only.
22pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
23
24/// The policy engine's access to time, sleeping, and randomness.
25///
26/// Object-safe by construction. Implementors: [`TokioCore`] (default) and
27/// [`TestCore`] (deterministic, for tests).
28pub trait Core {
29    /// Current monotonic instant.
30    fn now(&self) -> Instant;
31    /// A future that completes after `dur` of this `Core`'s time.
32    fn sleep(&self, dur: Duration) -> BoxFuture<'_, ()>;
33    /// Next pseudo-random `u64` (used for jitter). Not cryptographic.
34    fn next_u64(&self) -> u64;
35}
36
37/// The `Core` used by `ExecutionPolicy::builder().build()`.
38#[cfg(feature = "tokio")]
39pub type DefaultCore = TokioCore;