pub struct Event {
pub time: f64,
pub action: Box<dyn FnMut(&mut EventScheduler) -> Option<String>>,
pub context: HashMap<String, String>,
pub active: bool,
}Expand description
Represents an event in the simulation.
Each event has a scheduled time (time), an associated action (action)
that will be executed when the event occurs, and a context for storing
key-value pairs of additional information about the event.
§Fields
time: The time at which the event is scheduled to run.action: A closure that represents the task to be performed when the event is triggered. It returns anOption<String>to optionally pass a result when executed.context: A map containing any extra contextual information as key-value pairs (both asString).active: A boolean indicating if the event is active. If false, the event will not run.
Fields§
§time: f64§action: Box<dyn FnMut(&mut EventScheduler) -> Option<String>>§context: HashMap<String, String>§active: boolImplementations§
Source§impl Event
impl Event
Sourcepub fn new(
time: f64,
action: Option<Box<dyn FnMut(&mut EventScheduler) -> Option<String>>>,
context: Option<HashMap<String, String>>,
) -> Self
pub fn new( time: f64, action: Option<Box<dyn FnMut(&mut EventScheduler) -> Option<String>>>, context: Option<HashMap<String, String>>, ) -> Self
Creates a new Event with the given time, action, and context.
§Parameters
time: The time when the event should be executed.action: An optional closure representing the event’s task. Defaults to a no-op (returnsNone).context: An optionalHashMapof context information. Defaults to an empty map.
§Returns
A new Event instance.
§Example
use desru::{Event};
let event = Event::new(5.0, None, None);
assert_eq!(event.time, 5.0);Sourcepub fn run(&mut self, scheduler: &mut EventScheduler) -> Option<String>
pub fn run(&mut self, scheduler: &mut EventScheduler) -> Option<String>
Executes the action of the event if it is active.
§Returns
Some(String): The result of the action if the event is active and the action produces a result.None: If the event is inactive or the action produces no result.
§Example
use desru::{Event, EventScheduler};
let mut scheduler = EventScheduler::new();
let mut event = Event::new(0.0,
Some(Box::new(|scheduler| Some("Executed".to_string()))),
None);
assert_eq!(event.run(&mut scheduler), Some("Executed".to_string()));Sourcepub fn deactivate(&mut self)
pub fn deactivate(&mut self)
Sets the event to be inactive.
Trait Implementations§
Source§impl Clone for Event
impl Clone for Event
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Creates a clone of the event.
Note: The action closure is not cloned, since closures cannot be cloned. A placeholder
action that returns None is used in the cloned event. The context and other fields are
copied as usual.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Ord for Event
impl Ord for Event
Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
Defines the ordering between two events.
The event with the earlier time has higher priority, enabling
the BinaryHeap to act as a priority queue.
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Event
impl PartialOrd for Event
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
Compares two events based on their time, in reverse order, for use in a max-heap.
This allows events with earlier times to be processed first.