Skip to content

Commit c478b54

Browse files
committed
add as_ptr method to raw slices
1 parent 0cd7ff7 commit c478b54

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/libcore/ptr/const_ptr.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,27 @@ impl<T> *const [T] {
826826
// Only `std` can make this guarantee.
827827
unsafe { Repr { rust: self }.raw }.len
828828
}
829+
830+
/// Returns a raw pointer to the slice's buffer.
831+
///
832+
/// This is equivalent to casting `self` to `*const T`, but more type-safe.
833+
///
834+
/// # Examples
835+
///
836+
/// ```rust
837+
/// #![feature(slice_ptr_ptr)]
838+
///
839+
/// use std::ptr;
840+
///
841+
/// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3);
842+
/// assert_eq!(slice.as_ptr(), 0 as *const i8);
843+
/// ```
844+
#[inline]
845+
#[unstable(feature = "slice_ptr_ptr", issue = "none")]
846+
#[rustc_const_unstable(feature = "const_slice_ptr_ptr", issue = "none")]
847+
pub const fn as_ptr(self) -> *const T {
848+
self as *const T
849+
}
829850
}
830851

831852
// Equality for pointers

src/libcore/ptr/mut_ptr.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,27 @@ impl<T> *mut [T] {
10281028
// Only `std` can make this guarantee.
10291029
unsafe { Repr { rust_mut: self }.raw }.len
10301030
}
1031+
1032+
/// Returns a raw pointer to the slice's buffer.
1033+
///
1034+
/// This is equivalent to casting `self` to `*mut T`, but more type-safe.
1035+
///
1036+
/// # Examples
1037+
///
1038+
/// ```rust
1039+
/// #![feature(slice_ptr_ptr)]
1040+
///
1041+
/// use std::ptr;
1042+
///
1043+
/// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
1044+
/// assert_eq!(slice.as_ptr(), 0 as *mut i8);
1045+
/// ```
1046+
#[inline]
1047+
#[unstable(feature = "slice_ptr_ptr", issue = "none")]
1048+
#[rustc_const_unstable(feature = "const_slice_ptr_ptr", issue = "none")]
1049+
pub const fn as_ptr(self) -> *mut T {
1050+
self as *mut T
1051+
}
10311052
}
10321053

10331054
// Equality for pointers

0 commit comments

Comments
 (0)