@@ -1218,11 +1218,14 @@ impl<T, A: Allocator> Vec<T, A> {
1218
1218
/// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
1219
1219
/// derived from it. If you need to mutate the contents of the slice, use [`as_mut_ptr`].
1220
1220
///
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
+ ///
1226
1229
///
1227
1230
/// # Examples
1228
1231
///
@@ -1237,17 +1240,22 @@ impl<T, A: Allocator> Vec<T, A> {
1237
1240
/// }
1238
1241
/// ```
1239
1242
///
1240
- /// The validity guarantee works out this way :
1243
+ /// Due to the aliasing guarantee, the following code is legal :
1241
1244
///
1242
1245
/// ```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
+ /// }
1249
1255
/// ```
1256
+ ///
1250
1257
/// [`as_mut_ptr`]: Vec::as_mut_ptr
1258
+ /// [`as_ptr`]: Vec::as_ptr
1251
1259
#[ stable( feature = "vec_as_ptr" , since = "1.37.0" ) ]
1252
1260
#[ inline]
1253
1261
pub fn as_ptr ( & self ) -> * const T {
@@ -1264,11 +1272,13 @@ impl<T, A: Allocator> Vec<T, A> {
1264
1272
/// Modifying the vector may cause its buffer to be reallocated,
1265
1273
/// which would also make any pointers to it invalid.
1266
1274
///
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.
1272
1282
///
1273
1283
///
1274
1284
/// # Examples
@@ -1289,17 +1299,22 @@ impl<T, A: Allocator> Vec<T, A> {
1289
1299
/// assert_eq!(&*x, &[0, 1, 2, 3]);
1290
1300
/// ```
1291
1301
///
1292
- /// The validity guarantee works out this way :
1302
+ /// Due to the aliasing guarantee, the following code is legal :
1293
1303
///
1294
1304
/// ```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
+ /// }
1302
1314
/// ```
1315
+ ///
1316
+ /// [`as_mut_ptr`]: Vec::as_mut_ptr
1317
+ /// [`as_ptr`]: Vec::as_ptr
1303
1318
#[ stable( feature = "vec_as_ptr" , since = "1.37.0" ) ]
1304
1319
#[ inline]
1305
1320
pub fn as_mut_ptr ( & mut self ) -> * mut T {
0 commit comments