Skip to content

Commit 33389b0

Browse files
committed
more explicitly state the basic rules of working with the obtained raw pointers
1 parent 7953644 commit 33389b0

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

core/src/ptr/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,15 @@ where
779779
///
780780
/// For `r: &T`, `from_ref(r)` is equivalent to `r as *const T`, but is a bit safer since it will
781781
/// never silently change type or mutability, in particular if the code is refactored.
782+
///
783+
/// The caller must ensure that the pointee outlives the pointer this function returns, or else it
784+
/// will end up pointing to garbage.
785+
///
786+
/// The caller must also ensure that the memory the pointer (non-transitively) points to is never
787+
/// written to (except inside an `UnsafeCell`) using this pointer or any pointer derived from it. If
788+
/// you need to mutate the pointee, use [`from_mut`]`. Specifically, to turn a mutable reference `m:
789+
/// &mut T` into `*const T`, prefer `from_mut(m).cast_const()` to obtain a pointer that can later be
790+
/// used for mutation.
782791
#[inline(always)]
783792
#[must_use]
784793
#[stable(feature = "ptr_from_ref", since = "1.76.0")]
@@ -791,6 +800,9 @@ pub const fn from_ref<T: ?Sized>(r: &T) -> *const T {
791800

792801
/// Convert a mutable reference to a raw pointer.
793802
///
803+
/// The caller must ensure that the pointee outlives the pointer this function returns, or else it
804+
/// will end up pointing to garbage.
805+
///
794806
/// For `r: &mut T`, `from_mut(r)` is equivalent to `r as *mut T`, but is a bit safer since it will
795807
/// never silently change type or mutability, in particular if the code is refactored.
796808
#[inline(always)]

0 commit comments

Comments
 (0)