Struct State

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

State of a simulation holding all queues and arbitrary values in a store value.

Implementations§

Source§

impl State

Source

pub fn insert<V: 'static>(&mut self, value: V) -> Key<V>

Inserts an arbitrary value to the value store. Learn more in the documentation for Key.

Examples found in repository?
examples/simulation.rs (line 172)
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 remove<V: 'static>(&mut self, key: Key<V>) -> Option<V>

Removes a value of type V from the value store. Learn more in the documentation for Key.

Source

pub fn get<V: 'static>(&self, key: Key<V>) -> Option<&V>

Gets a immutable reference to a value of a type V from the value store. Learn more in the documentation for Key.

Examples found in repository?
examples/simulation.rs (line 63)
56    fn process_event(
57        &self,
58        self_id: ComponentId<ProducerEvent>,
59        _event: &ProducerEvent,
60        scheduler: &mut Scheduler,
61        state: &mut State,
62    ) {
63        let count = *state.get(self.produced_count).unwrap();
64        if count < 10 {
65            let _ = state.send(self.outgoing, self.produce());
66            self.log();
67            scheduler.schedule(self.interval(), self_id, ProducerEvent);
68            scheduler.schedule(Duration::default(), self.consumer, ConsumerEvent::Received);
69            *state.get_mut(self.produced_count).unwrap() = count + 1;
70        }
71    }
72}
73
74impl Component for Consumer {
75    type Event = ConsumerEvent;
76
77    fn process_event(
78        &self,
79        self_id: ComponentId<ConsumerEvent>,
80        event: &ConsumerEvent,
81        scheduler: &mut Scheduler,
82        state: &mut State,
83    ) {
84        let busy = state.get(self.working_on).unwrap().is_some();
85        match event {
86            ConsumerEvent::Received => {
87                if !busy {
88                    if let Some(product) = state.recv(self.incoming) {
89                        if let Some(w) = state.get_mut(self.working_on) {
90                            *w = Some(product);
91                        }
92                        scheduler.schedule(self.interval(), self_id, ConsumerEvent::Finished);
93                    }
94                }
95            }
96            ConsumerEvent::Finished => {
97                let product = state.get_mut(self.working_on).unwrap().take().unwrap();
98                self.log(product);
99                if state.len(self.incoming) > 0 {
100                    scheduler.schedule(Duration::default(), self_id, ConsumerEvent::Received);
101                }
102            }
103        }
104    }
Source

pub fn get_mut<V: 'static>(&mut self, key: Key<V>) -> Option<&mut V>

Gets a mutable reference to a value of a type V from the value store. Learn more in the documentation for Key.

Examples found in repository?
examples/simulation.rs (line 69)
56    fn process_event(
57        &self,
58        self_id: ComponentId<ProducerEvent>,
59        _event: &ProducerEvent,
60        scheduler: &mut Scheduler,
61        state: &mut State,
62    ) {
63        let count = *state.get(self.produced_count).unwrap();
64        if count < 10 {
65            let _ = state.send(self.outgoing, self.produce());
66            self.log();
67            scheduler.schedule(self.interval(), self_id, ProducerEvent);
68            scheduler.schedule(Duration::default(), self.consumer, ConsumerEvent::Received);
69            *state.get_mut(self.produced_count).unwrap() = count + 1;
70        }
71    }
72}
73
74impl Component for Consumer {
75    type Event = ConsumerEvent;
76
77    fn process_event(
78        &self,
79        self_id: ComponentId<ConsumerEvent>,
80        event: &ConsumerEvent,
81        scheduler: &mut Scheduler,
82        state: &mut State,
83    ) {
84        let busy = state.get(self.working_on).unwrap().is_some();
85        match event {
86            ConsumerEvent::Received => {
87                if !busy {
88                    if let Some(product) = state.recv(self.incoming) {
89                        if let Some(w) = state.get_mut(self.working_on) {
90                            *w = Some(product);
91                        }
92                        scheduler.schedule(self.interval(), self_id, ConsumerEvent::Finished);
93                    }
94                }
95            }
96            ConsumerEvent::Finished => {
97                let product = state.get_mut(self.working_on).unwrap().take().unwrap();
98                self.log(product);
99                if state.len(self.incoming) > 0 {
100                    scheduler.schedule(Duration::default(), self_id, ConsumerEvent::Received);
101                }
102            }
103        }
104    }
Source

pub fn add_queue<Q: Queue + 'static>(&mut self, queue: Q) -> QueueId<Q>

Creates a new unbounded queue, returning its ID.

Source

pub fn send<Q: Queue + 'static>( &mut self, queue: QueueId<Q>, value: Q::Item, ) -> Result<(), PushError>

Sends value to the queue. This is a shorthand for queue_mut(queue).push(value).

§Errors

It returns an error if the queue is full.

Examples found in repository?
examples/simulation.rs (line 65)
56    fn process_event(
57        &self,
58        self_id: ComponentId<ProducerEvent>,
59        _event: &ProducerEvent,
60        scheduler: &mut Scheduler,
61        state: &mut State,
62    ) {
63        let count = *state.get(self.produced_count).unwrap();
64        if count < 10 {
65            let _ = state.send(self.outgoing, self.produce());
66            self.log();
67            scheduler.schedule(self.interval(), self_id, ProducerEvent);
68            scheduler.schedule(Duration::default(), self.consumer, ConsumerEvent::Received);
69            *state.get_mut(self.produced_count).unwrap() = count + 1;
70        }
71    }
Source

pub fn recv<Q: Queue + 'static>(&mut self, queue: QueueId<Q>) -> Option<Q::Item>

Pops the first value from the queue. It returns None if the queue is empty. This is a shorthand for queue_mut(queue).pop(value).

Examples found in repository?
examples/simulation.rs (line 88)
77    fn process_event(
78        &self,
79        self_id: ComponentId<ConsumerEvent>,
80        event: &ConsumerEvent,
81        scheduler: &mut Scheduler,
82        state: &mut State,
83    ) {
84        let busy = state.get(self.working_on).unwrap().is_some();
85        match event {
86            ConsumerEvent::Received => {
87                if !busy {
88                    if let Some(product) = state.recv(self.incoming) {
89                        if let Some(w) = state.get_mut(self.working_on) {
90                            *w = Some(product);
91                        }
92                        scheduler.schedule(self.interval(), self_id, ConsumerEvent::Finished);
93                    }
94                }
95            }
96            ConsumerEvent::Finished => {
97                let product = state.get_mut(self.working_on).unwrap().take().unwrap();
98                self.log(product);
99                if state.len(self.incoming) > 0 {
100                    scheduler.schedule(Duration::default(), self_id, ConsumerEvent::Received);
101                }
102            }
103        }
104    }
Source

pub fn len<Q: Queue + 'static>(&self, queue: QueueId<Q>) -> usize

Checks the number of elements in the queue. This is a shorthand for queue(queue).len().

Examples found in repository?
examples/simulation.rs (line 99)
77    fn process_event(
78        &self,
79        self_id: ComponentId<ConsumerEvent>,
80        event: &ConsumerEvent,
81        scheduler: &mut Scheduler,
82        state: &mut State,
83    ) {
84        let busy = state.get(self.working_on).unwrap().is_some();
85        match event {
86            ConsumerEvent::Received => {
87                if !busy {
88                    if let Some(product) = state.recv(self.incoming) {
89                        if let Some(w) = state.get_mut(self.working_on) {
90                            *w = Some(product);
91                        }
92                        scheduler.schedule(self.interval(), self_id, ConsumerEvent::Finished);
93                    }
94                }
95            }
96            ConsumerEvent::Finished => {
97                let product = state.get_mut(self.working_on).unwrap().take().unwrap();
98                self.log(product);
99                if state.len(self.incoming) > 0 {
100                    scheduler.schedule(Duration::default(), self_id, ConsumerEvent::Received);
101                }
102            }
103        }
104    }
Source

pub fn queue<Q: Queue + 'static>(&self, queue: QueueId<Q>) -> &Q

Returns a immutable reference to the queue by the given ID.

Source

pub fn queue_mut<Q: Queue + 'static>(&mut self, queue: QueueId<Q>) -> &mut Q

Returns a mutable reference to the queue by the given ID.

Trait Implementations§

Source§

impl Default for State

Source§

fn default() -> State

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for State

§

impl !RefUnwindSafe for State

§

impl !Send for State

§

impl !Sync for State

§

impl Unpin for State

§

impl !UnwindSafe for State

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.