Skip to content

Commit 98137eb

Browse files
coolreader18youknowone
authored andcommitted
Switch to const-initialized thread_local variables where appropriate
1 parent 2230d6c commit 98137eb

4 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎common/src/static_cell.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ mod non_threading {
5656
$($(#[$attr])*
5757
$vis static $name: $crate::static_cell::StaticCell<$t> = {
5858
::std::thread_local! {
59-
$vis static $name: $crate::lock::OnceCell<&'static $t> = $crate::lock::OnceCell::new();
59+
$vis static $name: $crate::lock::OnceCell<&'static $t> = const {
60+
$crate::lock::OnceCell::new()
61+
};
6062
}
6163
$crate::static_cell::StaticCell::_from_local_key(&$name)
6264
};)+

‎common/src/str.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,10 @@ pub mod levenshtein {
486486

487487
pub fn levenshtein_distance(a: &str, b: &str, max_cost: usize) -> usize {
488488
thread_local! {
489-
static BUFFER: RefCell<[usize; MAX_STRING_SIZE]> = const { RefCell::new([0usize; MAX_STRING_SIZE]) };
489+
#[allow(clippy::declare_interior_mutable_const)]
490+
static BUFFER: RefCell<[usize; MAX_STRING_SIZE]> = const {
491+
RefCell::new([0usize; MAX_STRING_SIZE])
492+
};
490493
}
491494

492495
if a == b {

‎vm/src/stdlib/thread.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ pub(crate) mod _thread {
355355
Err(vm.new_exception_empty(vm.ctx.exceptions.system_exit.to_owned()))
356356
}
357357

358-
thread_local!(static SENTINELS: RefCell<Vec<PyRef<Lock>>> = RefCell::default());
358+
thread_local! {
359+
static SENTINELS: RefCell<Vec<PyRef<Lock>>> = const { RefCell::new(Vec::new()) };
360+
}
359361

360362
#[pyfunction]
361363
fn _set_sentinel(vm: &VirtualMachine) -> PyRef<Lock> {

‎wasm/lib/src/vm_class.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ pub fn add_init_func(f: fn(&mut VirtualMachine)) {
8686
// https://rustwasm.github.io/2018/10/24/multithreading-rust-and-wasm.html#atomic-instructions
8787
thread_local! {
8888
static STORED_VMS: RefCell<HashMap<String, Rc<StoredVirtualMachine>>> = RefCell::default();
89-
static VM_INIT_FUNCS: RefCell<Vec<fn(&mut VirtualMachine)>> = RefCell::default();
89+
static VM_INIT_FUNCS: RefCell<Vec<fn(&mut VirtualMachine)>> = const {
90+
RefCell::new(Vec::new())
91+
};
9092
}
9193

9294
pub fn get_vm_id(vm: &VirtualMachine) -> &str {

0 commit comments

Comments
 (0)