1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//! Pointers Pretending To Be Integers For Crimes -- [uptr][] and [iptr][].

#![allow(unstable_name_collisions)]
use crate::Strict;

/// A pointer that pretends to be an integer, for API Crimes.
///
/// **Please don't use this type.**
///
/// If you can't possibly satisfy strict provenance for whatever reason, you can at least
/// use this type to make sure the compiler still understands that Pointers Are Happening.
///
/// All operations on this type will derive provenance from the left-hand-size (lhs).
/// So `x + y` has `x`'s provenance. *Many* operations are nonsensical if the pointer
/// inside is a real pointer, but hey, you've reached for the "I Know What I'm Doing"
/// lever, so we'll let you *say* whatever gibberish you want.
///
/// Please submit a PR if you need some operation defined on usize to be exposed here.

#[repr(transparent)]
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct uptr(*mut ());

/// A pointer that pretends to be an integer, for API Crimes.
///
/// **Please don't use this type.**
///
/// If you can't possibly satisfy strict provenance for whatever reason, you can at least
/// use this type to make sure the compiler still understands that Pointers Are Happening.
///
/// All operations on this type will derive provenance from the left-hand-size (lhs).
/// So `x + y` has `x`'s provenance. *Many* operations are nonsensical if the pointer
/// inside is a real pointer, but hey, you've reached for the "I Know What I'm Doing"
/// lever, so we'll let you *say* whatever gibberish you want.
///
/// Please submit a PR if you need some operation defined on isize to be exposed here.
#[repr(transparent)]
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct iptr(*mut ());

