| 1 |
// { dg-additional-options "-w" } |
| 2 |
#![feature(intrinsics)] |
| 3 |
|
| 4 |
#[lang = "sized"] |
| 5 |
pub trait Sized {} |
| 6 |
|
| 7 |
extern "rust-intrinsic" { |
| 8 |
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")] |
| 9 |
pub fn offset<T>(dst: *const T, offset: isize) -> *const T; |
| 10 |
} |
| 11 |
|
| 12 |
struct FatPtr<T> { |
| 13 |
data: *const T, |
| 14 |
len: usize, |
| 15 |
} |
| 16 |
|
| 17 |
union Repr<T> { |
| 18 |
rust: *const [T], |
| 19 |
rust_mut: *mut [T], |
| 20 |
raw: FatPtr<T>, |
| 21 |
} |
| 22 |
|
| 23 |
impl<T> *const [T] { |
| 24 |
pub const fn len(self) -> usize { |
| 25 |
// SAFETY: this is safe because `*const [T]` and `FatPtr<T>` have the same layout. |
| 26 |
// Only `std` can make this guarantee. |
| 27 |
unsafe { Repr { rust: self }.raw.len } |
| 28 |
} |
| 29 |
|
| 30 |
pub const fn as_ptr(self) -> *const T { |
| 31 |
self as *const T |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl<T> *const T { |
| 36 |
pub const unsafe fn offset(self, count: isize) -> *const T { |
| 37 |
unsafe { offset(self, count) } |
| 38 |
} |
| 39 |
|
| 40 |
pub const unsafe fn add(self, count: usize) -> Self { |
| 41 |
unsafe { self.offset(count as isize) } |
| 42 |
} |
| 43 |
|
| 44 |
pub const fn as_ptr(self) -> *const T { |
| 45 |
self as *const T |
| 46 |
} |
| 47 |
} |