@@ -5272,18 +5272,24 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
5272
5272
///
5273
5273
/// # Safety
5274
5274
///
5275
- /// This function is unsafe as there is no guarantee that the given pointer is
5276
- /// valid for `len` elements, nor whether the lifetime inferred is a suitable
5277
- /// lifetime for the returned slice.
5275
+ /// Behavior is undefined if any of the following conditions are violated:
5278
5276
///
5279
- /// `data` must be non-null and aligned, even for zero-length slices. One
5280
- /// reason for this is that enum layout optimizations may rely on references
5281
- /// (including slices of any length) being aligned and non-null to distinguish
5282
- /// them from other data. You can obtain a pointer that is usable as `data`
5283
- /// for zero-length slices using [`NonNull::dangling()`].
5277
+ /// * `data` must be [valid] for reads for `len * mem::size_of::<T>()` many bytes,
5278
+ /// and it must be properly aligned. This means in particular:
5284
5279
///
5285
- /// The total size of the slice must be no larger than `isize::MAX` **bytes**
5286
- /// in memory. See the safety documentation of [`pointer::offset`].
5280
+ /// * The entire memory range of this slice must be contained within a single allocated object!
5281
+ /// Slices can never span across multiple allocated objects.
5282
+ /// * `data` must be non-null and aligned even for zero-length slices. One
5283
+ /// reason for this is that enum layout optimizations may rely on references
5284
+ /// (including slices of any length) being aligned and non-null to distinguish
5285
+ /// them from other data. You can obtain a pointer that is usable as `data`
5286
+ /// for zero-length slices using [`NonNull::dangling()`].
5287
+ ///
5288
+ /// * The memory referenced by the returned slice must not be mutated for the duration
5289
+ /// of lifetime `'a`, except inside an `UnsafeCell`.
5290
+ ///
5291
+ /// * The total size `len * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
5292
+ /// See the safety documentation of [`pointer::offset`].
5287
5293
///
5288
5294
/// # Caveat
5289
5295
///
@@ -5305,35 +5311,53 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
5305
5311
/// assert_eq!(slice[0], 42);
5306
5312
/// ```
5307
5313
///
5314
+ /// [valid]: ../../std/ptr/index.html#safety
5308
5315
/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
5309
5316
/// [`pointer::offset`]: ../../std/primitive.pointer.html#method.offset
5310
5317
#[ inline]
5311
5318
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
5312
5319
pub unsafe fn from_raw_parts < ' a , T > ( data : * const T , len : usize ) -> & ' a [ T ] {
5313
5320
debug_assert ! ( is_aligned_and_not_null( data) , "attempt to create unaligned or null slice" ) ;
5314
5321
debug_assert ! ( mem:: size_of:: <T >( ) . saturating_mul( len) <= isize :: MAX as usize ,
5315
- "attempt to create slice covering half the address space" ) ;
5322
+ "attempt to create slice covering at least half the address space" ) ;
5316
5323
& * ptr:: slice_from_raw_parts ( data, len)
5317
5324
}
5318
5325
5319
5326
/// Performs the same functionality as [`from_raw_parts`], except that a
5320
5327
/// mutable slice is returned.
5321
5328
///
5322
- /// This function is unsafe for the same reasons as [`from_raw_parts`], as well
5323
- /// as not being able to provide a non-aliasing guarantee of the returned
5324
- /// mutable slice. `data` must be non-null and aligned even for zero-length
5325
- /// slices as with [`from_raw_parts`]. The total size of the slice must be no
5326
- /// larger than `isize::MAX` **bytes** in memory.
5329
+ /// # Safety
5330
+ ///
5331
+ /// Behavior is undefined if any of the following conditions are violated:
5332
+ ///
5333
+ /// * `data` must be [valid] for writes for `len * mem::size_of::<T>()` many bytes,
5334
+ /// and it must be properly aligned. This means in particular:
5327
5335
///
5328
- /// See the documentation of [`from_raw_parts`] for more details.
5336
+ /// * The entire memory range of this slice must be contained within a single allocated object!
5337
+ /// Slices can never span across multiple allocated objects.
5338
+ /// * `data` must be non-null and aligned even for zero-length slices. One
5339
+ /// reason for this is that enum layout optimizations may rely on references
5340
+ /// (including slices of any length) being aligned and non-null to distinguish
5341
+ /// them from other data. You can obtain a pointer that is usable as `data`
5342
+ /// for zero-length slices using [`NonNull::dangling()`].
5329
5343
///
5344
+ /// * The memory referenced by the returned slice must not be accessed through any other pointer
5345
+ /// (not derived from the return value) for the duration of lifetime `'a`.
5346
+ /// Both read and write accesses are forbidden.
5347
+ ///
5348
+ /// * The total size `len * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
5349
+ /// See the safety documentation of [`pointer::offset`].
5350
+ ///
5351
+ /// [valid]: ../../std/ptr/index.html#safety
5352
+ /// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
5353
+ /// [`pointer::offset`]: ../../std/primitive.pointer.html#method.offset
5330
5354
/// [`from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
5331
5355
#[ inline]
5332
5356
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
5333
5357
pub unsafe fn from_raw_parts_mut < ' a , T > ( data : * mut T , len : usize ) -> & ' a mut [ T ] {
5334
5358
debug_assert ! ( is_aligned_and_not_null( data) , "attempt to create unaligned or null slice" ) ;
5335
5359
debug_assert ! ( mem:: size_of:: <T >( ) . saturating_mul( len) <= isize :: MAX as usize ,
5336
- "attempt to create slice covering half the address space" ) ;
5360
+ "attempt to create slice covering at least half the address space" ) ;
5337
5361
& mut * ptr:: slice_from_raw_parts_mut ( data, len)
5338
5362
}
5339
5363
0 commit comments