| 1 |
// { dg-options "-w" } |
| 2 |
|
| 3 |
#![feature(intrinsics)] |
| 4 |
|
| 5 |
#[lang = "sized"] |
| 6 |
pub trait Sized {} |
| 7 |
|
| 8 |
mod intrinsics { |
| 9 |
extern "rust-intrinsic" { |
| 10 |
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")] |
| 11 |
pub fn offset<T>(ptr: *const T, count: isize) -> *const T; |
| 12 |
} |
| 13 |
} |
| 14 |
|
| 15 |
mod mem { |
| 16 |
extern "rust-intrinsic" { |
| 17 |
fn size_of<T>() -> usize; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
extern "C" { |
| 22 |
fn printf(s: *const i8, ...); |
| 23 |
} |
| 24 |
|
| 25 |
struct FatPtr<T> { |
| 26 |
data: *const T, |
| 27 |
len: usize, |
| 28 |
} |
| 29 |
|
| 30 |
pub union Repr<T> { |
| 31 |
rust: *const [T], |
| 32 |
rust_mut: *mut [T], |
| 33 |
raw: FatPtr<T>, |
| 34 |
} |
| 35 |
|
| 36 |
pub enum Option<T> { |
| 37 |
None, |
| 38 |
Some(T), |
| 39 |
} |
| 40 |
|
| 41 |
#[lang = "Range"] |
| 42 |
pub struct Range<Idx> { |
| 43 |
pub start: Idx, |
| 44 |
pub end: Idx, |
| 45 |
} |
| 46 |
|
| 47 |
#[lang = "const_slice_ptr"] |
| 48 |
impl<T> *const [T] { |
| 49 |
pub const fn len(self) -> usize { |
| 50 |
unsafe { Repr { rust: self }.raw.len } |
| 51 |
} |
| 52 |
|
| 53 |
pub const fn as_ptr(self) -> *const T { |
| 54 |
self as *const T |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
#[lang = "const_ptr"] |
| 59 |
impl<T> *const T { |
| 60 |
pub const unsafe fn offset(self, count: isize) -> *const T { |
| 61 |
unsafe { intrinsics::offset(self, count) } |
| 62 |
} |
| 63 |
|
| 64 |
pub const unsafe fn add(self, count: usize) -> Self { |
| 65 |
unsafe { self.offset(count as isize) } |
| 66 |
} |
| 67 |
|
| 68 |
pub const fn as_ptr(self) -> *const T { |
| 69 |
self as *const T |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] { |
| 74 |
unsafe { |
| 75 |
Repr { |
| 76 |
raw: FatPtr { data, len }, |
| 77 |
} |
| 78 |
.rust |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
#[lang = "index"] |
| 83 |
trait Index<Idx> { |
| 84 |
type Output; |
| 85 |
|
| 86 |
fn index(&self, index: Idx) -> &Self::Output; |
| 87 |
} |
| 88 |
|
| 89 |
impl<T> [T] { |
| 90 |
pub const fn is_empty(&self) -> bool { |
| 91 |
self.len() == 0 |
| 92 |
} |
| 93 |
|
| 94 |
pub const fn len(&self) -> usize { |
| 95 |
unsafe { Repr { rust: self }.raw.len } |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
pub unsafe trait SliceIndex<T> { |
| 100 |
type Output; |
| 101 |
|
| 102 |
fn get(self, slice: &T) -> Option<&Self::Output>; |
| 103 |
|
| 104 |
unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output; |
| 105 |
|
| 106 |
fn index(self, slice: &T) -> &Self::Output; |
| 107 |
} |
| 108 |
|
| 109 |
unsafe impl<T> SliceIndex<[T]> for usize { |
| 110 |
type Output = T; |
| 111 |
|
| 112 |
fn get(self, slice: &[T]) -> Option<&T> { |
| 113 |
unsafe { Option::Some(&*self.get_unchecked(slice)) } |
| 114 |
} |
| 115 |
|
| 116 |
unsafe fn get_unchecked(self, slice: *const [T]) -> *const T { |
| 117 |
// SAFETY: the caller guarantees that `slice` is not dangling, so it |
| 118 |
// cannot be longer than `isize::MAX`. They also guarantee that |
| 119 |
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`, |
| 120 |
// so the call to `add` is safe. |
| 121 |
unsafe { slice.as_ptr().add(self) } |
| 122 |
} |
| 123 |
|
| 124 |
fn index(self, slice: &[T]) -> &T { |
| 125 |
unsafe { &*self.get_unchecked(slice) } |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
unsafe impl<T> SliceIndex<[T]> for Range<usize> { |
| 130 |
type Output = [T]; |
| 131 |
|
| 132 |
fn get(self, slice: &[T]) -> Option<&[T]> { |
| 133 |
if self.start > self.end || self.end > slice.len() { |
| 134 |
Option::None |
| 135 |
} else { |
| 136 |
unsafe { Option::Some(&*self.get_unchecked(slice)) } |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] { |
| 141 |
unsafe { |
| 142 |
let a: *const T = slice.as_ptr(); |
| 143 |
let b: *const T = a.add(self.start); |
| 144 |
slice_from_raw_parts(b, self.end - self.start) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
fn index(self, slice: &[T]) -> &[T] { |
| 149 |
unsafe { &*self.get_unchecked(slice) } |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl<T, I> Index<I> for [T] |
| 154 |
where |
| 155 |
I: SliceIndex<[T]>, |
| 156 |
{ |
| 157 |
type Output = I::Output; |
| 158 |
|
| 159 |
fn index(&self, index: I) -> &I::Output { |
| 160 |
unsafe { |
| 161 |
let a = "slice-index\n\0"; |
| 162 |
let b = a as *const str; |
| 163 |
let c = b as *const i8; |
| 164 |
|
| 165 |
printf(c); |
| 166 |
} |
| 167 |
|
| 168 |
index.index(self) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
fn main() -> i32 { |
| 173 |
let a = [1, 2, 3, 4, 5]; |
| 174 |
let b = a[1]; |
| 175 |
|
| 176 |
b - 2 |
| 177 |
} |