Skip to content

Commit 6520488

Browse files
committed
std: add safety comment in LazyLock::get
1 parent 7165e61 commit 6520488

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

library/std/src/sync/lazy_lock.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
111111
impl<T, F> LazyLock<T, F> {
112112
/// Get the inner value if it has already been initialized.
113113
fn get(&self) -> Option<&T> {
114-
if self.once.is_completed() { Some(unsafe { &*(*self.data.get()).value }) } else { None }
114+
if self.once.is_completed() {
115+
// SAFETY:
116+
// The closure has been run successfully, so `value` has been initialized
117+
// and will not be modified again.
118+
Some(unsafe { &*(*self.data.get()).value })
119+
} else {
120+
None
121+
}
115122
}
116123
}
117124

0 commit comments

Comments
 (0)