diff options
Diffstat (limited to 'src/libcore/ptr')
| -rw-r--r-- | src/libcore/ptr/const_ptr.rs | 26 | ||||
| -rw-r--r-- | src/libcore/ptr/mut_ptr.rs | 26 |
2 files changed, 50 insertions, 2 deletions
diff --git a/src/libcore/ptr/const_ptr.rs b/src/libcore/ptr/const_ptr.rs index 00bd985114a..33c9e0b5315 100644 --- a/src/libcore/ptr/const_ptr.rs +++ b/src/libcore/ptr/const_ptr.rs @@ -708,7 +708,31 @@ impl<T: ?Sized> *const T { #[cfg(not(bootstrap))] #[lang = "const_slice_ptr"] -impl<T> *const [T] {} +impl<T> *const [T] { + /// Returns the length of a raw slice. + /// + /// The returned value is the number of **elements**, not the number of bytes. + /// + /// This function is safe, even when the raw slice cannot be cast to a slice + /// reference because the pointer is null or unaligned. + /// + /// # Examples + /// + /// ```rust + /// #![feature(slice_ptr_len)] + /// + /// use std::ptr; + /// + /// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3); + /// assert_eq!(slice.len(), 3); + /// ``` + #[inline] + #[unstable(feature = "slice_ptr_len", issue = "none")] + #[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "none")] + pub const fn len(self) -> usize { + unsafe { Repr { rust: self }.raw }.len + } +} // Equality for pointers #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/ptr/mut_ptr.rs b/src/libcore/ptr/mut_ptr.rs index 00084456839..89475973675 100644 --- a/src/libcore/ptr/mut_ptr.rs +++ b/src/libcore/ptr/mut_ptr.rs @@ -896,7 +896,31 @@ impl<T: ?Sized> *mut T { #[cfg(not(bootstrap))] #[lang = "mut_slice_ptr"] -impl<T> *mut [T] {} +impl<T> *mut [T] { + /// Returns the length of a raw slice. + /// + /// The returned value is the number of **elements**, not the number of bytes. + /// + /// This function is safe, even when the raw slice cannot be cast to a slice + /// reference because the pointer is null or unaligned. + /// + /// # Examples + /// + /// ```rust + /// #![feature(slice_ptr_len)] + /// + /// use std::ptr; + /// + /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3); + /// assert_eq!(slice.len(), 3); + /// ``` + #[inline] + #[unstable(feature = "slice_ptr_len", issue = "none")] + #[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "none")] + pub const fn len(self) -> usize { + unsafe { Repr { rust_mut: self }.raw }.len + } +} // Equality for pointers #[stable(feature = "rust1", since = "1.0.0")] |
