Skip to content

Commit ce75632

Browse files
committed
Document that Unique::empty() and NonNull::dangling() aren't sentinel values
The documentation of Unique::empty() and NonNull::dangling() could potentially suggest that they work as sentinel values indicating a not-yet-initialized pointer. However, they both declare a non-null pointer equal to the alignment of the type, which could potentially reference a valid value of that type (specifically, the first such valid value in memory). Explicitly document that the return value of these functions does not work as a sentinel value.
1 parent 12ed235 commit ce75632

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/libcore/ptr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2703,6 +2703,11 @@ impl<T: Sized> Unique<T> {
27032703
///
27042704
/// This is useful for initializing types which lazily allocate, like
27052705
/// `Vec::new` does.
2706+
///
2707+
/// Note that the pointer value may potentially represent a valid pointer to
2708+
/// a `T`, which means this must not be used as a "not yet initialized"
2709+
/// sentinel value. Types that lazily allocate must track initialization by
2710+
/// some other means.
27062711
// FIXME: rename to dangling() to match NonNull?
27072712
pub const fn empty() -> Self {
27082713
unsafe {
@@ -2834,6 +2839,11 @@ impl<T: Sized> NonNull<T> {
28342839
///
28352840
/// This is useful for initializing types which lazily allocate, like
28362841
/// `Vec::new` does.
2842+
///
2843+
/// Note that the pointer value may potentially represent a valid pointer to
2844+
/// a `T`, which means this must not be used as a "not yet initialized"
2845+
/// sentinel value. Types that lazily allocate must track initialization by
2846+
/// some other means.
28372847
#[stable(feature = "nonnull", since = "1.25.0")]
28382848
pub fn dangling() -> Self {
28392849
unsafe {

0 commit comments

Comments
 (0)