sim: Make `Run` into a builder for `Images`
The `Run` type is used as a builder for `Images`, describing the
particular device config. Convert the `make...` methods to take `self`
by value to avoid needing to clone all of the fields for the new struct.
The `main` test running now needs to do a few clones of `Run` in order
to build different configurations, but the primary test runner (through
`cargo test`) is now able to avoid having to clone the entire flash
simulators.
Signed-off-by: David Brown <david.brown@linaro.org>
diff --git a/sim/src/lib.rs b/sim/src/lib.rs
index ce3abfc..effd76e 100644
--- a/sim/src/lib.rs
+++ b/sim/src/lib.rs
@@ -151,7 +151,11 @@
}
}
-/// A test run, intended to be run from "cargo test", so panics on failure.
+/// A Run describes a single run of the simulator. It captures the
+/// configuration of a particular device configuration, including the flash
+/// devices and the information about the slots. This can be thought of as
+/// a builder for `Images`.
+#[derive(Clone)]
pub struct Run {
flashmap: SimFlashMap,
areadesc: AreaDesc,
@@ -194,27 +198,27 @@
}
pub fn each_device<F>(f: F)
- where F: Fn(&mut Run)
+ where F: Fn(Run)
{
for &dev in ALL_DEVICES {
for &align in &[1, 2, 4, 8] {
for &erased_val in &[0, 0xff] {
- let mut run = Run::new(dev, align, erased_val);
- f(&mut run);
+ let run = Run::new(dev, align, erased_val);
+ f(run);
}
}
}
}
/// Construct an `Images` that doesn't expect an upgrade to happen.
- pub fn make_no_upgrade_image(&self) -> Images {
- let mut flashmap = self.flashmap.clone();
+ pub fn make_no_upgrade_image(self) -> Images {
+ let mut flashmap = self.flashmap;
let primaries = install_image(&mut flashmap, &self.slots, 0, 32784, false);
let upgrades = install_image(&mut flashmap, &self.slots, 1, 41928, false);
Images {
flashmap: flashmap,
- areadesc: self.areadesc.clone(),
- slots: [self.slots[0].clone(), self.slots[1].clone()],
+ areadesc: self.areadesc,
+ slots: self.slots,
primaries: primaries,
upgrades: upgrades,
total_count: None,
@@ -222,7 +226,7 @@
}
/// Construct an `Images` for normal testing.
- pub fn make_image(&self) -> Images {
+ pub fn make_image(self) -> Images {
let mut images = self.make_no_upgrade_image();
mark_upgrade(&mut images.flashmap, &images.slots[1]);
@@ -238,14 +242,14 @@
images
}
- pub fn make_bad_secondary_slot_image(&self) -> Images {
+ pub fn make_bad_secondary_slot_image(self) -> Images {
let mut bad_flashmap = self.flashmap.clone();
let primaries = install_image(&mut bad_flashmap, &self.slots, 0, 32784, false);
let upgrades = install_image(&mut bad_flashmap, &self.slots, 1, 41928, true);
Images {
flashmap: bad_flashmap,
- areadesc: self.areadesc.clone(),
- slots: [self.slots[0].clone(), self.slots[1].clone()],
+ areadesc: self.areadesc,
+ slots: self.slots,
primaries: primaries,
upgrades: upgrades,
total_count: None,
@@ -276,11 +280,11 @@
// Creates a badly signed image in the secondary slot to check that
// it is not upgraded to
- let bad_secondary_slot_image = run.make_bad_secondary_slot_image();
+ let bad_secondary_slot_image = run.clone().make_bad_secondary_slot_image();
failed |= bad_secondary_slot_image.run_signfail_upgrade();
- let images = run.make_no_upgrade_image();
+ let images = run.clone().make_no_upgrade_image();
failed |= images.run_norevert_newimage();
let images = run.make_image();