Add SAFETY comments to the thread local implementation
Reduce `unsafe` block scope and add `SAFETY` comments.
This commit is contained in:
@@ -281,11 +281,10 @@ impl<T: 'static> LocalKey<T> {
|
|||||||
where
|
where
|
||||||
F: FnOnce(&T) -> R,
|
F: FnOnce(&T) -> R,
|
||||||
{
|
{
|
||||||
unsafe {
|
// SAFETY: `inner` is safe to call within the lifetime of the thread
|
||||||
let thread_local = (self.inner)(None).ok_or(AccessError)?;
|
let thread_local = unsafe { (self.inner)(None).ok_or(AccessError)? };
|
||||||
Ok(f(thread_local))
|
Ok(f(thread_local))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Acquires a reference to the value in this TLS key, initializing it with
|
/// Acquires a reference to the value in this TLS key, initializing it with
|
||||||
/// `init` if it wasn't already initialized on this thread.
|
/// `init` if it wasn't already initialized on this thread.
|
||||||
@@ -303,15 +302,18 @@ impl<T: 'static> LocalKey<T> {
|
|||||||
where
|
where
|
||||||
F: FnOnce(Option<T>, &T) -> R,
|
F: FnOnce(Option<T>, &T) -> R,
|
||||||
{
|
{
|
||||||
unsafe {
|
|
||||||
let mut init = Some(init);
|
let mut init = Some(init);
|
||||||
let reference = (self.inner)(Some(&mut init)).expect(
|
|
||||||
|
// SAFETY: `inner` is safe to call within the lifetime of the thread
|
||||||
|
let reference = unsafe {
|
||||||
|
(self.inner)(Some(&mut init)).expect(
|
||||||
"cannot access a Thread Local Storage value \
|
"cannot access a Thread Local Storage value \
|
||||||
during or after destruction",
|
during or after destruction",
|
||||||
);
|
)
|
||||||
|
};
|
||||||
|
|
||||||
f(init, reference)
|
f(init, reference)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> LocalKey<Cell<T>> {
|
impl<T: 'static> LocalKey<Cell<T>> {
|
||||||
|
|||||||
Reference in New Issue
Block a user