Skip to content

Commit

Permalink
Rollup merge of rust-lang#77097 - fusion-engineering-forks:slice-ptr-…
Browse files Browse the repository at this point in the history
…range-const-fn, r=oli-obk

Make [].as_[mut_]ptr_range() (unstably) const.

Gated behind `const_ptr_offset`, as suggested by rust-lang#65807 (comment)

This also marks `[].as_mut_ptr()` as const, because it's used by `as_mut_ptr_range`. I gated it behind the same feature, because I figured it's not worth adding a separate tracking issue for const `as_mut_ptr`.
  • Loading branch information
RalfJung authored Sep 25, 2020
2 parents 6e3b289 + 631c688 commit d51976d
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,9 @@ impl<T> [T] {
/// assert_eq!(x, &[3, 4, 6]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut T {
pub const fn as_mut_ptr(&mut self) -> *mut T {
self as *mut [T] as *mut T
}

Expand Down Expand Up @@ -469,8 +470,9 @@ impl<T> [T] {
///
/// [`as_ptr`]: #method.as_ptr
#[unstable(feature = "slice_ptr_range", issue = "65807")]
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
#[inline]
pub fn as_ptr_range(&self) -> Range<*const T> {
pub const fn as_ptr_range(&self) -> Range<*const T> {
let start = self.as_ptr();
// SAFETY: The `add` here is safe, because:
//
Expand Down Expand Up @@ -510,8 +512,9 @@ impl<T> [T] {
///
/// [`as_mut_ptr`]: #method.as_mut_ptr
#[unstable(feature = "slice_ptr_range", issue = "65807")]
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
#[inline]
pub fn as_mut_ptr_range(&mut self) -> Range<*mut T> {
pub const fn as_mut_ptr_range(&mut self) -> Range<*mut T> {
let start = self.as_mut_ptr();
// SAFETY: See as_ptr_range() above for why `add` here is safe.
let end = unsafe { start.add(self.len()) };
Expand Down

0 comments on commit d51976d

Please sign in to comment.