Skip to content

Commit 3809c09

Browse files
committed
aliasing guarantee
1 parent 778fdf2 commit 3809c09

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,11 +1218,14 @@ impl<T, A: Allocator> Vec<T, A> {
12181218
/// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
12191219
/// derived from it. If you need to mutate the contents of the slice, use [`as_mut_ptr`].
12201220
///
1221-
/// This method guarantees that when it is called multiple times without
1222-
/// the buffer being reallocated in the mean time, the returned pointer will
1223-
/// always be exactly the same, even for the purpose of the aliasing model, where
1224-
/// pointers may be invalidated even when the actual memory does not move.
1225-
/// See the second example below for how this can be used.
1221+
/// This method guarantees that for the purpose of the aliasing model, this method
1222+
/// does not materialize a reference to the underlying slice, and thus the returned pointer
1223+
/// will remain valid when mixed with other calls to [`as_ptr`] and [`as_mut_ptr`],
1224+
/// Note that calling other methods that materialize mutable references to the slice,
1225+
/// or references to specific elements you are planning on accessing through this pointer,
1226+
/// may still invalidate this pointer.
1227+
/// See the second example below for how this guarantee can be used.
1228+
///
12261229
///
12271230
/// # Examples
12281231
///
@@ -1237,17 +1240,22 @@ impl<T, A: Allocator> Vec<T, A> {
12371240
/// }
12381241
/// ```
12391242
///
1240-
/// The validity guarantee works out this way:
1243+
/// Due to the aliasing guarantee, the following code is legal:
12411244
///
12421245
/// ```rust
1243-
/// let mut v = vec![0];
1244-
/// let ptr = v.as_ptr();
1245-
/// let x = ptr.read();
1246-
/// v[0] = 5;
1247-
/// // Notably, the write above did *not* invalidate `ptr1`:
1248-
/// let x = ptr.read();
1246+
/// unsafe {
1247+
/// let mut v = vec![0];
1248+
/// let ptr1 = v.as_ptr();
1249+
/// let _ = ptr1.read();
1250+
/// let ptr2 = v.as_mut_ptr();
1251+
/// ptr2.write(2);
1252+
/// // Notably, the write to `ptr2` did *not* invalidate `ptr1`:
1253+
/// let _ = ptr1.read();
1254+
/// }
12491255
/// ```
1256+
///
12501257
/// [`as_mut_ptr`]: Vec::as_mut_ptr
1258+
/// [`as_ptr`]: Vec::as_ptr
12511259
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
12521260
#[inline]
12531261
pub fn as_ptr(&self) -> *const T {
@@ -1264,11 +1272,13 @@ impl<T, A: Allocator> Vec<T, A> {
12641272
/// Modifying the vector may cause its buffer to be reallocated,
12651273
/// which would also make any pointers to it invalid.
12661274
///
1267-
/// This method guarantees that when it is called multiple times without
1268-
/// the buffer being reallocated in the mean time, the returned pointer will
1269-
/// always be exactly the same, even for the purpose of the aliasing model, where
1270-
/// pointers may be invalidated even when the actual memory does not move.
1271-
/// See the second example below for how this can be used.
1275+
/// This method guarantees that for the purpose of the aliasing model, this method
1276+
/// does not materialize a reference to the underlying slice, and thus the returned pointer
1277+
/// will remain valid when mixed with other calls to [`as_ptr`] and [`as_mut_ptr`],
1278+
/// Note that calling other methods that materialize references to the slice,
1279+
/// or references to specific elements you are planning on accessing through this pointer,
1280+
/// may still invalidate this pointer.
1281+
/// See the second example below for how this guarantee can be used.
12721282
///
12731283
///
12741284
/// # Examples
@@ -1289,17 +1299,22 @@ impl<T, A: Allocator> Vec<T, A> {
12891299
/// assert_eq!(&*x, &[0, 1, 2, 3]);
12901300
/// ```
12911301
///
1292-
/// The validity guarantee works out this way:
1302+
/// Due to the aliasing guarantee, the following code is legal:
12931303
///
12941304
/// ```rust
1295-
/// let mut v = vec![0];
1296-
/// let ptr1 = v.as_mut_ptr();
1297-
/// ptr1.write(1);
1298-
/// let ptr2 = v.as_mut_ptr();
1299-
/// ptr2.write(2);
1300-
/// // Notably, the write to `ptr2` did *not* invalidate `ptr1`:
1301-
/// ptr1.write(3);
1305+
/// unsafe {
1306+
/// let mut v = vec![0];
1307+
/// let ptr1 = v.as_mut_ptr();
1308+
/// ptr1.write(1);
1309+
/// let ptr2 = v.as_mut_ptr();
1310+
/// ptr2.write(2);
1311+
/// // Notably, the write to `ptr2` did *not* invalidate `ptr1`:
1312+
/// ptr1.write(3);
1313+
/// }
13021314
/// ```
1315+
///
1316+
/// [`as_mut_ptr`]: Vec::as_mut_ptr
1317+
/// [`as_ptr`]: Vec::as_ptr
13031318
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
13041319
#[inline]
13051320
pub fn as_mut_ptr(&mut self) -> *mut T {

0 commit comments

Comments
 (0)