Skip to content

Commit 5eee5e5

Browse files
committed
clarify validity requirement of end pointer in slice::from_ptr_range
1 parent 5255902 commit 5eee5e5

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

library/core/src/slice/raw.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Free functions to create `&[T]` and `&mut [T]`.
22
33
use crate::array;
4-
use crate::intrinsics::is_aligned_and_not_null;
5-
use crate::mem;
64
use crate::ops::Range;
75
use crate::ptr;
86

@@ -195,7 +193,8 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
195193
/// to the first element of a slice.
196194
///
197195
/// * The `end` pointer must be a [valid] and properly aligned pointer to *one past*
198-
/// the last element.
196+
/// the last element, such that the offset from the end to the start pointer is
197+
/// the length of the slice.
199198
///
200199
/// * The range must contain `N` consecutive properly initialized values of type `T`:
201200
///
@@ -251,7 +250,8 @@ pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
251250
/// to the first element of a slice.
252251
///
253252
/// * The `end` pointer must be a [valid] and properly aligned pointer to *one past*
254-
/// the last element.
253+
/// the last element, such that the offset from the end to the start pointer is
254+
/// the length of the slice.
255255
///
256256
/// * The range must contain `N` consecutive properly initialized values of type `T`:
257257
///
@@ -274,7 +274,7 @@ pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
274274
///
275275
/// use core::slice;
276276
///
277-
/// let x = [1, 2, 3];
277+
/// let mut x = [1, 2, 3];
278278
/// let range = x.as_mut_ptr_range();
279279
///
280280
/// unsafe {

library/core/tests/slice.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,7 @@ fn slice_split_array_mut_out_of_bounds() {
22342234
v.split_array_mut::<7>();
22352235
}
22362236

2237+
#[test]
22372238
fn test_slice_from_ptr_range() {
22382239
let arr = ["foo".to_owned(), "bar".to_owned()];
22392240
let range = arr.as_ptr_range();

0 commit comments

Comments
 (0)