From ca354c22ba5beef6a6d9cab482da16e46bf6322c Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 29 Jun 2026 04:09:48 -0700 Subject: [PATCH] Reduce unsafe usage with MaybeUninit::write Available since Rust 1.55 (we already require 1.63). https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html#method.write Signed-off-by: Anders Kaseorg --- src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9be72e0..69fa6b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -286,13 +286,12 @@ impl ThreadLocal { // the local thread can access this value. The target location of the pointer // is valid since it came from the UnsafeCell and a valid initialized value // of type `T` is being written into the cell. - unsafe { value_ptr.write(MaybeUninit::new(data)) }; + let data_ref = &*unsafe { &mut *value_ptr }.write(data); entry.present.store(true, Ordering::Release); self.values.fetch_add(1, Ordering::Release); - // SAFETY: The value was just initialized to the provided value. - unsafe { (&*value_ptr).assume_init_ref() } + data_ref } #[inline]