Skip to content

Commit bd9e5cb

Browse files
author
Gianmarco Garrisi
committed
Cargo fmt
1 parent 231c179 commit bd9e5cb

File tree

5 files changed

+142
-106
lines changed

5 files changed

+142
-106
lines changed

examples/carwash.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,19 @@ fn car_process<'a>(
7272
// Generate random drive_time and wash_time at beginning
7373
let t_drive = distr_drive.sample(rng);
7474
let t_wash = distr_wash.sample(rng);
75-
Box::new(#[coroutine] move |_| {
76-
// The car drives for `t_drive` time
77-
yield Drive(t_drive);
78-
// Arrives at carwash and waits for a machine to be free
79-
yield WaitMachine(carwash);
80-
// The car wash for `t_wash` time, keeping the carwash machine (resource) occupied
81-
yield Wash(t_wash);
82-
// The car leaves the carwash, freeing the resource
83-
yield Leave(carwash);
84-
})
75+
Box::new(
76+
#[coroutine]
77+
move |_| {
78+
// The car drives for `t_drive` time
79+
yield Drive(t_drive);
80+
// Arrives at carwash and waits for a machine to be free
81+
yield WaitMachine(carwash);
82+
// The car wash for `t_wash` time, keeping the carwash machine (resource) occupied
83+
yield Wash(t_wash);
84+
// The car leaves the carwash, freeing the resource
85+
yield Leave(carwash);
86+
},
87+
)
8588
}
8689

8790
fn main() {

examples/monitoring-state.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,28 +157,31 @@ impl PCBStateCtx {
157157
}
158158

159159
fn process_code(r: Resources) -> Box<Process<PCBState>> {
160-
Box::new(#[coroutine] move |_| {
161-
let mut current_pcb_id = 0;
162-
let mut ctx = PCBStateCtx::new(r);
163-
loop {
164-
// PCB first processing stage
165-
yield ctx.need_pip();
166-
yield ctx.pip_work();
167-
yield ctx.free_pip(PCBStage::SurfaceMountPlaced);
168-
// requeue PCB for second processing stage
169-
yield ctx.need_pip();
170-
yield ctx.pip_work();
171-
yield ctx.free_pip(PCBStage::ThruHolePlaced);
172-
// queue for electrical testing
173-
yield ctx.need_et();
174-
yield ctx.et_work();
175-
yield ctx.free_et(PCBStage::ElectricalTestPerformed);
176-
yield ctx.mark(PCBStage::Done);
160+
Box::new(
161+
#[coroutine]
162+
move |_| {
163+
let mut current_pcb_id = 0;
164+
let mut ctx = PCBStateCtx::new(r);
165+
loop {
166+
// PCB first processing stage
167+
yield ctx.need_pip();
168+
yield ctx.pip_work();
169+
yield ctx.free_pip(PCBStage::SurfaceMountPlaced);
170+
// requeue PCB for second processing stage
171+
yield ctx.need_pip();
172+
yield ctx.pip_work();
173+
yield ctx.free_pip(PCBStage::ThruHolePlaced);
174+
// queue for electrical testing
175+
yield ctx.need_et();
176+
yield ctx.et_work();
177+
yield ctx.free_et(PCBStage::ElectricalTestPerformed);
178+
yield ctx.mark(PCBStage::Done);
177179

178-
current_pcb_id += 1;
179-
ctx.set_new_id(current_pcb_id);
180-
}
181-
})
180+
current_pcb_id += 1;
181+
ctx.set_new_id(current_pcb_id);
182+
}
183+
},
184+
)
182185
}
183186

