Make <*const/mut T>::offset_from const fn
This commit is contained in:
parent
e0436d912d
commit
88b5e945e0
10 changed files with 276 additions and 3 deletions
|
@ -1286,7 +1286,22 @@ impl<T: ?Sized> *const T {
|
|||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "ptr_offset_from", issue = "41079")]
|
||||
#[cfg(not(bootstrap))]
|
||||
#[rustc_const_unstable(feature = "const_ptr_offset_from")]
|
||||
#[inline]
|
||||
pub const unsafe fn offset_from(self, origin: *const T) -> isize where T: Sized {
|
||||
let pointee_size = mem::size_of::<T>();
|
||||
let ok = 0 < pointee_size && pointee_size <= isize::max_value() as usize;
|
||||
// assert that the pointee size is valid in a const eval compatible way
|
||||
// FIXME: do this with a real assert at some point
|
||||
[()][(!ok) as usize];
|
||||
intrinsics::ptr_offset_from(self, origin)
|
||||
}
|
||||
|
||||
#[unstable(feature = "ptr_offset_from", issue = "41079")]
|
||||
#[inline]
|
||||
#[cfg(bootstrap)]
|
||||
/// bootstrap
|
||||
pub unsafe fn offset_from(self, origin: *const T) -> isize where T: Sized {
|
||||
let pointee_size = mem::size_of::<T>();
|
||||
assert!(0 < pointee_size && pointee_size <= isize::max_value() as usize);
|
||||
|
@ -2013,8 +2028,9 @@ impl<T: ?Sized> *mut T {
|
|||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "ptr_offset_from", issue = "41079")]
|
||||
#[rustc_const_unstable(feature = "const_ptr_offset_from")]
|
||||
#[inline]
|
||||
pub unsafe fn offset_from(self, origin: *const T) -> isize where T: Sized {
|
||||
pub const unsafe fn offset_from(self, origin: *const T) -> isize where T: Sized {
|
||||
(self as *const T).offset_from(origin)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue