Skip to content

Commit 14d13c1

Browse files
committed
refactor: Make MachineConfigUpdate::is_empty forward compatible
Instead of listing out each field in the function (which is error prone in case more fields get added, because the compiler will not be able to detect that they might be missing from the `is_empty` condition), compare against `Default::default`, which will automatically include all future fields in the comparision, and `Default::default()` will always set all fields to `None`. Signed-off-by: Patrick Roy <[email protected]>
1 parent aa29bff commit 14d13c1

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

src/vmm/src/vmm_config/machine_config.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Default for MachineConfig {
6060
/// All fields are optional, but at least one needs to be specified.
6161
/// If a field is `Some(value)` then we assume an update is requested
6262
/// for that field.
63-
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
63+
#[derive(Clone, Default, Debug, PartialEq, Eq, Deserialize, Serialize)]
6464
#[serde(deny_unknown_fields)]
6565
pub struct MachineConfigUpdate {
6666
/// Number of vcpu to start.
@@ -85,16 +85,7 @@ impl MachineConfigUpdate {
8585
/// Returns `true` if all fields are set to `None` which means that there is nothing
8686
/// to be updated.
8787
pub fn is_empty(&self) -> bool {
88-
if self.vcpu_count.is_none()
89-
&& self.mem_size_mib.is_none()
90-
&& self.cpu_template.is_none()
91-
&& self.smt.is_none()
92-
&& self.track_dirty_pages.is_none()
93-
{
94-
return true;
95-
}
96-
97-
false
88+
self == &Default::default()
9889
}
9990
}
10091

0 commit comments

Comments
 (0)