Skip to content

Commit 51cc5a4

Browse files
committed
chore: Eliminate miscellaneous .unwraps()s
Removes a handful of unwraps by slightly rearranging code (e.g. by using pattern matching to combine the fallible operation with the check that ensures it won't actually fail). Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
1 parent 4607c70 commit 51cc5a4

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

src/vmm/src/devices/virtio/vsock/unix/muxer_killq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl MuxerKillQ {
106106
pub fn pop(&mut self) -> Option<ConnMapKey> {
107107
if let Some(item) = self.q.front() {
108108
if Instant::now() > item.kill_time {
109-
return Some(self.q.pop_front().unwrap().key);
109+
return self.q.pop_front().map(|entry| entry.key);
110110
}
111111
}
112112
None

src/vmm/src/dumbo/tcp/connection.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,9 @@ impl Connection {
714714

715715
// We check this here because if a valid payload has been received, then we must have
716716
// set enqueue_ack = true earlier.
717-
if payload_len > 0 {
717+
if let Some(payload_len) = NonZeroUsize::new(payload_len.into()) {
718718
buf[..payload_len.into()].copy_from_slice(s.payload());
719-
// The unwrap is safe because payload_len > 0.
720-
return Ok((
721-
Some(NonZeroUsize::new(payload_len.into()).unwrap()),
722-
recv_status_flags,
723-
));
719+
return Ok((Some(payload_len), recv_status_flags));
724720
}
725721
}
726722

src/vmm/src/vmm_config/net.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,20 @@ impl NetBuilder {
109109
&mut self,
110110
netif_config: NetworkInterfaceConfig,
111111
) -> Result<Arc<Mutex<Net>>, NetworkInterfaceError> {
112-
let mac_conflict = |net: &Arc<Mutex<Net>>| {
113-
let net = net.lock().expect("Poisoned lock");
114-
// Check if another net dev has same MAC.
115-
netif_config.guest_mac.is_some()
116-
&& netif_config.guest_mac.as_ref() == net.guest_mac()
117-
&& &netif_config.iface_id != net.id()
118-
};
119-
// Validate there is no Mac conflict.
120-
// No need to validate host_dev_name conflict. In such a case,
121-
// an error will be thrown during device creation anyway.
122-
if self.net_devices.iter().any(mac_conflict) {
123-
return Err(NetworkInterfaceError::GuestMacAddressInUse(
124-
netif_config.guest_mac.unwrap().to_string(),
125-
));
112+
if let Some(ref mac_address) = netif_config.guest_mac {
113+
let mac_conflict = |net: &Arc<Mutex<Net>>| {
114+
let net = net.lock().expect("Poisoned lock");
115+
// Check if another net dev has same MAC.
116+
Some(mac_address) == net.guest_mac() && &netif_config.iface_id != net.id()
117+
};
118+
// Validate there is no Mac conflict.
119+
// No need to validate host_dev_name conflict. In such a case,
120+
// an error will be thrown during device creation anyway.
121+
if self.net_devices.iter().any(mac_conflict) {
122+
return Err(NetworkInterfaceError::GuestMacAddressInUse(
123+
mac_address.to_string(),
124+
));
125+
}
126126
}
127127

128128
// If this is an update, just remove the old one.

src/vmm/src/vstate/vm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ impl Vm {
235235
}
236236
guest_mem
237237
.iter()
238-
.enumerate()
239-
.try_for_each(|(index, region)| {
238+
.zip(0u32..)
239+
.try_for_each(|(region, slot)| {
240240
let memory_region = kvm_userspace_memory_region {
241-
slot: u32::try_from(index).unwrap(),
241+
slot,
242242
guest_phys_addr: region.start_addr().raw_value(),
243243
memory_size: region.len(),
244244
// It's safe to unwrap because the guest address is valid.

0 commit comments

Comments
 (0)