Skip to main content

moonpool_sim/providers/
task.rs

1//! Task provider backed by the moonpool deterministic executor.
2
3use std::future::Future;
4
5use moonpool_core::TaskProvider;
6
7/// Task provider that spawns onto the [deterministic
8/// executor](crate::executor) driving the current simulation iteration.
9///
10/// Where `TokioTaskProvider` binds tasks to the ambient tokio runtime, this
11/// provider binds them to the executor installed by
12/// [`Executor::block_on`](crate::executor::Executor::block_on): scheduling
13/// order is a seeded-random, fully reproducible function of the iteration
14/// seed.
15///
16/// # Panics
17///
18/// `spawn_task` panics when used outside `Executor::block_on` (mirroring
19/// `tokio::spawn` outside a runtime).
20#[derive(Clone, Debug, Default)]
21pub struct SimTaskProvider;
22
23impl TaskProvider for SimTaskProvider {
24    type JoinHandle = crate::executor::JoinHandle<()>;
25
26    fn spawn_task<F>(&self, name: &str, future: F) -> Self::JoinHandle
27    where
28        F: Future<Output = ()> + Send + 'static,
29    {
30        // No lifecycle-trace wrapper (unlike TokioTaskProvider, where the
31        // name would otherwise be lost): the executor stores the name in
32        // TaskMeta and traces every poll of the task with it.
33        crate::executor::spawn(name, future)
34    }
35
36    async fn yield_now(&self) {
37        crate::executor::yield_now().await;
38    }
39}