add real world

This commit is contained in:
Aleksandr 2025-09-20 22:22:19 +03:00
parent bd9b07052b
commit d171fc723b
4 changed files with 72 additions and 0 deletions

View file

@ -5,6 +5,7 @@ edition = "2024"
[features]
default = []
tokio = ["dep:tokio"]
test-util = ["dep:tokio"]
[dependencies]

View file

@ -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<F: Fut<Output: Send> + 'static>(&mut self, task: F) {
tokio::spawn(task);
}
fn spawn_blocking<F: BlockingFn>(&mut self, task: F) -> impl Fut<Output = F::Ret> {
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 {}

View file

@ -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;

43
core/src/world/real.rs Normal file
View file

@ -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<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())
}
}