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
impl State
Sourcepub fn insert<V: 'static>(&mut self, value: V) -> Key<V>
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?
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}
Sourcepub fn remove<V: 'static>(&mut self, key: Key<V>) -> Option<V>
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
.
Sourcepub fn get<V: 'static>(&self, key: Key<V>) -> Option<&V>
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?
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 }
Sourcepub fn get_mut<V: 'static>(&mut self, key: Key<V>) -> Option<&mut V>
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?
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 }
Sourcepub fn add_queue<Q: Queue + 'static>(&mut self, queue: Q) -> QueueId<Q>
pub fn add_queue<Q: Queue + 'static>(&mut self, queue: Q) -> QueueId<Q>
Creates a new unbounded queue, returning its ID.
Sourcepub fn send<Q: Queue + 'static>(
&mut self,
queue: QueueId<Q>,
value: Q::Item,
) -> Result<(), PushError>
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?
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 }
Sourcepub fn recv<Q: Queue + 'static>(&mut self, queue: QueueId<Q>) -> Option<Q::Item>
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?
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 }
Sourcepub fn len<Q: Queue + 'static>(&self, queue: QueueId<Q>) -> usize
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?
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 }