diff --git a/core/Cargo.toml b/core/Cargo.toml index 1103ffa..9067fc1 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -5,6 +5,7 @@ edition = "2024" [features] default = [] +tokio = ["dep:tokio"] test-util = ["dep:tokio"] [dependencies] diff --git a/core/src/rt.rs b/core/src/rt.rs index b7ce18f..b40c7a7 100644 --- a/core/src/rt.rs +++ b/core/src/rt.rs @@ -1,5 +1,30 @@ use eva::{auto_impl, fut::Fut}; +#[cfg(feature = "tokio")] +mod tokio_rt { + use super::*; + + #[derive(Debug, Clone, Copy)] + pub struct TokioRt; + + impl RtRef for TokioRt {} + impl RtMut for TokioRt { + fn spawn + 'static>(&mut self, task: F) { + tokio::spawn(task); + } + + fn spawn_blocking(&mut self, task: F) -> impl Fut { + async move { + let ret = tokio::task::spawn_blocking(task).await; + ret.expect("failed to join blocking task") + } + } + } +} + +#[cfg(feature = "tokio")] +pub use tokio_rt::*; + #[auto_impl(&, &mut)] pub trait RtRef: Send + Sync {} diff --git a/core/src/world.rs b/core/src/world.rs index baa42e4..86be68e 100644 --- a/core/src/world.rs +++ b/core/src/world.rs @@ -2,6 +2,9 @@ use eva::{auto_impl, rand, time::Clock}; use crate::rt; +#[cfg(feature = "tokio")] +pub mod real; + #[cfg(feature = "test-util")] pub mod testing; diff --git a/core/src/world/real.rs b/core/src/world/real.rs new file mode 100644 index 0000000..30fc2ec --- /dev/null +++ b/core/src/world/real.rs @@ -0,0 +1,43 @@ +use crate::{ + rt, + world::{Demiurge, World, WorldMut, WorldRef}, +}; + +use eva::{ + rand, + time::{Clock, RealTime, Timestamp}, +}; + +pub struct God; + +impl Demiurge for God { + type World = RealWorld; + + fn make_world(&self) -> World { + World(RealWorld) + } +} + +pub struct RealWorld; + +impl WorldRef for RealWorld { + fn rt(&self) -> impl rt::RtRef { + rt::TokioRt + } +} + +impl WorldMut for RealWorld { + fn rng(&mut self) -> impl rand::Rng { + rand::rng() + } + + fn rt_mut(&mut self) -> impl rt::RtMut { + rt::TokioRt + } +} + +impl Clock for RealWorld { + fn get(&self) -> Timestamp { + Clock::get(&RealTime::default()) + } +}