Skip to content

Commit c8aefa6

Browse files
committed
add slice::{from_ptr_range, from_mut_ptr_range}
1 parent e0f81e5 commit c8aefa6

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

core/src/slice/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ pub use raw::{from_raw_parts, from_raw_parts_mut};
7171
#[stable(feature = "from_ref", since = "1.28.0")]
7272
pub use raw::{from_mut, from_ref};
7373

74+
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
75+
pub use raw::{from_mut_ptr_range, from_ptr_range};
76+
7477
// This function is public only because there is no other way to unit test heapsort.
7578
#[unstable(feature = "sort_internals", reason = "internal to sort module", issue = "none")]
7679
pub use sort::heapsort;

core/src/slice/raw.rs

+111
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Free functions to create `&[T]` and `&mut [T]`.
22
33
use crate::array;
4+
use crate::ops::Range;
45
use crate::ptr;
56

67
/// Forms a slice from a pointer and a length.
@@ -177,3 +178,113 @@ pub const fn from_ref<T>(s: &T) -> &[T] {
177178
pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
178179
array::from_mut(s)
179180
}
181+
182+
/// Forms a slice from a pointer range.
183+
///
184+
/// This function is useful for interacting with foreign interfaces which
185+
/// use two pointers to refer to a range of elements in memory, as is
186+
/// common in C++.
187+
///
188+
/// # Safety
189+
///
190+
/// Behavior is undefined if any of the following conditions are violated:
191+
///
192+
/// * The `start` pointer of the range must be a [valid] and properly aligned pointer
193+
/// to the first element of a slice.
194+
///
195+
/// * The `end` pointer must be a [valid] and properly aligned pointer to *one past*
196+
/// the last element, such that the offset from the end to the start pointer is
197+
/// the length of the slice.
198+
///
199+
/// * The range must contain `N` consecutive properly initialized values of type `T`:
200+
///
201+
/// * The entire memory range of this slice must be contained within a single allocated object!
202+
/// Slices can never span across multiple allocated objects.
203+
///
204+
/// * The memory referenced by the returned slice must not be mutated for the duration
205+
/// of lifetime `'a`, except inside an `UnsafeCell`.
206+
///
207+
/// * The total length of the range must be no larger than `isize::MAX`.
208+
/// See the safety documentation of [`pointer::offset`].
209+
///
210+
/// Note that a range created from [`slice::as_ptr_range`] fulfills these requirements.
211+
///
212+
/// # Caveat
213+
///
214+
/// The lifetime for the returned slice is inferred from its usage. To
215+
/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
216+
/// source lifetime is safe in the context, such as by providing a helper
217+
/// function taking the lifetime of a host value for the slice, or by explicit
218+
/// annotation.
219+
///
220+
/// # Examples
221+
///
222+
/// ```
223+
/// #![feature(slice_from_ptr_range)]
224+
///
225+
/// use core::slice;
226+
///
227+
/// let x = [1, 2, 3];
228+
/// let range = x.as_ptr_range();
229+
///
230+
/// unsafe {
231+
/// assert_eq!(slice::from_ptr_range(range), &x);
232+
/// }
233+
/// ```
234+
///
235+
/// [valid]: ptr#safety
236+
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
237+
pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
238+
// SAFETY: the caller must uphold the safety contract for `from_ptr_range`.
239+
unsafe { from_raw_parts(range.start, range.end.offset_from(range.start) as usize) }
240+
}
241+
242+
/// Performs the same functionality as [`from_ptr_range`], except that a
243+
/// mutable slice is returned.
244+
///
245+
/// # Safety
246+
///
247+
/// Behavior is undefined if any of the following conditions are violated:
248+
///
249+
/// * The `start` pointer of the range must be a [valid] and properly aligned pointer
250+
/// to the first element of a slice.
251+
///
252+
/// * The `end` pointer must be a [valid] and properly aligned pointer to *one past*
253+
/// the last element, such that the offset from the end to the start pointer is
254+
/// the length of the slice.
255+
///
256+
/// * The range must contain `N` consecutive properly initialized values of type `T`:
257+
///
258+
/// * The entire memory range of this slice must be contained within a single allocated object!
259+
/// Slices can never span across multiple allocated objects.
260+
///
261+
/// * The memory referenced by the returned slice must not be accessed through any other pointer
262+
/// (not derived from the return value) for the duration of lifetime `'a`.
263+
/// Both read and write accesses are forbidden.
264+
///
265+
/// * The total length of the range must be no larger than `isize::MAX`.
266+
/// See the safety documentation of [`pointer::offset`].
267+
///
268+
/// Note that a range created from [`slice::as_mut_ptr_range`] fulfills these requirements.
269+
///
270+
/// # Examples
271+
///
272+
/// ```
273+
/// #![feature(slice_from_ptr_range)]
274+
///
275+
/// use core::slice;
276+
///
277+
/// let mut x = [1, 2, 3];
278+
/// let range = x.as_mut_ptr_range();
279+
///
280+
/// unsafe {
281+
/// assert_eq!(slice::from_mut_ptr_range(range), &mut [1, 2, 3]);
282+
/// }
283+
/// ```
284+
///
285+
/// [valid]: ptr#safety
286+
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
287+
pub unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] {
288+
// SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`.
289+
unsafe { from_raw_parts_mut(range.start, range.end.offset_from(range.start) as usize) }
290+
}

core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#![feature(pin_macro)]
4949
#![feature(sort_internals)]
5050
#![feature(slice_take)]
51+
#![feature(slice_from_ptr_range)]
5152
#![feature(maybe_uninit_uninit_array)]
5253
#![feature(maybe_uninit_array_assume_init)]
5354
#![feature(maybe_uninit_write_slice)]

core/tests/slice.rs

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::cell::Cell;
22
use core::cmp::Ordering;
33
use core::mem::MaybeUninit;
44
use core::result::Result::{Err, Ok};
5+
use core::slice;
56

67
#[test]
78
fn test_position() {
@@ -2480,3 +2481,24 @@ take_tests! {
24802481
(take_mut_oob_max_range_to_inclusive, (..=usize::MAX), None, empty_max_mut!()),
24812482
(take_mut_in_bounds_max_range_from, (usize::MAX..), Some(&mut [] as _), empty_max_mut!()),
24822483
}
2484+
2485+
#[test]
2486+
fn test_slice_from_ptr_range() {
2487+
let arr = ["foo".to_owned(), "bar".to_owned()];
2488+
let range = arr.as_ptr_range();
2489+
unsafe {
2490+
assert_eq!(slice::from_ptr_range(range), &arr);
2491+
}
2492+
2493+
let mut arr = [1, 2, 3];
2494+
let range = arr.as_mut_ptr_range();
2495+
unsafe {
2496+
assert_eq!(slice::from_mut_ptr_range(range), &mut [1, 2, 3]);
2497+
}
2498+
2499+
let arr: [Vec<String>; 0] = [];
2500+
let range = arr.as_ptr_range();
2501+
unsafe {
2502+
assert_eq!(slice::from_ptr_range(range), &arr);
2503+
}
2504+
}

0 commit comments

Comments
 (0)