Skip to content

Commit e4dd0b4

Browse files
committed
refactor: further simplify with_virtio_device functions
Replace 2 functions: `try_with_virtio_device_with_id` and `with_virtio_device_with_id`, with single `with_virtio_device` that handles any return type of the passed function. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 0223542 commit e4dd0b4

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

src/vmm/src/device_manager/mod.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -368,15 +368,10 @@ impl DeviceManager {
368368
}
369369

370370
/// Run fn `f()` for the virtio device matching `virtio_type` and `id`.
371-
pub fn try_with_virtio_device_with_id<T, F, R, E>(
372-
&self,
373-
id: &str,
374-
f: F,
375-
) -> Result<Result<R, E>, FindDeviceError>
371+
pub fn with_virtio_device<T, F, R>(&self, id: &str, f: F) -> Result<R, FindDeviceError>
376372
where
377373
T: VirtioDevice + 'static + Debug,
378-
E: std::error::Error + 'static + Send + Sync,
379-
F: FnOnce(&mut T) -> Result<R, E>,
374+
F: FnOnce(&mut T) -> R,
380375
{
381376
if let Some(device) = self.get_virtio_device(T::const_device_type(), id) {
382377
let mut dev = device.lock().expect("Poisoned lock");
@@ -388,15 +383,6 @@ impl DeviceManager {
388383
Err(FindDeviceError::DeviceNotFound)
389384
}
390385
}
391-
392-
/// Run fn `f()` for the virtio device matching `virtio_type` and `id`.
393-
pub fn with_virtio_device_with_id<T, F, R>(&self, id: &str, f: F) -> Result<R, FindDeviceError>
394-
where
395-
T: VirtioDevice + 'static + Debug,
396-
F: FnOnce(&mut T) -> R,
397-
{
398-
self.try_with_virtio_device_with_id(id, |dev: &mut T| Ok::<R, FindDeviceError>(f(dev)))?
399-
}
400386
}
401387

402388
#[derive(Debug, Default, Clone, Serialize, Deserialize)]

src/vmm/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl Vmm {
526526
path_on_host: String,
527527
) -> Result<(), VmmError> {
528528
self.device_manager
529-
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| {
529+
.with_virtio_device(drive_id, |block: &mut Block| {
530530
block.update_disk_image(path_on_host)
531531
})
532532
.map_err(VmmError::FindDeviceError)??;
@@ -541,7 +541,7 @@ impl Vmm {
541541
rl_ops: BucketUpdate,
542542
) -> Result<(), VmmError> {
543543
self.device_manager
544-
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| {
544+
.with_virtio_device(drive_id, |block: &mut Block| {
545545
block.update_rate_limiter(rl_bytes, rl_ops)
546546
})
547547
.map_err(VmmError::FindDeviceError)??;
@@ -551,7 +551,7 @@ impl Vmm {
551551
/// Updates the rate limiter parameters for block device with `drive_id` id.
552552
pub fn update_vhost_user_block_config(&mut self, drive_id: &str) -> Result<(), VmmError> {
553553
self.device_manager
554-
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| block.update_config())
554+
.with_virtio_device(drive_id, |block: &mut Block| block.update_config())
555555
.map_err(VmmError::FindDeviceError)??;
556556
Ok(())
557557
}
@@ -566,7 +566,7 @@ impl Vmm {
566566
tx_ops: BucketUpdate,
567567
) -> Result<(), VmmError> {
568568
self.device_manager
569-
.with_virtio_device_with_id(net_id, |net: &mut Net| {
569+
.with_virtio_device(net_id, |net: &mut Net| {
570570
net.patch_rate_limiters(rx_bytes, rx_ops, tx_bytes, tx_ops)
571571
})
572572
.map_err(VmmError::FindDeviceError)
@@ -575,23 +575,23 @@ impl Vmm {
575575
/// Returns a reference to the balloon device if present.
576576
pub fn balloon_config(&self) -> Result<BalloonConfig, VmmError> {
577577
self.device_manager
578-
.with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| dev.config())
578+
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| dev.config())
579579
.map_err(VmmError::FindDeviceError)
580580
}
581581

582582
/// Returns the latest balloon statistics if they are enabled.
583583
pub fn latest_balloon_stats(&self) -> Result<BalloonStats, VmmError> {
584584
let stats = self
585585
.device_manager
586-
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| dev.latest_stats())
586+
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| dev.latest_stats())
587587
.map_err(VmmError::FindDeviceError)??;
588588
Ok(stats)
589589
}
590590

591591
/// Updates configuration for the balloon device target size.
592592
pub fn update_balloon_config(&mut self, amount_mib: u32) -> Result<(), VmmError> {
593593
self.device_manager
594-
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| {
594+
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| {
595595
dev.update_size(amount_mib)
596596
})
597597
.map_err(VmmError::FindDeviceError)??;
@@ -604,7 +604,7 @@ impl Vmm {
604604
stats_polling_interval_s: u16,
605605
) -> Result<(), VmmError> {
606606
self.device_manager
607-
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| {
607+
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| {
608608
dev.update_stats_polling_interval(stats_polling_interval_s)
609609
})
610610
.map_err(VmmError::FindDeviceError)??;

0 commit comments

Comments
 (0)