Skip to content

Commit 5b30161

Browse files
committed
use array::from_ref for slices
1 parent ed97b42 commit 5b30161

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

library/core/src/slice/raw.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Free functions to create `&[T]` and `&mut [T]`.
22
3+
use crate::array;
34
use crate::intrinsics::is_aligned_and_not_null;
45
use crate::mem;
56
use crate::ptr;
@@ -140,19 +141,11 @@ pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T]
140141
/// Converts a reference to T into a slice of length 1 (without copying).
141142
#[stable(feature = "from_ref", since = "1.28.0")]
142143
pub fn from_ref<T>(s: &T) -> &[T] {
143-
// SAFETY: a reference is guaranteed to be valid for reads. The returned
144-
// reference cannot be mutated as it is an immutable reference.
145-
// `mem::size_of::<T>()` cannot be larger than `isize::MAX`.
146-
// Thus the call to `from_raw_parts` is safe.
147-
unsafe { from_raw_parts(s, 1) }
144+
array::from_ref(s)
148145
}
149146

150147
/// Converts a reference to T into a slice of length 1 (without copying).
151148
#[stable(feature = "from_ref", since = "1.28.0")]
152149
pub fn from_mut<T>(s: &mut T) -> &mut [T] {
153-
// SAFETY: a mutable reference is guaranteed to be valid for writes.
154-
// The reference cannot be accessed by another pointer as it is an mutable reference.
155-
// `mem::size_of::<T>()` cannot be larger than `isize::MAX`.
156-
// Thus the call to `from_raw_parts_mut` is safe.
157-
unsafe { from_raw_parts_mut(s, 1) }
150+
array::from_mut(s)
158151
}

0 commit comments

Comments
 (0)