macro_rules! int_impls {
    ($self_ty: ident, $int_ty: ident) => {
        impl $self_ty {
            // Inherent MIN/MAX requires 1.43
            // pub const MIN: $self_ty = Self::from_int(<$int_ty>::MIN);
            // pub const MAX: $self_ty = Self::from_int(<$int_ty>::MAX);
            pub const MIN: $self_ty = Self::from_int(core::$int_ty::MIN);
            pub const MAX: $self_ty = Self::from_int(core::$int_ty::MAX);

            // Inherent BITS requires 1.53
            // pub const BITS: u32 = <$int_ty>::BITS;
            pub const BITS: u32 = core::mem::size_of::<$int_ty>() as u32 * 8;

            #[inline]
            #[must_use]
            pub const fn from_int(val: $int_ty) -> Self {
                $self_ty(crate::invalid_mut(val as usize))
            }

            #[inline]
            #[must_use]
            pub const fn from_ptr_mut<T>(val: *mut T) -> Self {
                $self_ty(val as *mut ())
            }

            #[inline]
            #[must_use]
            pub const fn from_ptr<T>(val: *const T) -> Self {
                $self_ty(val as *const () as *mut ())
            }

            pub const fn to_ptr(self) -> *mut () {
                self.0
            }

            #[inline]
            #[must_use]
            pub fn wrapping_add(self, rhs: Self) -> Self {
                $self_ty(
                    self.0.map_addr(|a| {
                        ((a as $int_ty).wrapping_add(rhs.0.addr() as $int_ty)) as usize
                    }),
                )
            }
            #[inline]
            #[must_use]
            pub fn wrapping_sub(self, rhs: Self) -> Self {
                $self_ty(
                    self.0.map_addr(|a| {
                        ((a as $int_ty).wrapping_sub(rhs.0.addr() as $int_ty)) as usize
                    }),
                )
            }
            #[inline]
            #[must_use]
            pub fn wrapping_mul(self, rhs: Self) -> Self {
                $self_ty(
                    self.0.map_addr(|a| {
                        ((a as $int_ty).wrapping_mul(rhs.0.addr() as $int_ty)) as usize
                    }),
                )
            }
            #[inline]
            #[must_use]
            pub fn wrapping_div(self, rhs: Self) -> Self {
                $self_ty(
                    self.0.map_addr(|a| {
                        ((a as $int_ty).wrapping_div(rhs.0.addr() as $int_ty)) as usize
                    }),
                )
            }
        }

        impl From<$int_ty> for $self_ty {
            #[inline]
            #[must_use]
            fn from(val: $int_ty) -> Self {
                $self_ty(crate::invalid_mut(val as usize))
            }
        }
        impl<T> From<*mut T> for $self_ty {
            #[inline]
            #[must_use]
            fn from(val: *mut T) -> Self {
                $self_ty(val as *mut ())
            }
        }
        impl<T> From<*const T> for $self_ty {
            #[inline]
            #[must_use]
            fn from(val: *const T) -> Self {
                $self_ty(val as *const () as *mut ())
            }
        }

        impl core::ops::Add<Self> for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn add(self, rhs: Self) -> Self::Output {
                $self_ty(
                    self.0
                        .map_addr(|a| ((a as $int_ty) + (rhs.0.addr() as $int_ty)) as usize),
                )
            }
        }
        impl core::ops::Sub<Self> for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn sub(self, rhs: Self) -> Self::Output {
                $self_ty(
                    self.0
                        .map_addr(|a| ((a as $int_ty) - (rhs.0.addr() as $int_ty)) as usize),
                )
            }
        }
        impl core::ops::Mul<Self> for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn mul(self, rhs: Self) -> Self::Output {
                $self_ty(
                    self.0
                        .map_addr(|a| ((a as $int_ty) * (rhs.0.addr() as $int_ty)) as usize),
                )
            }
        }
        impl core::ops::Div<Self> for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn div(self, rhs: Self) -> Self::Output {
                $self_ty(
                    self.0
                        .map_addr(|a| ((a as $int_ty) / (rhs.0.addr() as $int_ty)) as usize),
                )
            }
        }
        impl core::ops::Rem<Self> for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn rem(self, rhs: Self) -> Self::Output {
                $self_ty(
                    self.0
                        .map_addr(|a| ((a as $int_ty) % (rhs.0.addr() as $int_ty)) as usize),
                )
            }
        }
        impl core::ops::BitAnd<Self> for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn bitand(self, rhs: Self) -> Self::Output {
                $self_ty(
                    self.0
                        .map_addr(|a| ((a as $int_ty) & (rhs.0.addr() as $int_ty)) as usize),
                )
            }
        }
        impl core::ops::BitOr<Self> for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn bitor(self, rhs: Self) -> Self::Output {
                $self_ty(
                    self.0
                        .map_addr(|a| ((a as $int_ty) | (rhs.0.addr() as $int_ty)) as usize),
                )
            }
        }
        impl core::ops::BitXor<Self> for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn bitxor(self, rhs: Self) -> Self::Output {
                $self_ty(
                    self.0
                        .map_addr(|a| ((a as $int_ty) ^ (rhs.0.addr() as $int_ty)) as usize),
                )
            }
        }
        impl core::ops::Shl<usize> for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn shl(self, rhs: usize) -> Self::Output {
                $self_ty(self.0.map_addr(|a| ((a as $int_ty) << rhs) as usize))
            }
        }
        impl core::ops::Shr<usize> for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn shr(self, rhs: usize) -> Self::Output {
                $self_ty(self.0.map_addr(|a| ((a as $int_ty) >> rhs) as usize))
            }
        }

        impl core::ops::Not for $self_ty {
            type Output = Self;
            #[inline]
            #[must_use]
            fn not(self) -> Self::Output {
                $self_ty(self.0.map_addr(|a| (!(a as $int_ty)) as usize))
            }
        }

        impl core::ops::AddAssign<Self> for $self_ty {
            #[inline]
            #[must_use]
            fn add_assign(&mut self, rhs: Self) {
                self.0 = self
                    .0
                    .map_addr(|a| ((a as $int_ty) + (rhs.0.addr() as $int_ty)) as usize);
            }
        }
        impl core::ops::SubAssign<Self> for $self_ty {
            #[inline]
            #[must_use]
            fn sub_assign(&mut self, rhs: Self) {
                self.0 = self
                    .0
                    .map_addr(|a| ((a as $int_ty) - (rhs.0.addr() as $int_ty)) as usize);
            }
        }
        impl core::ops::MulAssign<Self> for $self_ty {
            #[inline]
            #[must_use]
            fn mul_assign(&mut self, rhs: Self) {
                self.0 = self
                    .0
                    .map_addr(|a| ((a as $int_ty) * (rhs.0.addr() as $int_ty)) as usize);
            }
        }
        impl core::ops::DivAssign<Self> for $self_ty {
            #[inline]
            #[must_use]
            fn div_assign(&mut self, rhs: Self) {
                self.0 = self
                    .0
                    .map_addr(|a| ((a as $int_ty) / (rhs.0.addr() as $int_ty)) as usize);
            }
        }
        impl core::ops::RemAssign<Self> for $self_ty {
            #[inline]
            #[must_use]
            fn rem_assign(&mut self, rhs: Self) {
                self.0 = self
                    .0
                    .map_addr(|a| ((a as $int_ty) % (rhs.0.addr() as $int_ty)) as usize);
            }
        }
        impl core::ops::BitAndAssign<Self> for $self_ty {
            #[inline]
            #[must_use]
            fn bitand_assign(&mut self, rhs: Self) {
                self.0 = self
                    .0
                    .map_addr(|a| ((a as $int_ty) & (rhs.0.addr() as $int_ty)) as usize);
            }
        }
        impl core::ops::BitOrAssign<Self> for $self_ty {
            #[inline]
            #[must_use]
            fn bitor_assign(&mut self, rhs: Self) {
                self.0 = self
                    .0
                    .map_addr(|a| ((a as $int_ty) | (rhs.0.addr() as $int_ty)) as usize);
            }
        }
        impl core::ops::BitXorAssign<Self> for $self_ty {
            #[inline]
            #[must_use]
            fn bitxor_assign(&mut self, rhs: Self) {
                self.0 = self
                    .0
                    .map_addr(|a| ((a as $int_ty) ^ (rhs.0.addr() as $int_ty)) as usize);
            }
        }
        impl core::ops::ShlAssign<usize> for $self_ty {
            #[inline]
            #[must_use]
            fn shl_assign(&mut self, rhs: usize) {
                self.0 = self.0.map_addr(|a| ((a as $int_ty) << rhs) as usize);
            }
        }
        impl core::ops::ShrAssign<usize> for $self_ty {
            #[inline]
            #[must_use]
            fn shr_assign(&mut self, rhs: usize) {
                self.0 = self.0.map_addr(|a| ((a as $int_ty) >> rhs) as usize);
            }
        }

        impl core::fmt::Display for $self_ty {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "{}", self.0.addr() as $int_ty)
            }
        }

        impl core::fmt::Debug for $self_ty {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "{:?}", self.0.addr() as $int_ty)
            }
        }
    };
}

int_impls!(uptr, usize);
int_impls!(iptr, isize);

// usize can't be negated
impl core::ops::Neg for iptr {
    type Output = Self;
    #[inline]
    #[must_use]
    fn neg(self) -> Self::Output {
        iptr(self.0.map_addr(|a| (-(a as isize)) as usize))
    }
}