core/slice/raw.rs
1//! Free functions to create `&[T]` and `&mut [T]`.
2
3#[cfg(not(feature = "ferrocene_certified"))]
4use crate::ops::Range;
5use crate::{array, ptr, ub_checks};
6
7/// Forms a slice from a pointer and a length.
8///
9/// The `len` argument is the number of **elements**, not the number of bytes.
10///
11/// # Safety
12///
13/// Behavior is undefined if any of the following conditions are violated:
14///
15/// * `data` must be non-null, [valid] for reads for `len * size_of::<T>()` many bytes,
16/// and it must be properly aligned. This means in particular:
17///
18/// * The entire memory range of this slice must be contained within a single allocation!
19/// Slices can never span across multiple allocations. See [below](#incorrect-usage)
20/// for an example incorrectly not taking this into account.
21/// * `data` must be non-null and aligned even for zero-length slices or slices of ZSTs. One
22/// reason for this is that enum layout optimizations may rely on references
23/// (including slices of any length) being aligned and non-null to distinguish
24/// them from other data. You can obtain a pointer that is usable as `data`
25/// for zero-length slices using [`NonNull::dangling()`].
26///
27/// * `data` must point to `len` consecutive properly initialized values of type `T`.
28///
29/// * The memory referenced by the returned slice must not be mutated for the duration
30/// of lifetime `'a`, except inside an `UnsafeCell`.
31///
32/// * The total size `len * size_of::<T>()` of the slice must be no larger than `isize::MAX`,
33/// and adding that size to `data` must not "wrap around" the address space.
34/// See the safety documentation of [`pointer::offset`].
35///
36/// # Caveat
37///
38/// The lifetime for the returned slice is inferred from its usage. To
39/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
40/// source lifetime is safe in the context, such as by providing a helper
41/// function taking the lifetime of a host value for the slice, or by explicit
42/// annotation.
43///
44/// # Examples
45///
46/// ```
47/// use std::slice;
48///
49/// // manifest a slice for a single element
50/// let x = 42;
51/// let ptr = &x as *const _;
52/// let slice = unsafe { slice::from_raw_parts(ptr, 1) };
53/// assert_eq!(slice[0], 42);
54/// ```
55///
56/// ### Incorrect usage
57///
58/// The following `join_slices` function is **unsound** ⚠️
59///
60/// ```rust,no_run
61/// use std::slice;
62///
63/// fn join_slices<'a, T>(fst: &'a [T], snd: &'a [T]) -> &'a [T] {
64/// let fst_end = fst.as_ptr().wrapping_add(fst.len());
65/// let snd_start = snd.as_ptr();
66/// assert_eq!(fst_end, snd_start, "Slices must be contiguous!");
67/// unsafe {
68/// // The assertion above ensures `fst` and `snd` are contiguous, but they might
69/// // still be contained within _different allocations_, in which case
70/// // creating this slice is undefined behavior.
71/// slice::from_raw_parts(fst.as_ptr(), fst.len() + snd.len())
72/// }
73/// }
74///
75/// fn main() {
76/// // `a` and `b` are different allocations...
77/// let a = 42;
78/// let b = 27;
79/// // ... which may nevertheless be laid out contiguously in memory: | a | b |
80/// let _ = join_slices(slice::from_ref(&a), slice::from_ref(&b)); // UB
81/// }
82/// ```
83///
84/// ### FFI: Handling null pointers
85///
86/// In languages such as C++, pointers to empty collections are not guaranteed to be non-null.
87/// When accepting such pointers, they have to be checked for null-ness to avoid undefined
88/// behavior.
89///
90/// ```
91/// use std::slice;
92///
93/// /// Sum the elements of an FFI slice.
94/// ///
95/// /// # Safety
96/// ///
97/// /// If ptr is not NULL, it must be correctly aligned and
98/// /// point to `len` initialized items of type `f32`.
99/// unsafe extern "C" fn sum_slice(ptr: *const f32, len: usize) -> f32 {
100/// let data = if ptr.is_null() {
101/// // `len` is assumed to be 0.
102/// &[]
103/// } else {
104/// // SAFETY: see function docstring.
105/// unsafe { slice::from_raw_parts(ptr, len) }
106/// };
107/// data.into_iter().sum()
108/// }
109///
110/// // This could be the result of C++'s std::vector::data():
111/// let ptr = std::ptr::null();
112/// // And this could be std::vector::size():
113/// let len = 0;
114/// assert_eq!(unsafe { sum_slice(ptr, len) }, 0.0);
115/// ```
116///
117/// [valid]: ptr#safety
118/// [`NonNull::dangling()`]: ptr::NonNull::dangling
119#[inline]
120#[stable(feature = "rust1", since = "1.0.0")]
121#[rustc_const_stable(feature = "const_slice_from_raw_parts", since = "1.64.0")]
122#[must_use]
123#[rustc_diagnostic_item = "slice_from_raw_parts"]
124#[track_caller]
125pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
126 // SAFETY: the caller must uphold the safety contract for `from_raw_parts`.
127 unsafe {
128 ub_checks::assert_unsafe_precondition!(
129 check_language_ub,
130 "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`",
131 (
132 data: *mut () = data as *mut (),
133 size: usize = size_of::<T>(),
134 align: usize = align_of::<T>(),
135 len: usize = len,
136 ) =>
137 ub_checks::maybe_is_aligned_and_not_null(data, align, false)
138 && ub_checks::is_valid_allocation_size(size, len)
139 );
140 &*ptr::slice_from_raw_parts(data, len)
141 }
142}
143
144/// Performs the same functionality as [`from_raw_parts`], except that a
145/// mutable slice is returned.
146///
147/// # Safety
148///
149/// Behavior is undefined if any of the following conditions are violated:
150///
151/// * `data` must be non-null, [valid] for both reads and writes for `len * size_of::<T>()` many bytes,
152/// and it must be properly aligned. This means in particular:
153///
154/// * The entire memory range of this slice must be contained within a single allocation!
155/// Slices can never span across multiple allocations.
156/// * `data` must be non-null and aligned even for zero-length slices or slices of ZSTs. One
157/// reason for this is that enum layout optimizations may rely on references
158/// (including slices of any length) being aligned and non-null to distinguish
159/// them from other data. You can obtain a pointer that is usable as `data`
160/// for zero-length slices using [`NonNull::dangling()`].
161///
162/// * `data` must point to `len` consecutive properly initialized values of type `T`.
163///
164/// * The memory referenced by the returned slice must not be accessed through any other pointer
165/// (not derived from the return value) for the duration of lifetime `'a`.
166/// Both read and write accesses are forbidden.
167///
168/// * The total size `len * size_of::<T>()` of the slice must be no larger than `isize::MAX`,
169/// and adding that size to `data` must not "wrap around" the address space.
170/// See the safety documentation of [`pointer::offset`].
171///
172/// [valid]: ptr#safety
173/// [`NonNull::dangling()`]: ptr::NonNull::dangling
174#[inline]
175#[stable(feature = "rust1", since = "1.0.0")]
176#[rustc_const_stable(feature = "const_slice_from_raw_parts_mut", since = "1.83.0")]
177#[must_use]
178#[rustc_diagnostic_item = "slice_from_raw_parts_mut"]
179#[track_caller]
180pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
181 // SAFETY: the caller must uphold the safety contract for `from_raw_parts_mut`.
182 unsafe {
183 ub_checks::assert_unsafe_precondition!(
184 check_language_ub,
185 "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`",
186 (
187 data: *mut () = data as *mut (),
188 size: usize = size_of::<T>(),
189 align: usize = align_of::<T>(),
190 len: usize = len,
191 ) =>
192 ub_checks::maybe_is_aligned_and_not_null(data, align, false)
193 && ub_checks::is_valid_allocation_size(size, len)
194 );
195 &mut *ptr::slice_from_raw_parts_mut(data, len)
196 }
197}
198
199/// Converts a reference to T into a slice of length 1 (without copying).
200#[stable(feature = "from_ref", since = "1.28.0")]
201#[rustc_const_stable(feature = "const_slice_from_ref_shared", since = "1.63.0")]
202#[rustc_diagnostic_item = "slice_from_ref"]
203#[must_use]
204pub const fn from_ref<T>(s: &T) -> &[T] {
205 array::from_ref(s)
206}
207
208/// Converts a reference to T into a slice of length 1 (without copying).
209#[stable(feature = "from_ref", since = "1.28.0")]
210#[rustc_const_stable(feature = "const_slice_from_ref", since = "1.83.0")]
211#[must_use]
212pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
213 array::from_mut(s)
214}
215
216/// Forms a slice from a pointer range.
217///
218/// This function is useful for interacting with foreign interfaces which
219/// use two pointers to refer to a range of elements in memory, as is
220/// common in C++.
221///
222/// # Safety
223///
224/// Behavior is undefined if any of the following conditions are violated:
225///
226/// * The `start` pointer of the range must be a non-null, [valid] and properly aligned pointer
227/// to the first element of a slice.
228///
229/// * The `end` pointer must be a [valid] and properly aligned pointer to *one past*
230/// the last element, such that the offset from the end to the start pointer is
231/// the length of the slice.
232///
233/// * The entire memory range of this slice must be contained within a single allocation!
234/// Slices can never span across multiple allocations.
235///
236/// * The range must contain `N` consecutive properly initialized values of type `T`.
237///
238/// * The memory referenced by the returned slice must not be mutated for the duration
239/// of lifetime `'a`, except inside an `UnsafeCell`.
240///
241/// * The total length of the range must be no larger than `isize::MAX`,
242/// and adding that size to `start` must not "wrap around" the address space.
243/// See the safety documentation of [`pointer::offset`].
244///
245/// Note that a range created from [`slice::as_ptr_range`] fulfills these requirements.
246///
247/// # Panics
248///
249/// This function panics if `T` is a Zero-Sized Type (“ZST”).
250///
251/// # Caveat
252///
253/// The lifetime for the returned slice is inferred from its usage. To
254/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
255/// source lifetime is safe in the context, such as by providing a helper
256/// function taking the lifetime of a host value for the slice, or by explicit
257/// annotation.
258///
259/// # Examples
260///
261/// ```
262/// #![feature(slice_from_ptr_range)]
263///
264/// use core::slice;
265///
266/// let x = [1, 2, 3];
267/// let range = x.as_ptr_range();
268///
269/// unsafe {
270/// assert_eq!(slice::from_ptr_range(range), &x);
271/// }
272/// ```
273///
274/// [valid]: ptr#safety
275#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
276#[rustc_const_unstable(feature = "const_slice_from_ptr_range", issue = "89792")]
277#[track_caller]
278#[cfg(not(feature = "ferrocene_certified"))]
279pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
280 // SAFETY: the caller must uphold the safety contract for `from_ptr_range`.
281 unsafe { from_raw_parts(range.start, range.end.offset_from_unsigned(range.start)) }
282}
283
284/// Forms a mutable slice from a pointer range.
285///
286/// This is the same functionality as [`from_ptr_range`], except that a
287/// mutable slice is returned.
288///
289/// This function is useful for interacting with foreign interfaces which
290/// use two pointers to refer to a range of elements in memory, as is
291/// common in C++.
292///
293/// # Safety
294///
295/// Behavior is undefined if any of the following conditions are violated:
296///
297/// * The `start` pointer of the range must be a non-null, [valid] and properly aligned pointer
298/// to the first element of a slice.
299///
300/// * The `end` pointer must be a [valid] and properly aligned pointer to *one past*
301/// the last element, such that the offset from the end to the start pointer is
302/// the length of the slice.
303///
304/// * The entire memory range of this slice must be contained within a single allocation!
305/// Slices can never span across multiple allocations.
306///
307/// * The range must contain `N` consecutive properly initialized values of type `T`.
308///
309/// * The memory referenced by the returned slice must not be accessed through any other pointer
310/// (not derived from the return value) for the duration of lifetime `'a`.
311/// Both read and write accesses are forbidden.
312///
313/// * The total length of the range must be no larger than `isize::MAX`,
314/// and adding that size to `start` must not "wrap around" the address space.
315/// See the safety documentation of [`pointer::offset`].
316///
317/// Note that a range created from [`slice::as_mut_ptr_range`] fulfills these requirements.
318///
319/// # Panics
320///
321/// This function panics if `T` is a Zero-Sized Type (“ZST”).
322///
323/// # Caveat
324///
325/// The lifetime for the returned slice is inferred from its usage. To
326/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
327/// source lifetime is safe in the context, such as by providing a helper
328/// function taking the lifetime of a host value for the slice, or by explicit
329/// annotation.
330///
331/// # Examples
332///
333/// ```
334/// #![feature(slice_from_ptr_range)]
335///
336/// use core::slice;
337///
338/// let mut x = [1, 2, 3];
339/// let range = x.as_mut_ptr_range();
340///
341/// unsafe {
342/// assert_eq!(slice::from_mut_ptr_range(range), &mut [1, 2, 3]);
343/// }
344/// ```
345///
346/// [valid]: ptr#safety
347#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
348#[rustc_const_unstable(feature = "const_slice_from_mut_ptr_range", issue = "89792")]
349#[cfg(not(feature = "ferrocene_certified"))]
350pub const unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] {
351 // SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`.
352 unsafe { from_raw_parts_mut(range.start, range.end.offset_from_unsigned(range.start)) }
353}