use std::{
any::{Any, TypeId},
sync::Arc,
};
use downcast_rs::{impl_downcast, DowncastSync};
use crate::{
rand::GlobalRng,
task::{NodeId, Spawner},
time::TimeHandle,
Config,
};
pub trait Simulator: Any + Send + Sync + DowncastSync {
fn new(rand: &GlobalRng, time: &TimeHandle, config: &Config) -> Self
where
Self: Sized;
#[doc(hidden)]
fn new1(rand: &GlobalRng, time: &TimeHandle, _task: &Spawner, config: &Config) -> Self
where
Self: Sized,
{
Self::new(rand, time, config)
}
fn create_node(&self, _id: NodeId) {}
fn reset_node(&self, _id: NodeId) {}
}
impl_downcast!(sync Simulator);
pub fn simulator<S: Simulator>() -> Arc<S> {
crate::context::current(|h| {
let sims = h.sims.lock();
sims[&TypeId::of::<S>()]
.clone()
.downcast_arc()
.ok()
.unwrap()
})
}
pub fn node() -> NodeId {
crate::context::current_node()
}