use crate::all_storages::{AllStoragesBuilder, LockPresent, ThreadIdPresent};
use crate::atomic_refcell::AtomicRefCell;
use crate::public_transport::ShipyardRwLock;
use crate::world::World;
use alloc::sync::Arc;
use core::sync::atomic::AtomicU64;
pub struct WorldBuilder<Lock, ThreadId> {
all_storages_builder: AllStoragesBuilder<Lock, ThreadId>,
#[cfg(feature = "parallel")]
thread_pool: Option<rayon::ThreadPool>,
}
impl World {
#[cfg(feature = "std")]
pub fn builder() -> WorldBuilder<LockPresent, ThreadIdPresent> {
WorldBuilder {
all_storages_builder: AllStoragesBuilder::<LockPresent, ThreadIdPresent>::new(),
#[cfg(feature = "parallel")]
thread_pool: None,
}
}
#[cfg(all(not(feature = "std"), not(feature = "thread_local")))]
pub fn builder() -> WorldBuilder<crate::all_storages::MissingLock, ThreadIdPresent> {
WorldBuilder {
all_storages_builder: AllStoragesBuilder::<
crate::all_storages::MissingLock,
ThreadIdPresent,
>::new(),
}
}
#[cfg(all(not(feature = "std"), feature = "thread_local"))]
pub fn builder(
) -> WorldBuilder<crate::all_storages::MissingLock, crate::all_storages::MissingThreadId> {
WorldBuilder {
all_storages_builder: AllStoragesBuilder::<
crate::all_storages::MissingLock,
crate::all_storages::MissingThreadId,
>::new(),
}
}
}
impl<Lock, ThreadId> WorldBuilder<Lock, ThreadId> {
pub fn with_custom_lock<L: ShipyardRwLock + Send + Sync>(
self,
) -> WorldBuilder<LockPresent, ThreadId> {
WorldBuilder {
all_storages_builder: self.all_storages_builder.with_custom_lock::<L>(),
#[cfg(feature = "parallel")]
thread_pool: self.thread_pool,
}
}
#[cfg(feature = "thread_local")]
pub fn with_custom_thread_id(
self,
thread_id: impl Fn() -> u64 + Send + Sync + 'static,
) -> WorldBuilder<Lock, ThreadIdPresent> {
WorldBuilder {
all_storages_builder: self.all_storages_builder.with_custom_thread_id(thread_id),
#[cfg(feature = "parallel")]
thread_pool: self.thread_pool,
}
}
#[cfg(feature = "parallel")]
pub fn with_local_thread_pool(
mut self,
thread_pool: rayon::ThreadPool,
) -> WorldBuilder<Lock, ThreadId> {
self.thread_pool = Some(thread_pool);
self
}
}
impl WorldBuilder<LockPresent, ThreadIdPresent> {
pub fn build(self) -> World {
let counter = Arc::new(AtomicU64::new(1));
let all_storages = self.all_storages_builder.build(counter.clone());
World {
all_storages,
scheduler: AtomicRefCell::new(Default::default()),
counter,
#[cfg(feature = "parallel")]
thread_pool: self.thread_pool,
}
}
}