184187
fn main() {

examples/one_cpu.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,33 @@ use desim::{Effect, EndCondition, Simulation};
1414
fn main() {
1515
let mut s = Simulation::new();
1616
let cpu = s.create_resource(Box::new(SimpleResource::new(1)));
17-
let p1 = s.create_process(Box::new(#[coroutine] move |_| {
18-
for _ in 0..10 {
19-
// wait for the cpu to be available
20-
yield Effect::Request(cpu);
21-
// do some job that requires a fixed amount of 5 time units
22-
yield Effect::TimeOut(5.0);
23-
// release the CPU
24-
yield Effect::Release(cpu);
25-
}
26-
}));
27-
let p2 = s.create_process(Box::new(#[coroutine] move |_| {
28-
let mut rng = Rng::from_entropy();
29-
loop {
30-
// wait for the CPU
31-
yield Effect::Request(cpu);
32-
// do some job for a random amount of time units between 0 and 10
33-
yield Effect::TimeOut((rng.next_u32() % 10) as f64);
34-
// release the CPU
35-
yield Effect::Release(cpu);
36-
}
37-
}));
17+
let p1 = s.create_process(Box::new(
18+
#[coroutine]
19+
move |_| {
20+
for _ in 0..10 {
21+
// wait for the cpu to be available
22+
yield Effect::Request(cpu);
23+
// do some job that requires a fixed amount of 5 time units
24+
yield Effect::TimeOut(5.0);
25+
// release the CPU
26+
yield Effect::Release(cpu);
27+
}
28+
},
29+
));
30+
let p2 = s.create_process(Box::new(
31+
#[coroutine]
32+
move |_| {
33+
let mut rng = Rng::from_entropy();
34+
loop {
35+
// wait for the CPU
36+
yield Effect::Request(cpu);
37+
// do some job for a random amount of time units between 0 and 10
38+
yield Effect::TimeOut((rng.next_u32() % 10) as f64);
39+
// release the CPU
40+
yield Effect::Release(cpu);
41+
}
42+
},
43+
));
3844
// let p1 to start immediately...
3945
s.schedule_event(0.0, p1, Effect::TimeOut(0.));
4046
// ...and p2 after 17 time units

examples/store.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,31 @@ impl SimState for MyState {
4444
fn main() {
4545
let mut s = Simulation::new();
4646
let queue = s.create_store(Box::new(SimpleStore::new(1)));
47-
let p1 = s.create_process(Box::new(#[coroutine] move |_| {
48-
for i in 0..10 {
49-
// wait for the cpu to be available
50-
yield MyState::Push(queue, i);
51-
// do some job that requires a fixed amount of 5 time units
52-
// release the CPU
53-
yield MyState::Wait(10.0);
54-
}
55-
}));
56-
let p2 = s.create_process(Box::new(#[coroutine] move |_| {
57-
for _ in 0..10 {
58-
// wait for the CPU
59-
let ret = yield MyState::Pull(queue);
60-
println!("ret: {:?}", ret);
61-
// do some job for a random amount of time units between 0 and 10
62-
// yield MyState::Wait(10.0);
63-
// release the CPU
64-
}
65-
}));
47+
let p1 = s.create_process(Box::new(
48+
#[coroutine]
49+
move |_| {
50+
for i in 0..10 {
51+
// wait for the cpu to be available
52+
yield MyState::Push(queue, i);
53+
// do some job that requires a fixed amount of 5 time units
54+
// release the CPU
55+
yield MyState::Wait(10.0);
56+
}
57+
},
58+
));
59+
let p2 = s.create_process(Box::new(
60+
#[coroutine]
61+
move |_| {
62+
for _ in 0..10 {
63+
// wait for the CPU
64+
let ret = yield MyState::Pull(queue);
65+
println!("ret: {:?}", ret);
66+
// do some job for a random amount of time units between 0 and 10
67+
// yield MyState::Wait(10.0);
68+
// release the CPU
69+
}
70+
},
71+
));
6672
// let p1 to start immediately...
6773
s.schedule_event(0.0, p1, MyState::default());
6874
// ...and p2 after 17 time units

