Event

Struct Event 

Source
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 an Option<String> to optionally pass a result when executed.
  • context: A map containing any extra contextual information as key-value pairs (both as String).
  • 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: bool

Implementations§

Source§

impl Event

Source

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 (returns None).
  • context: An optional HashMap of 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);
Source

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

pub fn activate(&mut self)

Sets the event to be active.

Source

pub fn deactivate(&mut self)

Sets the event to be inactive.

Trait Implementations§

Source§

impl Clone for Event

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for Event

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for Event

Source§

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) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Event

Source§

fn eq(&self, other: &Self) -> bool

Checks if two events are equal based on their scheduled time.

1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Event

Source§

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.

1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.