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