@@ -5272,18 +5272,24 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
52725272///
52735273/// # Safety
52745274///
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:
52785276///
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:
52845279///
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`].
52875293///
52885294/// # Caveat
52895295///
@@ -5305,35 +5311,53 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
53055311/// assert_eq!(slice[0], 42);
53065312/// ```
53075313///
5314+ /// [valid]: ../../std/ptr/index.html#safety
53085315/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
53095316/// [`pointer::offset`]: ../../std/primitive.pointer.html#method.offset
53105317#[ inline]
53115318#[ stable( feature = "rust1" , since = "1.0.0" ) ]
53125319pub unsafe fn from_raw_parts < ' a , T > ( data : * const T , len : usize ) -> & ' a [ T ] {
53135320 debug_assert ! ( is_aligned_and_not_null( data) , "attempt to create unaligned or null slice" ) ;
53145321 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" ) ;
53165323 & * ptr:: slice_from_raw_parts ( data, len)
53175324}
53185325
53195326/// Performs the same functionality as [`from_raw_parts`], except that a
53205327/// mutable slice is returned.
53215328///
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:
53275335///
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()`].
53295343///
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
53305354/// [`from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
53315355#[ inline]
53325356#[ stable( feature = "rust1" , since = "1.0.0" ) ]
53335357pub unsafe fn from_raw_parts_mut < ' a , T > ( data : * mut T , len : usize ) -> & ' a mut [ T ] {
53345358 debug_assert ! ( is_aligned_and_not_null( data) , "attempt to create unaligned or null slice" ) ;
53355359 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" ) ;
53375361 & mut * ptr:: slice_from_raw_parts_mut ( data, len)
53385362}
53395363
0 commit comments