From bd024bffb191031ab7a0d360a5b97367b4ff19e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Date: Thu, 27 Jan 2022 10:35:40 +0100 Subject: [PATCH] vmm: config: Move add_to_config to config.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let's move add_to_config to config.rs so it can be used from both inside and outside of the vm.rs file. Signed-off-by: Fabiano FidĂȘncio --- vmm/src/config.rs | 8 ++++++++ vmm/src/vm.rs | 36 ++++++++++++++---------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/vmm/src/config.rs b/vmm/src/config.rs index 0185ea689..06e7ae9e8 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -284,6 +284,14 @@ impl fmt::Display for Error { } } +pub fn add_to_config(items: &mut Option>, item: T) { + if let Some(items) = items { + items.push(item); + } else { + *items = Some(vec![item]); + } +} + pub type Result = result::Result; pub struct VmParams<'a> { diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 0e60212f4..9e4267e22 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -14,8 +14,8 @@ #[cfg(any(target_arch = "aarch64", feature = "acpi"))] use crate::config::NumaConfig; use crate::config::{ - DeviceConfig, DiskConfig, FsConfig, HotplugMethod, NetConfig, PmemConfig, UserDeviceConfig, - ValidationError, VmConfig, VsockConfig, + add_to_config, DeviceConfig, DiskConfig, FsConfig, HotplugMethod, NetConfig, PmemConfig, + UserDeviceConfig, ValidationError, VmConfig, VsockConfig, }; use crate::cpu; use crate::device_manager::{self, Console, DeviceManager, DeviceManagerError, PtyPair}; @@ -1390,19 +1390,11 @@ impl Vm { Err(Error::ResizeZone) } - fn add_to_config(devices: &mut Option>, device: T) { - if let Some(devices) = devices { - devices.push(device); - } else { - *devices = Some(vec![device]); - } - } - pub fn add_device(&mut self, mut device_cfg: DeviceConfig) -> Result { { // Validate on a clone of the config let mut config = self.config.lock().unwrap().clone(); - Self::add_to_config(&mut config.devices, device_cfg.clone()); + add_to_config(&mut config.devices, device_cfg.clone()); config.validate().map_err(Error::ConfigValidation)?; } @@ -1417,7 +1409,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - Self::add_to_config(&mut config.devices, device_cfg); + add_to_config(&mut config.devices, device_cfg); } self.device_manager @@ -1433,7 +1425,7 @@ impl Vm { { // Validate on a clone of the config let mut config = self.config.lock().unwrap().clone(); - Self::add_to_config(&mut config.user_devices, device_cfg.clone()); + add_to_config(&mut config.user_devices, device_cfg.clone()); config.validate().map_err(Error::ConfigValidation)?; } @@ -1448,7 +1440,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - Self::add_to_config(&mut config.user_devices, device_cfg); + add_to_config(&mut config.user_devices, device_cfg); } self.device_manager @@ -1515,7 +1507,7 @@ impl Vm { { // Validate on a clone of the config let mut config = self.config.lock().unwrap().clone(); - Self::add_to_config(&mut config.disks, disk_cfg.clone()); + add_to_config(&mut config.disks, disk_cfg.clone()); config.validate().map_err(Error::ConfigValidation)?; } @@ -1530,7 +1522,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - Self::add_to_config(&mut config.disks, disk_cfg); + add_to_config(&mut config.disks, disk_cfg); } self.device_manager @@ -1546,7 +1538,7 @@ impl Vm { { // Validate on a clone of the config let mut config = self.config.lock().unwrap().clone(); - Self::add_to_config(&mut config.fs, fs_cfg.clone()); + add_to_config(&mut config.fs, fs_cfg.clone()); config.validate().map_err(Error::ConfigValidation)?; } @@ -1561,7 +1553,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - Self::add_to_config(&mut config.fs, fs_cfg); + add_to_config(&mut config.fs, fs_cfg); } self.device_manager @@ -1577,7 +1569,7 @@ impl Vm { { // Validate on a clone of the config let mut config = self.config.lock().unwrap().clone(); - Self::add_to_config(&mut config.pmem, pmem_cfg.clone()); + add_to_config(&mut config.pmem, pmem_cfg.clone()); config.validate().map_err(Error::ConfigValidation)?; } @@ -1592,7 +1584,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - Self::add_to_config(&mut config.pmem, pmem_cfg); + add_to_config(&mut config.pmem, pmem_cfg); } self.device_manager @@ -1608,7 +1600,7 @@ impl Vm { { // Validate on a clone of the config let mut config = self.config.lock().unwrap().clone(); - Self::add_to_config(&mut config.net, net_cfg.clone()); + add_to_config(&mut config.net, net_cfg.clone()); config.validate().map_err(Error::ConfigValidation)?; } @@ -1623,7 +1615,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - Self::add_to_config(&mut config.net, net_cfg); + add_to_config(&mut config.net, net_cfg); } self.device_manager