Skip to content

Commit 66abeb1

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 <yegorlz@amazon.co.uk>
1 parent e63983b commit 66abeb1

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
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: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,9 @@ 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)
531-
})
532-
.map_err(VmmError::FindDeviceError)??;
531+
})??;
533532
Ok(())
534533
}
535534

@@ -541,18 +540,16 @@ impl Vmm {
541540
rl_ops: BucketUpdate,
542541
) -> Result<(), VmmError> {
543542
self.device_manager
544-
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| {
543+
.with_virtio_device(drive_id, |block: &mut Block| {
545544
block.update_rate_limiter(rl_bytes, rl_ops)
546-
})
547-
.map_err(VmmError::FindDeviceError)??;
545+
})??;
548546
Ok(())
549547
}
550548

551549
/// Updates the rate limiter parameters for block device with `drive_id` id.
552550
pub fn update_vhost_user_block_config(&mut self, drive_id: &str) -> Result<(), VmmError> {
553551
self.device_manager
554-
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| block.update_config())
555-
.map_err(VmmError::FindDeviceError)??;
552+
.with_virtio_device(drive_id, |block: &mut Block| block.update_config())??;
556553
Ok(())
557554
}
558555

@@ -566,35 +563,34 @@ impl Vmm {
566563
tx_ops: BucketUpdate,
567564
) -> Result<(), VmmError> {
568565
self.device_manager
569-
.with_virtio_device_with_id(net_id, |net: &mut Net| {
566+
.with_virtio_device(net_id, |net: &mut Net| {
570567
net.patch_rate_limiters(rx_bytes, rx_ops, tx_bytes, tx_ops)
571-
})
572-
.map_err(VmmError::FindDeviceError)
568+
})?;
569+
Ok(())
573570
}
574571

575572
/// Returns a reference to the balloon device if present.
576573
pub fn balloon_config(&self) -> Result<BalloonConfig, VmmError> {
577-
self.device_manager
578-
.with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| dev.config())
579-
.map_err(VmmError::FindDeviceError)
574+
let config = self
575+
.device_manager
576+
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| dev.config())?;
577+
Ok(config)
580578
}
581579

582580
/// Returns the latest balloon statistics if they are enabled.
583581
pub fn latest_balloon_stats(&self) -> Result<BalloonStats, VmmError> {
584582
let stats = self
585583
.device_manager
586-
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| dev.latest_stats())
587-
.map_err(VmmError::FindDeviceError)??;
584+
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| dev.latest_stats())??;
588585
Ok(stats)
589586
}
590587

591588
/// Updates configuration for the balloon device target size.
592589
pub fn update_balloon_config(&mut self, amount_mib: u32) -> Result<(), VmmError> {
593590
self.device_manager
594-
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| {
591+
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| {
595592
dev.update_size(amount_mib)
596-
})
597-
.map_err(VmmError::FindDeviceError)??;
593+
})??;
598594
Ok(())
599595
}
600596

@@ -604,10 +600,9 @@ impl Vmm {
604600
stats_polling_interval_s: u16,
605601
) -> Result<(), VmmError> {
606602
self.device_manager
607-
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| {
603+
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| {
608604
dev.update_stats_polling_interval(stats_polling_interval_s)
609-
})
610-
.map_err(VmmError::FindDeviceError)??;
605+
})??;
611606
Ok(())
612607
}
613608

0 commit comments

Comments
 (0)