pub unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T]Notable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
🔬This is a nightly-only experimental API. (slice_from_ptr_range #89792)
Expand description

Performs the same functionality as from_ptr_range, except that a mutable slice is returned.

Safety

Behavior is undefined if any of the following conditions are violated:

  • The start pointer of the range must be a valid and properly aligned pointer to the first element of a slice.

  • The end pointer must be a valid and properly aligned pointer to one past the last element, such that the offset from the end to the start pointer is the length of the slice.

  • The range must contain N consecutive properly initialized values of type T:

    • The entire memory range of this slice must be contained within a single allocated object! Slices can never span across multiple allocated objects.
  • 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 length of the range must be no larger than isize::MAX. See the safety documentation of pointer::offset.

Note that a range created from slice::as_mut_ptr_range fulfills these requirements.

Examples

#![feature(slice_from_ptr_range)]

use core::slice;

let mut x = [1, 2, 3];
let range = x.as_mut_ptr_range();

unsafe {
    assert_eq!(slice::from_mut_ptr_range(range), &mut [1, 2, 3]);
}
Run