add as_ptr method to raw slices

This commit is contained in:
Ralf Jung 2020-07-03 11:58:05 +02:00
parent 0cd7ff7ddf
commit c478b5473d
2 changed files with 42 additions and 0 deletions

View file

@ -826,6 +826,27 @@ impl<T> *const [T] {
// Only `std` can make this guarantee. // Only `std` can make this guarantee.
unsafe { Repr { rust: self }.raw }.len unsafe { Repr { rust: self }.raw }.len
} }
/// Returns a raw pointer to the slice's buffer.
///
/// This is equivalent to casting `self` to `*const T`, but more type-safe.
///
/// # Examples
///
/// ```rust
/// #![feature(slice_ptr_ptr)]
///
/// use std::ptr;
///
/// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3);
/// assert_eq!(slice.as_ptr(), 0 as *const i8);
/// ```
#[inline]
#[unstable(feature = "slice_ptr_ptr", issue = "none")]
#[rustc_const_unstable(feature = "const_slice_ptr_ptr", issue = "none")]
pub const fn as_ptr(self) -> *const T {
self as *const T
}
} }
// Equality for pointers // Equality for pointers

View file

@ -1028,6 +1028,27 @@ impl<T> *mut [T] {
// Only `std` can make this guarantee. // Only `std` can make this guarantee.
unsafe { Repr { rust_mut: self }.raw }.len unsafe { Repr { rust_mut: self }.raw }.len
} }
/// Returns a raw pointer to the slice's buffer.
///
/// This is equivalent to casting `self` to `*mut T`, but more type-safe.
///
/// # Examples
///
/// ```rust
/// #![feature(slice_ptr_ptr)]
///
/// use std::ptr;
///
/// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
/// assert_eq!(slice.as_ptr(), 0 as *mut i8);
/// ```
#[inline]
#[unstable(feature = "slice_ptr_ptr", issue = "none")]
#[rustc_const_unstable(feature = "const_slice_ptr_ptr", issue = "none")]
pub const fn as_ptr(self) -> *mut T {
self as *mut T
}
} }
// Equality for pointers // Equality for pointers