-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add note that Vec::as_mut_ptr() does not materialize a reference to the internal buffer #113859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
778fdf2
3809c09
90642ad
10fc06f
5bf1bfd
6c6875d
7cea69c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
/// | ||
/// ``` | ||
|
@@ -1231,6 +1237,16 @@ impl<T, A: Allocator> Vec<T, A> { | |
/// } | ||
/// ``` | ||
/// | ||
/// The validity guarantee works out this way: | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// ```rust | ||
/// let mut v = vec![0]; | ||
/// let ptr = v.as_ptr(); | ||
/// let x = ptr.read(); | ||
/// v[0] = 5; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i tried to write a similar example for
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say it is up to t-libs if they want to guarantee this -- basically this means that the pointer returned here was not derived via a shared reference. By analogy with |
||
/// // Notably, the write above did *not* invalidate `ptr1`: | ||
Manishearth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// let x = ptr.read(); | ||
/// ``` | ||
/// [`as_mut_ptr`]: Vec::as_mut_ptr | ||
#[stable(feature = "vec_as_ptr", since = "1.37.0")] | ||
#[inline] | ||
|
@@ -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 | ||
Manishearth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// pointers may be invalidated even when the actual memory does not move. | ||
/// See the second example below for how this can be used. | ||
/// | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
|
@@ -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); | ||
/// let ptr2 = v.as_mut_ptr(); | ||
/// ptr2.write(2); | ||
/// // Notably, the write to `ptr2` did *not* invalidate `ptr1`: | ||
/// ptr1.write(3); | ||
/// ``` | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[stable(feature = "vec_as_ptr", since = "1.37.0")] | ||
#[inline] | ||
pub fn as_mut_ptr(&mut self) -> *mut T { | ||
|
Uh oh!
There was an error while loading. Please reload this page.