2020-09-04 04:46:02 +00:00
|
|
|
//! Free functions to create `&[T]` and `&mut [T]`.
|
|
|
|
|
2020-09-23 19:33:45 +00:00
|
|
|
use crate::array;
|
2024-02-07 15:27:57 +00:00
|
|
|
use crate::mem::{align_of, size_of};
|
2021-10-11 21:41:25 +00:00
|
|
|
use crate::ops::Range;
|
2020-09-04 04:46:02 +00:00
|
|
|
use crate::ptr;
|
2024-03-17 09:12:25 +00:00
|
|
|
use crate::ub_checks;
|
2020-09-04 04:46:02 +00:00
|
|
|
|
|
|
|
/// Forms a slice from a pointer and a length.
|
|
|
|
///
|
|
|
|
/// The `len` argument is the number of **elements**, not the number of bytes.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Behavior is undefined if any of the following conditions are violated:
|
|
|
|
///
|
|
|
|
/// * `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. See [below](#incorrect-usage)
|
|
|
|
/// for an example incorrectly not taking this into account.
|
|
|
|
/// * `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()`].
|
|
|
|
///
|
|
|
|
/// * `data` must point to `len` consecutive properly initialized values of type `T`.
|
|
|
|
///
|
|
|
|
/// * The memory referenced by the returned slice must not be mutated for the duration
|
|
|
|
/// of lifetime `'a`, except inside an `UnsafeCell`.
|
|
|
|
///
|
2023-06-16 12:56:31 +00:00
|
|
|
/// * The total size `len * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`,
|
|
|
|
/// and adding that size to `data` must not "wrap around" the address space.
|
2020-09-04 04:46:02 +00:00
|
|
|
/// See the safety documentation of [`pointer::offset`].
|
|
|
|
///
|
|
|
|
/// # Caveat
|
|
|
|
///
|
|
|
|
/// The lifetime for the returned slice is inferred from its usage. To
|
|
|
|
/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
|
|
|
|
/// source lifetime is safe in the context, such as by providing a helper
|
|
|
|
/// function taking the lifetime of a host value for the slice, or by explicit
|
|
|
|
/// annotation.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::slice;
|
|
|
|
///
|
|
|
|
/// // manifest a slice for a single element
|
|
|
|
/// let x = 42;
|
|
|
|
/// let ptr = &x as *const _;
|
|
|
|
/// let slice = unsafe { slice::from_raw_parts(ptr, 1) };
|
|
|
|
/// assert_eq!(slice[0], 42);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Incorrect usage
|
|
|
|
///
|
|
|
|
/// The following `join_slices` function is **unsound** ⚠️
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// use std::slice;
|
|
|
|
///
|
|
|
|
/// fn join_slices<'a, T>(fst: &'a [T], snd: &'a [T]) -> &'a [T] {
|
|
|
|
/// let fst_end = fst.as_ptr().wrapping_add(fst.len());
|
|
|
|
/// let snd_start = snd.as_ptr();
|
|
|
|
/// assert_eq!(fst_end, snd_start, "Slices must be contiguous!");
|
|
|
|
/// unsafe {
|
|
|
|
/// // The assertion above ensures `fst` and `snd` are contiguous, but they might
|
|
|
|
/// // still be contained within _different allocated objects_, in which case
|
|
|
|
/// // creating this slice is undefined behavior.
|
|
|
|
/// slice::from_raw_parts(fst.as_ptr(), fst.len() + snd.len())
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// // `a` and `b` are different allocated objects...
|
|
|
|
/// let a = 42;
|
|
|
|
/// let b = 27;
|
|
|
|
/// // ... which may nevertheless be laid out contiguously in memory: | a | b |
|
|
|
|
/// let _ = join_slices(slice::from_ref(&a), slice::from_ref(&b)); // UB
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2024-04-02 16:54:24 +00:00
|
|
|
/// ### FFI: Handling null pointers
|
|
|
|
///
|
|
|
|
/// In languages such as C++, pointers to empty collections are not guaranteed to be non-null.
|
|
|
|
/// When accepting such pointers, they have to be checked for null-ness to avoid undefined
|
|
|
|
/// behavior.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::slice;
|
|
|
|
///
|
|
|
|
/// unsafe extern "C" fn handle_slice(ptr: *const f32, len: usize) {
|
|
|
|
/// let data = if ptr.is_null() {
|
|
|
|
/// // `len` is assumed to be 0.
|
|
|
|
/// &[]
|
|
|
|
/// } else {
|
|
|
|
/// unsafe { slice::from_raw_parts(ptr, len) }
|
|
|
|
/// };
|
|
|
|
/// dbg!(data);
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2020-09-04 04:46:02 +00:00
|
|
|
/// [valid]: ptr#safety
|
|
|
|
/// [`NonNull::dangling()`]: ptr::NonNull::dangling
|
|
|
|
#[inline]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-05-29 16:01:26 +00:00
|
|
|
#[rustc_const_stable(feature = "const_slice_from_raw_parts", since = "1.64.0")]
|
2022-01-13 13:57:57 +00:00
|
|
|
#[must_use]
|
2023-09-27 03:56:38 +00:00
|
|
|
#[rustc_diagnostic_item = "slice_from_raw_parts"]
|
2021-10-28 14:15:25 +00:00
|
|
|
pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
|
2020-09-04 04:46:02 +00:00
|
|
|
// SAFETY: the caller must uphold the safety contract for `from_raw_parts`.
|
2022-01-09 04:55:09 +00:00
|
|
|
unsafe {
|
2024-03-17 09:12:25 +00:00
|
|
|
ub_checks::assert_unsafe_precondition!(
|
2024-02-27 02:25:27 +00:00
|
|
|
check_language_ub,
|
2022-10-14 03:01:58 +00:00
|
|
|
"slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`",
|
2024-02-07 15:27:57 +00:00
|
|
|
(
|
|
|
|
data: *mut () = data as *mut (),
|
|
|
|
size: usize = size_of::<T>(),
|
|
|
|
align: usize = align_of::<T>(),
|
|
|
|
len: usize = len,
|
|
|
|
) =>
|
2024-03-17 09:12:25 +00:00
|
|
|
ub_checks::is_aligned_and_not_null(data, align)
|
|
|
|
&& ub_checks::is_valid_allocation_size(size, len)
|
2022-01-09 04:55:09 +00:00
|
|
|
);
|
|
|
|
&*ptr::slice_from_raw_parts(data, len)
|
|
|
|
}
|
2020-09-04 04:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs the same functionality as [`from_raw_parts`], except that a
|
|
|
|
/// mutable slice is returned.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Behavior is undefined if any of the following conditions are violated:
|
|
|
|
///
|
2021-01-02 18:45:11 +00:00
|
|
|
/// * `data` must be [valid] for both reads and writes for `len * mem::size_of::<T>()` many bytes,
|
2020-09-04 04:46:02 +00:00
|
|
|
/// 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()`].
|
|
|
|
///
|
|
|
|
/// * `data` must point to `len` consecutive properly initialized values of type `T`.
|
|
|
|
///
|
|
|
|
/// * 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.
|
|
|
|
///
|
2023-06-16 12:56:31 +00:00
|
|
|
/// * The total size `len * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`,
|
|
|
|
/// and adding that size to `data` must not "wrap around" the address space.
|
2020-09-04 04:46:02 +00:00
|
|
|
/// See the safety documentation of [`pointer::offset`].
|
|
|
|
///
|
|
|
|
/// [valid]: ptr#safety
|
|
|
|
/// [`NonNull::dangling()`]: ptr::NonNull::dangling
|
|
|
|
#[inline]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-05-29 16:01:26 +00:00
|
|
|
#[rustc_const_unstable(feature = "const_slice_from_raw_parts_mut", issue = "67456")]
|
2022-01-13 13:57:57 +00:00
|
|
|
#[must_use]
|
2023-09-27 03:56:38 +00:00
|
|
|
#[rustc_diagnostic_item = "slice_from_raw_parts_mut"]
|
2021-10-28 14:15:25 +00:00
|
|
|
pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
|
2020-09-04 04:46:02 +00:00
|
|
|
// SAFETY: the caller must uphold the safety contract for `from_raw_parts_mut`.
|
2021-10-28 14:15:25 +00:00
|
|
|
unsafe {
|
2024-03-17 09:12:25 +00:00
|
|
|
ub_checks::assert_unsafe_precondition!(
|
2024-02-27 02:25:27 +00:00
|
|
|
check_language_ub,
|
2022-10-14 03:01:58 +00:00
|
|
|
"slice::from_raw_parts_mut requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`",
|
2024-02-07 15:27:57 +00:00
|
|
|
(
|
|
|
|
data: *mut () = data as *mut (),
|
|
|
|
size: usize = size_of::<T>(),
|
|
|
|
align: usize = align_of::<T>(),
|
|
|
|
len: usize = len,
|
|
|
|
) =>
|
2024-03-17 09:12:25 +00:00
|
|
|
ub_checks::is_aligned_and_not_null(data, align)
|
|
|
|
&& ub_checks::is_valid_allocation_size(size, len)
|
2022-01-09 04:55:09 +00:00
|
|
|
);
|
|
|
|
&mut *ptr::slice_from_raw_parts_mut(data, len)
|
2021-10-28 14:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 04:46:02 +00:00
|
|
|
/// Converts a reference to T into a slice of length 1 (without copying).
|
|
|
|
#[stable(feature = "from_ref", since = "1.28.0")]
|
2022-05-24 18:33:31 +00:00
|
|
|
#[rustc_const_stable(feature = "const_slice_from_ref_shared", since = "1.63.0")]
|
2022-01-13 13:57:57 +00:00
|
|
|
#[must_use]
|
2021-10-22 08:15:56 +00:00
|
|
|
pub const fn from_ref<T>(s: &T) -> &[T] {
|
2020-09-23 19:33:45 +00:00
|
|
|
array::from_ref(s)
|
2020-09-04 04:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a reference to T into a slice of length 1 (without copying).
|
|
|
|
#[stable(feature = "from_ref", since = "1.28.0")]
|
2021-10-23 17:59:15 +00:00
|
|
|
#[rustc_const_unstable(feature = "const_slice_from_ref", issue = "90206")]
|
2022-01-13 13:57:57 +00:00
|
|
|
#[must_use]
|
2021-10-22 08:15:56 +00:00
|
|
|
pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
|
2020-09-23 19:33:45 +00:00
|
|
|
array::from_mut(s)
|
2020-09-04 04:46:02 +00:00
|
|
|
}
|
2021-10-11 21:41:25 +00:00
|
|
|
|
|
|
|
/// Forms a slice from a pointer range.
|
|
|
|
///
|
|
|
|
/// This function is useful for interacting with foreign interfaces which
|
|
|
|
/// use two pointers to refer to a range of elements in memory, as is
|
|
|
|
/// common in C++.
|
|
|
|
///
|
|
|
|
/// # 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.
|
|
|
|
///
|
2023-06-16 12:56:31 +00:00
|
|
|
/// * The entire memory range of this slice must be contained within a single allocated object!
|
|
|
|
/// Slices can never span across multiple allocated objects.
|
2021-10-11 21:41:25 +00:00
|
|
|
///
|
2023-06-16 12:56:31 +00:00
|
|
|
/// * The range must contain `N` consecutive properly initialized values of type `T`.
|
2021-10-11 21:41:25 +00:00
|
|
|
///
|
|
|
|
/// * The memory referenced by the returned slice must not be mutated for the duration
|
|
|
|
/// of lifetime `'a`, except inside an `UnsafeCell`.
|
|
|
|
///
|
2023-06-16 12:56:31 +00:00
|
|
|
/// * The total length of the range must be no larger than `isize::MAX`,
|
|
|
|
/// and adding that size to `data` must not "wrap around" the address space.
|
2021-10-11 21:41:25 +00:00
|
|
|
/// See the safety documentation of [`pointer::offset`].
|
|
|
|
///
|
|
|
|
/// Note that a range created from [`slice::as_ptr_range`] fulfills these requirements.
|
|
|
|
///
|
2022-10-03 00:41:54 +00:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function panics if `T` is a Zero-Sized Type (“ZST”).
|
|
|
|
///
|
2021-10-11 21:41:25 +00:00
|
|
|
/// # Caveat
|
|
|
|
///
|
|
|
|
/// The lifetime for the returned slice is inferred from its usage. To
|
|
|
|
/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
|
|
|
|
/// source lifetime is safe in the context, such as by providing a helper
|
|
|
|
/// function taking the lifetime of a host value for the slice, or by explicit
|
|
|
|
/// annotation.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(slice_from_ptr_range)]
|
|
|
|
///
|
|
|
|
/// use core::slice;
|
|
|
|
///
|
|
|
|
/// let x = [1, 2, 3];
|
|
|
|
/// let range = x.as_ptr_range();
|
|
|
|
///
|
|
|
|
/// unsafe {
|
|
|
|
/// assert_eq!(slice::from_ptr_range(range), &x);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [valid]: ptr#safety
|
|
|
|
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
|
2022-05-27 15:09:46 +00:00
|
|
|
#[rustc_const_unstable(feature = "const_slice_from_ptr_range", issue = "89792")]
|
2022-05-26 09:06:29 +00:00
|
|
|
pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
|
2021-10-11 21:41:25 +00:00
|
|
|
// SAFETY: the caller must uphold the safety contract for `from_ptr_range`.
|
2022-04-09 21:14:35 +00:00
|
|
|
unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
|
2021-10-11 21:41:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 00:44:50 +00:00
|
|
|
/// Forms a mutable slice from a pointer range.
|
|
|
|
///
|
|
|
|
/// This is the same functionality as [`from_ptr_range`], except that a
|
2021-10-11 21:41:25 +00:00
|
|
|
/// mutable slice is returned.
|
|
|
|
///
|
2022-10-03 00:44:50 +00:00
|
|
|
/// This function is useful for interacting with foreign interfaces which
|
|
|
|
/// use two pointers to refer to a range of elements in memory, as is
|
|
|
|
/// common in C++.
|
|
|
|
///
|
2021-10-11 21:41:25 +00:00
|
|
|
/// # 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.
|
|
|
|
///
|
2023-06-16 12:56:31 +00:00
|
|
|
/// * The entire memory range of this slice must be contained within a single allocated object!
|
|
|
|
/// Slices can never span across multiple allocated objects.
|
2021-10-11 21:41:25 +00:00
|
|
|
///
|
2023-06-16 12:56:31 +00:00
|
|
|
/// * The range must contain `N` consecutive properly initialized values of type `T`.
|
2021-10-11 21:41:25 +00:00
|
|
|
///
|
|
|
|
/// * 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.
|
|
|
|
///
|
2023-06-16 12:56:31 +00:00
|
|
|
/// * The total length of the range must be no larger than `isize::MAX`,
|
|
|
|
/// and adding that size to `data` must not "wrap around" the address space.
|
2021-10-11 21:41:25 +00:00
|
|
|
/// See the safety documentation of [`pointer::offset`].
|
|
|
|
///
|
|
|
|
/// Note that a range created from [`slice::as_mut_ptr_range`] fulfills these requirements.
|
|
|
|
///
|
2022-10-03 00:41:54 +00:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function panics if `T` is a Zero-Sized Type (“ZST”).
|
|
|
|
///
|
2022-10-03 00:44:50 +00:00
|
|
|
/// # Caveat
|
|
|
|
///
|
|
|
|
/// The lifetime for the returned slice is inferred from its usage. To
|
|
|
|
/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
|
|
|
|
/// source lifetime is safe in the context, such as by providing a helper
|
|
|
|
/// function taking the lifetime of a host value for the slice, or by explicit
|
|
|
|
/// annotation.
|
|
|
|
///
|
2021-10-11 21:41:25 +00:00
|
|
|
/// # 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]);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [valid]: ptr#safety
|
|
|
|
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
|
2022-05-31 19:12:28 +00:00
|
|
|
#[rustc_const_unstable(feature = "const_slice_from_mut_ptr_range", issue = "89792")]
|
2022-05-26 09:06:29 +00:00
|
|
|
pub const unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] {
|
2021-10-11 21:41:25 +00:00
|
|
|
// SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`.
|
2022-04-09 21:14:35 +00:00
|
|
|
unsafe { from_raw_parts_mut(range.start, range.end.sub_ptr(range.start)) }
|
2021-10-11 21:41:25 +00:00
|
|
|
}
|