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