Skip to content

Commit 83f77b3

Browse files
committed
refactor: use #[from] in VmmError
When a variant wraps another error type, and this error type does not appear in any other variant, use thiserrors #[from] directive to implement a `From` impl. This allows eliminating almost all remaining `map_err`s from `create_vmm_and_vcpus`, as the `?` operator will automatically call .from() once (although it cannot do it for multiple layers of wrapping, which is why it was important to change the function to return VmmError). Signed-off-by: Patrick Roy <[email protected]>
1 parent 5bc902b commit 83f77b3

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/vmm/src/builder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,22 @@ fn create_vmm_and_vcpus(
159159
vcpu_count: u8,
160160
kvm_capabilities: Vec<KvmCapability>,
161161
) -> Result<(Vmm, Vec<Vcpu>), VmmError> {
162-
let kvm = Kvm::new(kvm_capabilities).map_err(VmmError::Kvm)?;
162+
let kvm = Kvm::new(kvm_capabilities)?;
163163
// Set up Kvm Vm and register memory regions.
164164
// Build custom CPU config if a custom template is provided.
165-
let mut vm = Vm::new(&kvm).map_err(VmmError::Vm)?;
166-
kvm.check_memory(&guest_memory).map_err(VmmError::Kvm)?;
167-
vm.memory_init(&guest_memory).map_err(VmmError::Vm)?;
165+
let mut vm = Vm::new(&kvm)?;
166+
kvm.check_memory(&guest_memory)?;
167+
vm.memory_init(&guest_memory)?;
168168

169-
let resource_allocator = ResourceAllocator::new().map_err(VmmError::AllocateResources)?;
169+
let resource_allocator = ResourceAllocator::new()?;
170170

171171
// Instantiate the MMIO device manager.
172172
let mmio_device_manager = MMIODeviceManager::new();
173173

174174
// Instantiate ACPI device manager.
175175
let acpi_device_manager = ACPIDeviceManager::new();
176176

177-
let (vcpus, vcpus_exit_evt) = vm.create_vcpus(vcpu_count).map_err(VmmError::Vm)?;
177+
let (vcpus, vcpus_exit_evt) = vm.create_vcpus(vcpu_count)?;
178178

179179
#[cfg(target_arch = "x86_64")]
180180
let pio_device_manager = {

src/vmm/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ pub enum VmmError {
257257
/// Cannot spawn Vcpu thread: {0}
258258
VcpuSpawn(io::Error),
259259
/// Vm error: {0}
260-
Vm(vstate::vm::VmError),
260+
Vm(#[from] vstate::vm::VmError),
261261
/// Kvm error: {0}
262-
Kvm(vstate::kvm::KvmError),
262+
Kvm(#[from] vstate::kvm::KvmError),
263263
/// Error thrown by observer object on Vmm initialization: {0}
264264
VmmObserverInit(vmm_sys_util::errno::Error),
265265
/// Error thrown by observer object on Vmm teardown: {0}

0 commit comments

Comments
 (0)