pub use tick::Tick; mod tick{ #[derive(Clone,Copy,Default,Hash,PartialEq,Eq,PartialOrd,Ord)] pub struct Tick(u64); impl Tick{ pub const fn new(value:u64)->Self{ Self(value) } pub const fn get(&self)->u64{ self.0 } } impl std::ops::Add for Tick{ type Output=Self; fn add(self,rhs:u64)->Self::Output{ Self(self.0+rhs) } } impl std::ops::Sub for Tick{ type Output=Self; fn sub(self,rhs:u64)->Self::Output{ Self(self.0-rhs) } } impl std::ops::AddAssign for Tick{ fn add_assign(&mut self,rhs:u64){ self.0+=rhs; } } impl std::ops::SubAssign for Tick{ fn sub_assign(&mut self,rhs:u64){ self.0-=rhs; } } } #[derive(Default)] pub struct Scheduler{ tick:Tick, schedule:std::collections::HashMap>, } impl Scheduler{ pub const fn tick(&self)->Tick{ self.tick } pub fn has_scheduled_threads(&self)->bool{ !self.schedule.is_empty() } pub fn schedule_thread(&mut self,delay:u64,thread:mlua::Thread){ self.schedule.entry(self.tick+delay.max(1)) .or_insert(Vec::new()) .push(thread); } pub fn tick_threads(&mut self)->Option>{ self.tick+=1; self.schedule.remove(&self.tick) } }