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.
impl Eq for Event
Auto Trait Implementations§
impl Freeze for Event
impl !RefUnwindSafe for Event
impl !Send for Event
impl !Sync for Event
impl Unpin for Event
impl !UnwindSafe for Event
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)