expand slice from_raw_part docs
This commit is contained in:
parent
82dc3aa5fb
commit
1a254e4f43
2 changed files with 46 additions and 18 deletions
|
@ -18,6 +18,10 @@
|
|||
//! * A [null] pointer is *never* valid, not even for accesses of [size zero][zst].
|
||||
//! * All pointers (except for the null pointer) are valid for all operations of
|
||||
//! [size zero][zst].
|
||||
//! * For a pointer to be valid, it is necessary (but not always sufficient) that the pointer
|
||||
//! be *dereferencable*: the memory range of the given size starting at the pointer must all be
|
||||
//! within the bounds of a single allocated object. Note that in Rust,
|
||||
//! every (stack-allocated) variable is considered a separate allocated object.
|
||||
//! * All accesses performed by functions in this module are *non-atomic* in the sense
|
||||
//! of [atomic operations] used to synchronize between threads. This means it is
|
||||
//! undefined behavior to perform two concurrent accesses to the same location from different
|
||||
|
|
|
@ -5272,18 +5272,24 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
|
|||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function is unsafe as there is no guarantee that the given pointer is
|
||||
/// valid for `len` elements, nor whether the lifetime inferred is a suitable
|
||||
/// lifetime for the returned slice.
|
||||
/// Behavior is undefined if any of the following conditions are violated:
|
||||
///
|
||||
/// `data` must be non-null and aligned, even for zero-length slices. One
|
||||
/// * `data` must be [valid] for reads for `len * mem::size_of::<T>()` many bytes,
|
||||
/// and it must be properly aligned. This means in particular:
|
||||
///
|
||||
/// * The entire memory range of this slice must be contained within a single allocated object!
|
||||
/// Slices can never span across multiple allocated objects.
|
||||
/// * `data` must be non-null and aligned even for zero-length slices. One
|
||||
/// reason for this is that enum layout optimizations may rely on references
|
||||
/// (including slices of any length) being aligned and non-null to distinguish
|
||||
/// them from other data. You can obtain a pointer that is usable as `data`
|
||||
/// for zero-length slices using [`NonNull::dangling()`].
|
||||
///
|
||||
/// The total size of the slice must be no larger than `isize::MAX` **bytes**
|
||||
/// in memory. See the safety documentation of [`pointer::offset`].
|
||||
/// * The memory referenced by the returned slice must not be mutated for the duration
|
||||
/// of lifetime `'a`, except inside an `UnsafeCell`.
|
||||
///
|
||||
/// * The total size `len * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
|
||||
/// See the safety documentation of [`pointer::offset`].
|
||||
///
|
||||
/// # Caveat
|
||||
///
|
||||
|
@ -5305,6 +5311,7 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
|
|||
/// assert_eq!(slice[0], 42);
|
||||
/// ```
|
||||
///
|
||||
/// [valid]: ../ptr/index.html#safety
|
||||
/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
|
||||
/// [`pointer::offset`]: ../../std/primitive.pointer.html#method.offset
|
||||
#[inline]
|
||||
|
@ -5312,28 +5319,45 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
|
|||
pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
|
||||
debug_assert!(is_aligned_and_not_null(data), "attempt to create unaligned or null slice");
|
||||
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
|
||||
"attempt to create slice covering half the address space");
|
||||
"attempt to create slice covering at least half the address space");
|
||||
&*ptr::slice_from_raw_parts(data, len)
|
||||
}
|
||||
|
||||
/// Performs the same functionality as [`from_raw_parts`], except that a
|
||||
/// mutable slice is returned.
|
||||
///
|
||||
/// This function is unsafe for the same reasons as [`from_raw_parts`], as well
|
||||
/// as not being able to provide a non-aliasing guarantee of the returned
|
||||
/// mutable slice. `data` must be non-null and aligned even for zero-length
|
||||
/// slices as with [`from_raw_parts`]. The total size of the slice must be no
|
||||
/// larger than `isize::MAX` **bytes** in memory.
|
||||
/// # Safety
|
||||
///
|
||||
/// See the documentation of [`from_raw_parts`] for more details.
|
||||
/// Behavior is undefined if any of the following conditions are violated:
|
||||
///
|
||||
/// * `data` must be [valid] for writes for `len * mem::size_of::<T>()` many bytes,
|
||||
/// and it must be properly aligned. This means in particular:
|
||||
///
|
||||
/// * The entire memory range of this slice must be contained within a single allocated object!
|
||||
/// Slices can never span across multiple allocated objects.
|
||||
/// * `data` must be non-null and aligned even for zero-length slices. One
|
||||
/// reason for this is that enum layout optimizations may rely on references
|
||||
/// (including slices of any length) being aligned and non-null to distinguish
|
||||
/// them from other data. You can obtain a pointer that is usable as `data`
|
||||
/// for zero-length slices using [`NonNull::dangling()`].
|
||||
///
|
||||
/// * The memory referenced by the returned slice must not be accessed through any other pointer
|
||||
/// (not derived from the return value) for the duration of lifetime `'a`.
|
||||
/// Both read and write accesses are forbidden.
|
||||
///
|
||||
/// * The total size `len * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
|
||||
/// See the safety documentation of [`pointer::offset`].
|
||||
///
|
||||
/// [valid]: ../ptr/index.html#safety
|
||||
/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
|
||||
/// [`pointer::offset`]: ../../std/primitive.pointer.html#method.offset
|
||||
/// [`from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
|
||||
#[inline]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
|
||||
debug_assert!(is_aligned_and_not_null(data), "attempt to create unaligned or null slice");
|
||||
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
|
||||
"attempt to create slice covering half the address space");
|
||||
"attempt to create slice covering at least half the address space");
|
||||
&mut *ptr::slice_from_raw_parts_mut(data, len)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue