Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 749b376

Browse files
committedMay 24, 2024
Change pedantically incorrect OnceCell/OnceLock wording
While the semantic intent of a OnceCell/OnceLock is that it can only be written to once (upon init), the fact of the matter is that both these types offer a `take(&mut self) -> Option<T>` mechanism that, when successful, resets the cell to its initial state, thereby technically allowing it to be written to again. Despite the fact that this can only happen with a mutable reference (generally only used during the construction of the OnceCell/OnceLock), it would be incorrect to say that the type itself as a whole categorically prevents being initialized or written to more than once (since it is possible to imagine an identical type only without the `take()` method that actually fulfills that contract). To clarify, change "that cannot be.." to "that nominally cannot.." and add a note to OnceCell about what can be done with an `&mut Self` reference.
1 parent a365890 commit 749b376

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed
 

‎core/src/cell/once.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use crate::cell::UnsafeCell;
22
use crate::fmt;
33
use crate::mem;
44

5-
/// A cell which can be written to only once.
5+
/// A cell which can nominally be written to only once.
66
///
77
/// This allows obtaining a shared `&T` reference to its inner value without copying or replacing
88
/// it (unlike [`Cell`]), and without runtime borrow checks (unlike [`RefCell`]). However,
99
/// only immutable references can be obtained unless one has a mutable reference to the cell
10-
/// itself.
10+
/// itself. In the same vein, the cell can only be re-initialized with such a mutable reference.
1111
///
1212
/// For a thread-safe version of this struct, see [`std::sync::OnceLock`].
1313
///

‎std/src/sync/once_lock.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::mem::MaybeUninit;
55
use crate::panic::{RefUnwindSafe, UnwindSafe};
66
use crate::sync::Once;
77

8-
/// A synchronization primitive which can be written to only once.
8+
/// A synchronization primitive which can nominally be written to only once.
99
///
1010
/// This type is a thread-safe [`OnceCell`], and can be used in statics.
1111
///

0 commit comments

Comments
 (0)
Failed to load comments.