Skip to content

Commit 487ad79

Browse files
committed
refactor: remove remaining usage of anyhow
The only place where anyhow was used is in the device manager code that runs a specified function with a certain device. No instead of converting the possible error of that function to anyhow::error, just return the error and lest the call site do deal with it. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 313468e commit 487ad79

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vmm/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ gdb = ["arrayvec", "gdbstub", "gdbstub_arch"]
1717

1818
acpi_tables = { path = "../acpi-tables" }
1919
aes-gcm = { version = "0.10.1", default-features = false, features = ["aes"] }
20-
anyhow = "1.0.100"
2120
arrayvec = { version = "0.7.6", optional = true }
2221
aws-lc-rs = { version = "1.14.0", features = ["bindgen"] }
2322
base64 = "0.22.1"

src/vmm/src/device_manager/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ pub enum FindDeviceError {
9191
InvalidDeviceType,
9292
/// Device not found
9393
DeviceNotFound,
94-
/// Internal Device error: {0}
95-
InternalDeviceError(anyhow::Error),
9694
}
9795

9896
#[derive(Debug)]
@@ -376,19 +374,18 @@ impl DeviceManager {
376374
&self,
377375
id: &str,
378376
f: F,
379-
) -> Result<R, FindDeviceError>
377+
) -> Result<Result<R, E>, FindDeviceError>
380378
where
381379
T: VirtioDevice + 'static + Debug,
382380
E: std::error::Error + 'static + Send + Sync,
383381
F: FnOnce(&mut T) -> Result<R, E>,
384382
{
385383
if let Some(device) = self.get_virtio_device(T::const_device_type(), id) {
386384
let mut dev = device.lock().expect("Poisoned lock");
387-
f(dev
385+
Ok(f(dev
388386
.as_mut_any()
389387
.downcast_mut::<T>()
390-
.ok_or(FindDeviceError::InvalidDeviceType)?)
391-
.map_err(|e| FindDeviceError::InternalDeviceError(e.into()))
388+
.ok_or(FindDeviceError::InvalidDeviceType)?))
392389
} else {
393390
Err(FindDeviceError::DeviceNotFound)
394391
}
@@ -400,7 +397,7 @@ impl DeviceManager {
400397
T: VirtioDevice + 'static + Debug,
401398
F: FnOnce(&mut T) -> R,
402399
{
403-
self.try_with_virtio_device_with_id(id, |dev: &mut T| Ok::<R, FindDeviceError>(f(dev)))
400+
self.try_with_virtio_device_with_id(id, |dev: &mut T| Ok::<R, FindDeviceError>(f(dev)))?
404401
}
405402
}
406403

src/vmm/src/lib.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ use vstate::kvm::Kvm;
136136
use vstate::vcpu::{self, StartThreadedError, VcpuSendEventError};
137137

138138
use crate::cpu_config::templates::CpuConfiguration;
139-
use crate::devices::virtio::balloon::{BALLOON_DEV_ID, Balloon, BalloonConfig, BalloonStats};
139+
use crate::devices::virtio::balloon::{
140+
BALLOON_DEV_ID, Balloon, BalloonConfig, BalloonError, BalloonStats,
141+
};
142+
use crate::devices::virtio::block::BlockError;
140143
use crate::devices::virtio::block::device::Block;
141144
use crate::devices::virtio::net::Net;
142145
use crate::logger::{METRICS, MetricsError, error, info, warn};
@@ -248,6 +251,10 @@ pub enum VmmError {
248251
VMGenID(#[from] VmGenIdError),
249252
/// Failed perform action on device: {0}
250253
FindDeviceError(#[from] device_manager::FindDeviceError),
254+
/// Block: {0}
255+
Block(#[from] BlockError),
256+
/// Balloon: {0}
257+
Balloon(#[from] BalloonError),
251258
}
252259

253260
/// Shorthand type for KVM dirty page bitmap.
@@ -522,7 +529,8 @@ impl Vmm {
522529
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| {
523530
block.update_disk_image(path_on_host)
524531
})
525-
.map_err(VmmError::FindDeviceError)
532+
.map_err(VmmError::FindDeviceError)??;
533+
Ok(())
526534
}
527535

528536
/// Updates the rate limiter parameters for block device with `drive_id` id.
@@ -536,14 +544,16 @@ impl Vmm {
536544
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| {
537545
block.update_rate_limiter(rl_bytes, rl_ops)
538546
})
539-
.map_err(VmmError::FindDeviceError)
547+
.map_err(VmmError::FindDeviceError)??;
548+
Ok(())
540549
}
541550

542551
/// Updates the rate limiter parameters for block device with `drive_id` id.
543552
pub fn update_vhost_user_block_config(&mut self, drive_id: &str) -> Result<(), VmmError> {
544553
self.device_manager
545554
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| block.update_config())
546-
.map_err(VmmError::FindDeviceError)
555+
.map_err(VmmError::FindDeviceError)??;
556+
Ok(())
547557
}
548558

549559
/// Updates the rate limiter parameters for net device with `net_id` id.
@@ -571,9 +581,11 @@ impl Vmm {
571581

572582
/// Returns the latest balloon statistics if they are enabled.
573583
pub fn latest_balloon_stats(&self) -> Result<BalloonStats, VmmError> {
574-
self.device_manager
584+
let stats = self
585+
.device_manager
575586
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| dev.latest_stats())
576-
.map_err(VmmError::FindDeviceError)
587+
.map_err(VmmError::FindDeviceError)??;
588+
Ok(stats)
577589
}
578590

579591
/// Updates configuration for the balloon device target size.
@@ -582,7 +594,8 @@ impl Vmm {
582594
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| {
583595
dev.update_size(amount_mib)
584596
})
585-
.map_err(VmmError::FindDeviceError)
597+
.map_err(VmmError::FindDeviceError)??;
598+
Ok(())
586599
}
587600

588601
/// Updates configuration for the balloon device as described in `balloon_stats_update`.
@@ -594,7 +607,8 @@ impl Vmm {
594607
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| {
595608
dev.update_stats_polling_interval(stats_polling_interval_s)
596609
})
597-
.map_err(VmmError::FindDeviceError)
610+
.map_err(VmmError::FindDeviceError)??;
611+
Ok(())
598612
}
599613

600614
/// Signals Vmm to stop and exit.

0 commit comments

Comments
 (0)