effect_rs/
runtime.rs

1use crate::core::{Effect, EnvRef, Exit};
2use tokio::runtime::Runtime as TokioRuntime;
3use tracing::Instrument;
4
5pub struct Runtime {
6    pub(crate) rt: tokio::runtime::Runtime,
7}
8
9impl Default for Runtime {
10    fn default() -> Self {
11        Self::new()
12    }
13}
14
15impl Runtime {
16    pub fn new() -> Self {
17        Self {
18            rt: TokioRuntime::new().unwrap(),
19        }
20    }
21    pub fn block_on<R, E, A>(&self, effect: Effect<R, E, A>, env: R) -> Exit<E, A>
22    where
23        R: Send + Sync + 'static,
24        E: Send + Sync + 'static,
25        A: Send + Sync + 'static,
26    {
27        self.rt.block_on(async move {
28            let ctx = crate::core::Ctx::new(); // Use Ctx::new() which sets defaults including locals
29
30            let result = (effect.inner)(EnvRef { value: env }, ctx.clone())
31                .instrument(tracing::info_span!("runtime_execution"))
32                .await;
33            ctx.scope.close(crate::core::ScopeExit::from(&result)).await;
34            result
35        })
36    }
37}