Skip to content

Commit

Permalink
more explicitly state the basic rules of working with the obtained ra…
Browse files Browse the repository at this point in the history
…w pointers
  • Loading branch information
RalfJung committed Jun 4, 2024
1 parent 7953644 commit 33389b0
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,15 @@ where
///
/// For `r: &T`, `from_ref(r)` is equivalent to `r as *const T`, but is a bit safer since it will
/// never silently change type or mutability, in particular if the code is refactored.
///
/// The caller must ensure that the pointee outlives the pointer this function returns, or else it
/// will end up pointing to garbage.
///
/// The caller must also ensure that the memory the pointer (non-transitively) points to is never
/// written to (except inside an `UnsafeCell`) using this pointer or any pointer derived from it. If
/// you need to mutate the pointee, use [`from_mut`]`. Specifically, to turn a mutable reference `m:
/// &mut T` into `*const T`, prefer `from_mut(m).cast_const()` to obtain a pointer that can later be
/// used for mutation.
#[inline(always)]
#[must_use]
#[stable(feature = "ptr_from_ref", since = "1.76.0")]
Expand All @@ -791,6 +800,9 @@ pub const fn from_ref<T: ?Sized>(r: &T) -> *const T {

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

0 comments on commit 33389b0

Please sign in to comment.