From 289fdbd7fdab8b6dd5c52c702e1d473f9ff481f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Thu, 16 Oct 2025 15:42:43 +0200 Subject: [PATCH] refactor: remove nightly feature `map_try_insert` --- src/executor/vsock.rs | 18 ++++++++++----- src/lib.rs | 1 - src/scheduler/mod.rs | 22 ++++++++++-------- src/scheduler/task/mod.rs | 48 ++++++++++----------------------------- 4 files changed, 37 insertions(+), 52 deletions(-) diff --git a/src/executor/vsock.rs b/src/executor/vsock.rs index c635ae10aa..740af43e5f 100644 --- a/src/executor/vsock.rs +++ b/src/executor/vsock.rs @@ -1,4 +1,4 @@ -use alloc::collections::BTreeMap; +use alloc::collections::{BTreeMap, btree_map}; use alloc::vec::Vec; use core::future; use core::task::Poll; @@ -170,10 +170,15 @@ impl VsockMap { } pub fn bind(&mut self, port: u32) -> io::Result<()> { - self.port_map - .try_insert(port, RawSocket::new(VsockState::Listen)) - .map_err(|_| Errno::Addrinuse)?; - Ok(()) + let entry = self.port_map.entry(port); + + match entry { + btree_map::Entry::Vacant(vacant_entry) => { + vacant_entry.insert(RawSocket::new(VsockState::Listen)); + Ok(()) + } + btree_map::Entry::Occupied(_occupied_entry) => Err(Errno::Addrinuse), + } } pub fn connect(&mut self, port: u32, cid: u32) -> io::Result { @@ -182,7 +187,8 @@ impl VsockMap { raw.remote_cid = cid; raw.remote_port = port; - if self.port_map.try_insert(i, raw).is_ok() { + if let btree_map::Entry::Vacant(vacant_entry) = self.port_map.entry(i) { + vacant_entry.insert(raw); return Ok(i); } } diff --git a/src/lib.rs b/src/lib.rs index 326d7db3ba..4a203ce562 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,6 @@ #![feature(allocator_api)] #![feature(linkage)] #![feature(linked_list_cursors)] -#![feature(map_try_insert)] #![feature(maybe_uninit_as_bytes)] #![feature(maybe_uninit_slice)] #![feature(maybe_uninit_write_slice)] diff --git a/src/scheduler/mod.rs b/src/scheduler/mod.rs index 0196ea7b2f..3732e27b18 100644 --- a/src/scheduler/mod.rs +++ b/src/scheduler/mod.rs @@ -14,7 +14,7 @@ use core::sync::atomic::{AtomicI32, AtomicU32, Ordering}; use ahash::RandomState; use crossbeam_utils::Backoff; -use hashbrown::HashMap; +use hashbrown::{HashMap, hash_map}; use hermit_sync::*; #[cfg(target_arch = "riscv64")] use riscv::register::sstatus; @@ -564,10 +564,12 @@ impl PerCoreScheduler { }; let fd = new_fd()?; - if object_map.try_insert(fd, obj).is_err() { - Err(Errno::Mfile) - } else { - Ok(fd) + match object_map.entry(fd) { + hash_map::Entry::Occupied(_occupied_entry) => Err(Errno::Mfile), + hash_map::Entry::Vacant(vacant_entry) => { + vacant_entry.insert(obj); + Ok(fd) + } } }) } @@ -583,10 +585,12 @@ impl PerCoreScheduler { let obj = object_map.get(&fd1).cloned().ok_or(Errno::Badf)?; - if object_map.try_insert(fd2, obj).is_err() { - Err(Errno::Mfile) - } else { - Ok(fd2) + match object_map.entry(fd2) { + hash_map::Entry::Occupied(_occupied_entry) => Err(Errno::Mfile), + hash_map::Entry::Vacant(vacant_entry) => { + vacant_entry.insert(obj); + Ok(fd2) + } } }) } diff --git a/src/scheduler/task/mod.rs b/src/scheduler/task/mod.rs index 98484c13c5..dfca1377fe 100644 --- a/src/scheduler/task/mod.rs +++ b/src/scheduler/task/mod.rs @@ -468,43 +468,19 @@ impl Task { let objmap = OBJECT_MAP.get().unwrap().clone(); let mut guard = objmap.write(); if env::is_uhyve() { - guard - .try_insert( - STDIN_FILENO, - Arc::new(async_lock::RwLock::new(UhyveStdin::new())), - ) - .expect("cannot insert stdin"); - guard - .try_insert( - STDOUT_FILENO, - Arc::new(async_lock::RwLock::new(UhyveStdout::new())), - ) - .expect("cannot insert stdout"); - guard - .try_insert( - STDERR_FILENO, - Arc::new(async_lock::RwLock::new(UhyveStderr::new())), - ) - .expect("cannot insert stderr"); + let stdin = Arc::new(async_lock::RwLock::new(UhyveStdin::new())); + let stdout = Arc::new(async_lock::RwLock::new(UhyveStdout::new())); + let stderr = Arc::new(async_lock::RwLock::new(UhyveStderr::new())); + guard.insert(STDIN_FILENO, stdin); + guard.insert(STDOUT_FILENO, stdout); + guard.insert(STDERR_FILENO, stderr); } else { - guard - .try_insert( - STDIN_FILENO, - Arc::new(async_lock::RwLock::new(GenericStdin::new())), - ) - .expect("cannot insert stdin"); - guard - .try_insert( - STDOUT_FILENO, - Arc::new(async_lock::RwLock::new(GenericStdout::new())), - ) - .expect("cannot insert stdout"); - guard - .try_insert( - STDERR_FILENO, - Arc::new(async_lock::RwLock::new(GenericStderr::new())), - ) - .expect("cannot insert stderr"); + let stdin = Arc::new(async_lock::RwLock::new(GenericStdin::new())); + let stdout = Arc::new(async_lock::RwLock::new(GenericStdout::new())); + let stderr = Arc::new(async_lock::RwLock::new(GenericStderr::new())); + guard.insert(STDIN_FILENO, stdin); + guard.insert(STDOUT_FILENO, stdout); + guard.insert(STDERR_FILENO, stderr); } }