Skip to content

Commit 538ddb0

Browse files
thread_local: use less &mut T in LazyKeyInner::take
Instead, use raw pointers to accomplish internal mutability throughout.
1 parent 9e6c4fd commit 538ddb0

File tree

1 file changed

+8
-6
lines changed
  • library/std/src/sys/thread_local

1 file changed

+8
-6
lines changed

library/std/src/sys/thread_local/mod.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ mod lazy {
9191
}
9292
}
9393

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

0 commit comments

Comments
 (0)