Skip to content

Commit

Permalink
Add note that Vec::as_mut_ptr() does not materialize a reference to t…
Browse files Browse the repository at this point in the history
…he internal buffer
  • Loading branch information
Manishearth committed Jul 25, 2023
1 parent 77e24f9 commit 778fdf2
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,12 @@ impl<T, A: Allocator> Vec<T, A> {
/// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
/// derived from it. If you need to mutate the contents of the slice, use [`as_mut_ptr`].
///
/// This method guarantees that when it is called multiple times without
/// the buffer being reallocated in the mean time, the returned pointer will
/// always be exactly the same, even for the purpose of the aliasing model, where
/// pointers may be invalidated even when the actual memory does not move.
/// See the second example below for how this can be used.
///
/// # Examples
///
/// ```
Expand All @@ -1231,6 +1237,16 @@ impl<T, A: Allocator> Vec<T, A> {
/// }
/// ```
///
/// The validity guarantee works out this way:
///
/// ```rust
/// let mut v = vec![0];
/// let ptr = v.as_ptr();
/// let x = ptr.read();

Check failure on line 1245 in library/alloc/src/vec/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

call to unsafe function is unsafe and requires unsafe function or block
/// v[0] = 5;
/// // Notably, the write above did *not* invalidate `ptr1`:
/// let x = ptr.read();

Check failure on line 1248 in library/alloc/src/vec/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

call to unsafe function is unsafe and requires unsafe function or block
/// ```
/// [`as_mut_ptr`]: Vec::as_mut_ptr
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
#[inline]
Expand All @@ -1248,6 +1264,13 @@ impl<T, A: Allocator> Vec<T, A> {
/// Modifying the vector may cause its buffer to be reallocated,
/// which would also make any pointers to it invalid.
///
/// This method guarantees that when it is called multiple times without
/// the buffer being reallocated in the mean time, the returned pointer will
/// always be exactly the same, even for the purpose of the aliasing model, where
/// pointers may be invalidated even when the actual memory does not move.
/// See the second example below for how this can be used.
///
///
/// # Examples
///
/// ```
Expand All @@ -1265,6 +1288,18 @@ impl<T, A: Allocator> Vec<T, A> {
/// }
/// assert_eq!(&*x, &[0, 1, 2, 3]);
/// ```
///
/// The validity guarantee works out this way:
///
/// ```rust
/// let mut v = vec![0];
/// let ptr1 = v.as_mut_ptr();
/// ptr1.write(1);

Check failure on line 1297 in library/alloc/src/vec/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

call to unsafe function is unsafe and requires unsafe function or block
/// let ptr2 = v.as_mut_ptr();
/// ptr2.write(2);

Check failure on line 1299 in library/alloc/src/vec/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

call to unsafe function is unsafe and requires unsafe function or block
/// // Notably, the write to `ptr2` did *not* invalidate `ptr1`:
/// ptr1.write(3);

Check failure on line 1301 in library/alloc/src/vec/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

call to unsafe function is unsafe and requires unsafe function or block
/// ```
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut T {
Expand Down

0 comments on commit 778fdf2

Please sign in to comment.