Skip to content

Commit

Permalink
Rewrite LazyKeyInner::take to use less &mut Ts
Browse files Browse the repository at this point in the history
Instead, use raw pointers to accomplish internal mutability throughout.
  • Loading branch information
workingjubilee committed Apr 25, 2024
1 parent 9e6c4fd commit 141bba9
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions library/std/src/sys/thread_local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ mod lazy {
}
}

/// 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) }
}
}
}
Expand Down

0 comments on commit 141bba9

Please sign in to comment.