Skip to content

Commit 2ad9875

Browse files
committed
Add core::ptr::slice_len
1 parent 72b2bd5 commit 2ad9875

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/libcore/ptr/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,29 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
281281
unsafe { Repr { raw: FatPtr { data, len } }.rust_mut }
282282
}
283283

284+
/// Get the length of a raw pointer to a slice without manifesting a reference.
285+
///
286+
/// # Examples
287+
///
288+
/// ```rust
289+
/// #![feature(raw_slice_len)]
290+
/// use std::ptr;
291+
///
292+
/// let slice_ptr = 1_usize as *const [(); 64] as *const [()] as *const [u64];
293+
///
294+
/// // This is unsound, as slice_ptr is not a pointer to a valid slice:
295+
/// // assert_eq!((&*slice_ptr).len(), 64);
296+
///
297+
/// // This is sound, as it does not create a reference:
298+
/// assert_eq!(ptr::slice_len(slice_ptr), 64);
299+
/// ```
300+
#[inline]
301+
#[unstable(feature = "raw_slice_len", reason = "recently added", issue = "none")]
302+
#[rustc_const_unstable(feature = "const_raw_slice_len", issue = "none")]
303+
pub const fn slice_len<T>(data: *const [T]) -> usize {
304+
unsafe { Repr { rust: data }.raw.len }
305+
}
306+
284307
/// Swaps the values at two mutable locations of the same type, without
285308
/// deinitializing either.
286309
///

0 commit comments

Comments
 (0)