Skip to content

Add core::ptr::slice_len #68169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/libcore/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
unsafe { Repr { raw: FatPtr { data, len } }.rust_mut }
}

/// Get the length of a raw pointer to a slice without manifesting a reference.
///
/// # Examples
///
/// ```rust
/// #![feature(raw_slice_len)]
/// use std::ptr;
///
/// let slice_ptr = 1_usize as *const [(); 64] as *const [()] as *const [u64];
///
/// // This is unsound, as slice_ptr is not a pointer to a valid slice:
/// // assert_eq!((&*slice_ptr).len(), 64);
///
/// // This is sound, as it does not create a reference:
/// assert_eq!(ptr::slice_len(slice_ptr), 64);
/// ```
#[inline]
#[unstable(feature = "raw_slice_len", reason = "recently added", issue = "none")]
#[rustc_const_unstable(feature = "const_raw_slice_len", issue = "none")]
pub const fn slice_len<T>(data: *const [T]) -> usize {
unsafe { Repr { rust: data }.raw.len }
}

/// Swaps the values at two mutable locations of the same type, without
/// deinitializing either.
///
Expand Down