thread_local: use less &mut T in LazyKeyInner::take

Instead, use raw pointers to accomplish internal mutability throughout.
This commit is contained in:
Jubilee Young 2024-04-25 11:05:01 -07:00
parent 9e6c4fddda
commit 538ddb0ac2

View File

@ -91,13 +91,15 @@ pub unsafe fn initialize<F: FnOnce() -> T>(&self, init: F) -> &'static T {
} }
} }
/// The other methods hand out references while taking &self. /// Watch out: unsynchronized internal mutability!
/// As such, callers of this method must ensure no `&` and `&mut` are ///
/// available and used at the same time. /// # Safety
/// Unsound if called while any `&'static T` is active.
#[allow(unused)] #[allow(unused)]
pub unsafe fn take(&mut self) -> Option<T> { pub(crate) unsafe fn take(&self) -> Option<T> {
// SAFETY: See doc comment for this method. let mutable: *mut _ = UnsafeCell::get(&self.inner);
unsafe { (*self.inner.get()).take() } // SAFETY: That's the caller's problem.
unsafe { mutable.replace(None) }
} }
} }
} }