Struct Executor

Source
pub struct Executor { /* private fields */ }
Expand description

Executor is used for simple execution of an entire simulation.

See the crate level documentation for examples.

Implementations§

Source§

impl Executor

Source

pub fn unbound() -> Self

Simulation will end only once there is no available events in the queue.

Examples found in repository?
examples/simulation.rs (line 190)
168fn main() {
169    let messages = Rc::new(RefCell::new(Vec::<String>::new()));
170    let mut simulation = Simulation::default();
171    let queue = simulation.add_queue(Fifo::default());
172    let working_on = simulation.state.insert::<Option<Product>>(None);
173    let consumer = simulation.add_component(Consumer {
174        incoming: queue,
175        working_on,
176        messages: messages.clone(),
177    });
178    let produced_count = simulation.state.insert(0_usize);
179    let producer = simulation.add_component(Producer {
180        outgoing: queue,
181        consumer,
182        produced_count,
183        messages: messages.clone(),
184    });
185    simulation.schedule(Duration::new(0, 0), producer, ProducerEvent);
186    // simulation.schedule(Duration::new(0, 0), consumer, ProducerEvent);
187    // The above would fail with:                         ^^^^^^^^^^^^^ expected enum `ConsumerEvent`, found struct `ProducerEvent`
188    {
189        let messages = messages.clone();
190        simulation.execute(Executor::unbound().side_effect(move |sim| {
191            messages
192                .borrow_mut()
193                .push(format!("{:?}", sim.scheduler.time()));
194        }));
195    }
196    assert_eq!(*messages.borrow(), EXPECTED.split('\n').collect::<Vec<_>>());
197}
Source

pub fn timed(time: Duration) -> Self

Simulation will be run no longer than the given time. It may terminate early if no events are available.

Source

pub fn steps(steps: usize) -> Self

Simulation will execute exactly this many steps, unless we run out of events.

Source

pub fn side_effect<F>(self, func: F) -> ExecutorWithSideEffect<F>
where F: Fn(&Simulation),

Registers a side effect that is called after each simulation step.

Examples found in repository?
examples/simulation.rs (lines 190-194)
168fn main() {
169    let messages = Rc::new(RefCell::new(Vec::<String>::new()));
170    let mut simulation = Simulation::default();
171    let queue = simulation.add_queue(Fifo::default());
172    let working_on = simulation.state.insert::<Option<Product>>(None);
173    let consumer = simulation.add_component(Consumer {
174        incoming: queue,
175        working_on,
176        messages: messages.clone(),
177    });
178    let produced_count = simulation.state.insert(0_usize);
179    let producer = simulation.add_component(Producer {
180        outgoing: queue,
181        consumer,
182        produced_count,
183        messages: messages.clone(),
184    });
185    simulation.schedule(Duration::new(0, 0), producer, ProducerEvent);
186    // simulation.schedule(Duration::new(0, 0), consumer, ProducerEvent);
187    // The above would fail with:                         ^^^^^^^^^^^^^ expected enum `ConsumerEvent`, found struct `ProducerEvent`
188    {
189        let messages = messages.clone();
190        simulation.execute(Executor::unbound().side_effect(move |sim| {
191            messages
192                .borrow_mut()
193                .push(format!("{:?}", sim.scheduler.time()));
194        }));
195    }
196    assert_eq!(*messages.borrow(), EXPECTED.split('\n').collect::<Vec<_>>());
197}

Trait Implementations§

Source§

impl Clone for Executor

Source§

fn clone(&self) -> Executor

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Executor

Source§

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

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

impl Execute for Executor

Source§

fn execute(self, sim: &mut Simulation)

Executes the simulation until some stopping condition is reached. The condition is implementation-specific.
Source§

impl PartialEq for Executor

Source§

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

Tests for self and other values to be equal, and is used by ==.
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 Copy for Executor

Source§

impl Eq for Executor

Source§

impl StructuralPartialEq for Executor

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> 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.