src/lib.rs

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,17 @@ mod tests {
530530
use crate::{Effect, Simulation};
531531

532532
let mut s = Simulation::new();
533-
let p = s.create_process(Box::new(#[coroutine] |_| {
534-
let mut a = 0.0;
535-
loop {
536-
a += 1.0;
537-
538-
yield Effect::TimeOut(a);
539-
}
540-
}));
533+
let p = s.create_process(Box::new(
534+
#[coroutine]
535+
|_| {
536+
let mut a = 0.0;
537+
loop {
538+
a += 1.0;
539+
540+
yield Effect::TimeOut(a);
541+
}
542+
},
543+
));
541544
s.schedule_event(0.0, p, Effect::TimeOut(0.));
542545
s.step();
543546
s.step();
@@ -553,13 +556,16 @@ mod tests {
553556
use crate::{Effect, EndCondition, Simulation};
554557

555558
let mut s = Simulation::new();
556-
let p = s.create_process(Box::new(#[coroutine] |_| {
557-
let tik = 0.7;
558-
loop {
559-
println!("tik");
560-
yield Effect::TimeOut(tik);
561-
}
562-
}));
559+
let p = s.create_process(Box::new(
560+
#[coroutine]
561+
|_| {
562+
let tik = 0.7;
563+
loop {
564+
println!("tik");
565+
yield Effect::TimeOut(tik);
566+
}
567+
},
568+
));
563569
s.schedule_event(0.0, p, Effect::TimeOut(0.));
564570
let s = s.run(EndCondition::Time(10.0));
565571
println!("{}", s.time());
@@ -575,17 +581,23 @@ mod tests {
575581
let r = s.create_resource(Box::new(SimpleResource::new(1)));
576582

577583
// simple process that lock the resource for 7 time units
578-
let p1 = s.create_process(Box::new(#[coroutine] move |_| {
579-
yield Effect::Request(r);
580-
yield Effect::TimeOut(7.0);
581-
yield Effect::Release(r);
582-
}));
584+
let p1 = s.create_process(Box::new(
585+
#[coroutine]
586+
move |_| {
587+
yield Effect::Request(r);
588+
yield Effect::TimeOut(7.0);
589+
yield Effect::Release(r);
590+
},
591+
));
583592
// simple process that holds the resource for 3 time units
584-
let p2 = s.create_process(Box::new(#[coroutine] move |_| {
585-
yield Effect::Request(r);
586-
yield Effect::TimeOut(3.0);
587-
yield Effect::Release(r);
588-
}));
593+
let p2 = s.create_process(Box::new(
594+
#[coroutine]
595+
move |_| {
596+
yield Effect::Request(r);
597+
yield Effect::TimeOut(3.0);
598+
yield Effect::Release(r);
599+
},
600+
));
589601

590602
// let p1 start immediately...
591603
s.schedule_event(0.0, p1, Effect::TimeOut(0.));
@@ -608,17 +620,23 @@ mod tests {
608620
let store = sim.create_store(Box::new(SimpleStore::new(1)));
609621

610622
// simple process that pulls out of the store immediately and after 7 time units
611-
let p1 = sim.create_process(Box::new(#[coroutine] move |_| {
612-
yield Effect::Pull(store);
613-
yield Effect::TimeOut(7.0);
614-
yield Effect::Pull(store);
615-
}));
623+
let p1 = sim.create_process(Box::new(
624+
#[coroutine]
625+
move |_| {
626+
yield Effect::Pull(store);
627+
yield Effect::TimeOut(7.0);
628+
yield Effect::Pull(store);
629+
},
630+
));
616631
// simple process that pushes into the store immediately and after 3 time units
617-
let p2 = sim.create_process(Box::new(#[coroutine] move |_| {
618-
yield Effect::Push(store);
619-
yield Effect::TimeOut(3.0);
620-
yield Effect::Push(store);
621-
}));
632+
let p2 = sim.create_process(Box::new(
633+
#[coroutine]
634+
move |_| {
635+
yield Effect::Push(store);
636+
yield Effect::TimeOut(3.0);
637+
yield Effect::Push(store);
638+
},
639+
));
622640

623641
// let p1 start immediately...
624642
sim.schedule_event(0.0, p1, Effect::TimeOut(0.));

0 commit comments

Comments
 (0)