EventScheduler

Struct EventScheduler 

Source
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

Source

pub fn new() -> Self

Creates a new EventScheduler with an empty event queue.

§Returns

A new EventScheduler instance.

§Example
use desru::{EventScheduler};

let scheduler = EventScheduler::new();
assert_eq!(scheduler.current_time, 0.0);
Source

pub fn schedule(&mut self, event: Event)

Schedules a new event by adding it to the event queue.

§Parameters
  • event: The event to be scheduled.
§Example
use desru::{Event, EventScheduler};

let mut scheduler = EventScheduler::new();
let event = Event::new(5.0, None, None);
scheduler.schedule(event);
Source

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);
Source

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 returns true when 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);
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.