pub struct EventScheduler {
pub current_time: f64,
pub event_queue: BinaryHeap<Event>,
pub event_log: Vec<(Event, Option<String>)>,
}Expand description
Manages and schedules events using a priority queue.
The EventScheduler executes events based on their scheduled time, maintaining an event log
and allowing for conditional execution (e.g., stop after a certain time or when certain criteria are met).
§Fields
current_time: The current time in the simulation, updated as events are processed.event_queue: A binary heap used as a priority queue for storing scheduled events.event_log: A log that stores all events executed and their results.
Fields§
§current_time: f64§event_queue: BinaryHeap<Event>§event_log: Vec<(Event, Option<String>)>Implementations§
Source§impl EventScheduler
impl EventScheduler
Sourcepub fn timeout(
&mut self,
delay: f64,
action: Option<Box<dyn FnMut(&mut EventScheduler) -> Option<String>>>,
context: Option<HashMap<String, String>>,
)
pub fn timeout( &mut self, delay: f64, action: Option<Box<dyn FnMut(&mut EventScheduler) -> Option<String>>>, context: Option<HashMap<String, String>>, )
Schedules a timeout event to be executed after a specified delay.
§Parameters
delay: The amount of time after which the event should occur.action: The action to be executed (optional).context: Additional context for the event (optional).
§Example
use desru::EventScheduler;
let mut scheduler = EventScheduler::new();
scheduler.timeout(10.0,
Some(Box::new(|_| Some("Timeout event".to_string()))),
None);Sourcepub fn run(
&mut self,
stop: Box<dyn Fn(&Self) -> bool>,
log_filter: Option<Box<dyn Fn(&Event, &Option<String>) -> bool>>,
) -> Vec<(Event, Option<String>)>
pub fn run( &mut self, stop: Box<dyn Fn(&Self) -> bool>, log_filter: Option<Box<dyn Fn(&Event, &Option<String>) -> bool>>, ) -> Vec<(Event, Option<String>)>
Runs the event scheduler until a stop condition is met.
§Parameters
stop: A closure that takes a reference to the scheduler and returnstruewhen the scheduler should stop.log_filter: An optional closure that determines whether to log an event. Defaults to logging all events.
§Returns
A vector of executed events along with their results.
§Example
use desru::{Event, EventScheduler};
let mut scheduler = EventScheduler::new();
scheduler.timeout(5.0,
Some(Box::new(|_| Some("Event executed".to_string()))),
None);
let stop_fn = Box::new(|s: &EventScheduler| s.current_time >= 10.0);
scheduler.run(stop_fn, None);Sourcepub fn run_until_max_time(
&mut self,
max_time: f64,
) -> Vec<(Event, Option<String>)>
pub fn run_until_max_time( &mut self, max_time: f64, ) -> Vec<(Event, Option<String>)>
Runs the event scheduler until a specified maximum time is reached.
This is a convenience method that calls run with a predefined stop condition based on max_time.
§Parameters
max_time: The maximum simulation time.
§Returns
A vector of executed events along with their results.
§Example
use desru::{Event, EventScheduler};
let mut scheduler = EventScheduler::new();
scheduler.timeout(5.0,
Some(Box::new(|_| Some("Timeout event".to_string()))),
None);
scheduler.run_until_max_time(10.0);Auto Trait Implementations§
impl Freeze for EventScheduler
impl !RefUnwindSafe for EventScheduler
impl !Send for EventScheduler
impl !Sync for EventScheduler
impl Unpin for EventScheduler
impl !UnwindSafe for EventScheduler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more