| 1 | //! Pointers Pretending To Be Integers For Crimes -- [uptr][] and [iptr][]. |
| 2 | |
| 3 | #![allow(unstable_name_collisions)] |
| 4 | use crate::Strict; |
| 5 | |
| 6 | /// A pointer that pretends to be an integer, for API Crimes. |
| 7 | /// |
| 8 | /// **Please don't use this type.** |
| 9 | /// |
| 10 | /// If you can't possibly satisfy strict provenance for whatever reason, you can at least |
| 11 | /// use this type to make sure the compiler still understands that Pointers Are Happening. |
| 12 | /// |
| 13 | /// All operations on this type will derive provenance from the left-hand-size (lhs). |
| 14 | /// So `x + y` has `x`'s provenance. *Many* operations are nonsensical if the pointer |
| 15 | /// inside is a real pointer, but hey, you've reached for the "I Know What I'm Doing" |
| 16 | /// lever, so we'll let you *say* whatever gibberish you want. |
| 17 | /// |
| 18 | /// Please submit a PR if you need some operation defined on usize to be exposed here. |
| 19 | |
| 20 | #[repr(transparent)] |
| 21 | #[allow(non_camel_case_types)] |
| 22 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 23 | pub struct uptr(*mut ()); |
| 24 | |
| 25 | /// A pointer that pretends to be an integer, for API Crimes. |
| 26 | /// |
| 27 | /// **Please don't use this type.** |
| 28 | /// |
| 29 | /// If you can't possibly satisfy strict provenance for whatever reason, you can at least |
| 30 | /// use this type to make sure the compiler still understands that Pointers Are Happening. |
| 31 | /// |
| 32 | /// All operations on this type will derive provenance from the left-hand-size (lhs). |
| 33 | /// So `x + y` has `x`'s provenance. *Many* operations are nonsensical if the pointer |
| 34 | /// inside is a real pointer, but hey, you've reached for the "I Know What I'm Doing" |
| 35 | /// lever, so we'll let you *say* whatever gibberish you want. |
| 36 | /// |
| 37 | /// Please submit a PR if you need some operation defined on isize to be exposed here. |
| 38 | #[repr(transparent)] |
| 39 | #[allow(non_camel_case_types)] |
| 40 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 41 | pub struct iptr(*mut ()); |
| 42 | |
| 43 | macro_rules! int_impls { |
| 44 | ($self_ty: ident, $int_ty: ident) => { |
| 45 | impl $self_ty { |
| 46 | // Inherent MIN/MAX requires 1.43 |
| 47 | // pub const MIN: $self_ty = Self::from_int(<$int_ty>::MIN); |
| 48 | // pub const MAX: $self_ty = Self::from_int(<$int_ty>::MAX); |
| 49 | pub const MIN: $self_ty = Self::from_int(core::$int_ty::MIN); |
| 50 | pub const MAX: $self_ty = Self::from_int(core::$int_ty::MAX); |
| 51 | |
| 52 | // Inherent BITS requires 1.53 |
| 53 | // pub const BITS: u32 = <$int_ty>::BITS; |
| 54 | pub const BITS: u32 = core::mem::size_of::<$int_ty>() as u32 * 8; |
| 55 | |
| 56 | #[inline] |
| 57 | #[must_use] |
| 58 | pub const fn from_int(val: $int_ty) -> Self { |
| 59 | $self_ty(crate::invalid_mut(val as usize)) |
| 60 | } |
| 61 | |
| 62 | #[inline] |
| 63 | #[must_use] |
| 64 | pub const fn from_ptr_mut<T>(val: *mut T) -> Self { |
| 65 | $self_ty(val as *mut ()) |
| 66 | } |
| 67 | |
| 68 | #[inline] |
| 69 | #[must_use] |
| 70 | pub const fn from_ptr<T>(val: *const T) -> Self { |
| 71 | $self_ty(val as *const () as *mut ()) |
| 72 | } |
| 73 | |
| 74 | pub const fn to_ptr(self) -> *mut () { |
| 75 | self.0 |
| 76 | } |
| 77 | |
| 78 | #[inline] |
| 79 | #[must_use] |
| 80 | pub fn wrapping_add(self, rhs: Self) -> Self { |
| 81 | $self_ty( |
| 82 | self.0.map_addr(|a| { |
| 83 | ((a as $int_ty).wrapping_add(rhs.0.addr() as $int_ty)) as usize |
| 84 | }), |
| 85 | ) |
| 86 | } |
| 87 | #[inline] |
| 88 | #[must_use] |
| 89 | pub fn wrapping_sub(self, rhs: Self) -> Self { |
| 90 | $self_ty( |
| 91 | self.0.map_addr(|a| { |
| 92 | ((a as $int_ty).wrapping_sub(rhs.0.addr() as $int_ty)) as usize |
| 93 | }), |
| 94 | ) |
| 95 | } |
| 96 | #[inline] |
| 97 | #[must_use] |
| 98 | pub fn wrapping_mul(self, rhs: Self) -> Self { |
| 99 | $self_ty( |
| 100 | self.0.map_addr(|a| { |
| 101 | ((a as $int_ty).wrapping_mul(rhs.0.addr() as $int_ty)) as usize |
| 102 | }), |
| 103 | ) |
| 104 | } |
| 105 | #[inline] |
| 106 | #[must_use] |
| 107 | pub fn wrapping_div(self, rhs: Self) -> Self { |
| 108 | $self_ty( |
| 109 | self.0.map_addr(|a| { |
| 110 | ((a as $int_ty).wrapping_div(rhs.0.addr() as $int_ty)) as usize |
| 111 | }), |
| 112 | ) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl From<$int_ty> for $self_ty { |
| 117 | #[inline] |
| 118 | #[must_use] |
| 119 | fn from(val: $int_ty) -> Self { |
| 120 | $self_ty(crate::invalid_mut(val as usize)) |
| 121 | } |
| 122 | } |
| 123 | impl<T> From<*mut T> for $self_ty { |
| 124 | #[inline] |
| 125 | #[must_use] |
| 126 | fn from(val: *mut T) -> Self { |
| 127 | $self_ty(val as *mut ()) |
| 128 | } |
| 129 | } |
| 130 | impl<T> From<*const T> for $self_ty { |
| 131 | #[inline] |
| 132 | #[must_use] |
| 133 | fn from(val: *const T) -> Self { |
| 134 | $self_ty(val as *const () as *mut ()) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | impl core::ops::Add<Self> for $self_ty { |
| 139 | type Output = Self; |
| 140 | #[inline] |
| 141 | #[must_use] |
| 142 | fn add(self, rhs: Self) -> Self::Output { |
| 143 | $self_ty( |
| 144 | self.0 |
| 145 | .map_addr(|a| ((a as $int_ty) + (rhs.0.addr() as $int_ty)) as usize), |
| 146 | ) |
| 147 | } |
| 148 | } |
| 149 | impl core::ops::Sub<Self> for $self_ty { |
| 150 | type Output = Self; |
| 151 | #[inline] |
| 152 | #[must_use] |
| 153 | fn sub(self, rhs: Self) -> Self::Output { |
| 154 | $self_ty( |
| 155 | self.0 |
| 156 | .map_addr(|a| ((a as $int_ty) - (rhs.0.addr() as $int_ty)) as usize), |
| 157 | ) |
| 158 | } |
| 159 | } |
| 160 | impl core::ops::Mul<Self> for $self_ty { |
| 161 | type Output = Self; |
| 162 | #[inline] |
| 163 | #[must_use] |
| 164 | fn mul(self, rhs: Self) -> Self::Output { |
| 165 | $self_ty( |
| 166 | self.0 |
| 167 | .map_addr(|a| ((a as $int_ty) * (rhs.0.addr() as $int_ty)) as usize), |
| 168 | ) |
| 169 | } |
| 170 | } |
| 171 | impl core::ops::Div<Self> for $self_ty { |
| 172 | type Output = Self; |
| 173 | #[inline] |
| 174 | #[must_use] |
| 175 | fn div(self, rhs: Self) -> Self::Output { |
| 176 | $self_ty( |
| 177 | self.0 |
| 178 | .map_addr(|a| ((a as $int_ty) / (rhs.0.addr() as $int_ty)) as usize), |
| 179 | ) |
| 180 | } |
| 181 | } |
| 182 | impl core::ops::Rem<Self> for $self_ty { |
| 183 | type Output = Self; |
| 184 | #[inline] |
| 185 | #[must_use] |
| 186 | fn rem(self, rhs: Self) -> Self::Output { |
| 187 | $self_ty( |
| 188 | self.0 |
| 189 | .map_addr(|a| ((a as $int_ty) % (rhs.0.addr() as $int_ty)) as usize), |
| 190 | ) |
| 191 | } |
| 192 | } |
| 193 | impl core::ops::BitAnd<Self> for $self_ty { |
| 194 | type Output = Self; |
| 195 | #[inline] |
| 196 | #[must_use] |
| 197 | fn bitand(self, rhs: Self) -> Self::Output { |
| 198 | $self_ty( |
| 199 | self.0 |
| 200 | .map_addr(|a| ((a as $int_ty) & (rhs.0.addr() as $int_ty)) as usize), |
| 201 | ) |
| 202 | } |
| 203 | } |
| 204 | impl core::ops::BitOr<Self> for $self_ty { |
| 205 | type Output = Self; |
| 206 | #[inline] |
| 207 | #[must_use] |
| 208 | fn bitor(self, rhs: Self) -> Self::Output { |
| 209 | $self_ty( |
| 210 | self.0 |
| 211 | .map_addr(|a| ((a as $int_ty) | (rhs.0.addr() as $int_ty)) as usize), |
| 212 | ) |
| 213 | } |
| 214 | } |
| 215 | impl core::ops::BitXor<Self> for $self_ty { |
| 216 | type Output = Self; |
| 217 | #[inline] |
| 218 | #[must_use] |
| 219 | fn bitxor(self, rhs: Self) -> Self::Output { |
| 220 | $self_ty( |
| 221 | self.0 |
| 222 | .map_addr(|a| ((a as $int_ty) ^ (rhs.0.addr() as $int_ty)) as usize), |
| 223 | ) |
| 224 | } |
| 225 | } |
| 226 | impl core::ops::Shl<usize> for $self_ty { |
| 227 | type Output = Self; |
| 228 | #[inline] |
| 229 | #[must_use] |
| 230 | fn shl(self, rhs: usize) -> Self::Output { |
| 231 | $self_ty(self.0.map_addr(|a| ((a as $int_ty) << rhs) as usize)) |
| 232 | } |
| 233 | } |
| 234 | impl core::ops::Shr<usize> for $self_ty { |
| 235 | type Output = Self; |
| 236 | #[inline] |
| 237 | #[must_use] |
| 238 | fn shr(self, rhs: usize) -> Self::Output { |
| 239 | $self_ty(self.0.map_addr(|a| ((a as $int_ty) >> rhs) as usize)) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | impl core::ops::Not for $self_ty { |
| 244 | type Output = Self; |
| 245 | #[inline] |
| 246 | #[must_use] |
| 247 | fn not(self) -> Self::Output { |
| 248 | $self_ty(self.0.map_addr(|a| (!(a as $int_ty)) as usize)) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | impl core::ops::AddAssign<Self> for $self_ty { |
| 253 | #[inline] |
| 254 | #[must_use] |
| 255 | fn add_assign(&mut self, rhs: Self) { |
| 256 | self.0 = self |
| 257 | .0 |
| 258 | .map_addr(|a| ((a as $int_ty) + (rhs.0.addr() as $int_ty)) as usize); |
| 259 | } |
| 260 | } |
| 261 | impl core::ops::SubAssign<Self> for $self_ty { |
| 262 | #[inline] |
| 263 | #[must_use] |
| 264 | fn sub_assign(&mut self, rhs: Self) { |
| 265 | self.0 = self |
| 266 | .0 |
| 267 | .map_addr(|a| ((a as $int_ty) - (rhs.0.addr() as $int_ty)) as usize); |
| 268 | } |
| 269 | } |
| 270 | impl core::ops::MulAssign<Self> for $self_ty { |
| 271 | #[inline] |
| 272 | #[must_use] |
| 273 | fn mul_assign(&mut self, rhs: Self) { |
| 274 | self.0 = self |
| 275 | .0 |
| 276 | .map_addr(|a| ((a as $int_ty) * (rhs.0.addr() as $int_ty)) as usize); |
| 277 | } |
| 278 | } |
| 279 | impl core::ops::DivAssign<Self> for $self_ty { |
| 280 | #[inline] |
| 281 | #[must_use] |
| 282 | fn div_assign(&mut self, rhs: Self) { |
| 283 | self.0 = self |
| 284 | .0 |
| 285 | .map_addr(|a| ((a as $int_ty) / (rhs.0.addr() as $int_ty)) as usize); |
| 286 | } |
| 287 | } |
| 288 | impl core::ops::RemAssign<Self> for $self_ty { |
| 289 | #[inline] |
| 290 | #[must_use] |
| 291 | fn rem_assign(&mut self, rhs: Self) { |
| 292 | self.0 = self |
| 293 | .0 |
| 294 | .map_addr(|a| ((a as $int_ty) % (rhs.0.addr() as $int_ty)) as usize); |
| 295 | } |
| 296 | } |
| 297 | impl core::ops::BitAndAssign<Self> for $self_ty { |
| 298 | #[inline] |
| 299 | #[must_use] |
| 300 | fn bitand_assign(&mut self, rhs: Self) { |
| 301 | self.0 = self |
| 302 | .0 |
| 303 | .map_addr(|a| ((a as $int_ty) & (rhs.0.addr() as $int_ty)) as usize); |
| 304 | } |
| 305 | } |
| 306 | impl core::ops::BitOrAssign<Self> for $self_ty { |
| 307 | #[inline] |
| 308 | #[must_use] |
| 309 | fn bitor_assign(&mut self, rhs: Self) { |
| 310 | self.0 = self |
| 311 | .0 |
| 312 | .map_addr(|a| ((a as $int_ty) | (rhs.0.addr() as $int_ty)) as usize); |
| 313 | } |
| 314 | } |
| 315 | impl core::ops::BitXorAssign<Self> for $self_ty { |
| 316 | #[inline] |
| 317 | #[must_use] |
| 318 | fn bitxor_assign(&mut self, rhs: Self) { |
| 319 | self.0 = self |
| 320 | .0 |
| 321 | .map_addr(|a| ((a as $int_ty) ^ (rhs.0.addr() as $int_ty)) as usize); |
| 322 | } |
| 323 | } |
| 324 | impl core::ops::ShlAssign<usize> for $self_ty { |
| 325 | #[inline] |
| 326 | #[must_use] |
| 327 | fn shl_assign(&mut self, rhs: usize) { |
| 328 | self.0 = self.0.map_addr(|a| ((a as $int_ty) << rhs) as usize); |
| 329 | } |
| 330 | } |
| 331 | impl core::ops::ShrAssign<usize> for $self_ty { |
| 332 | #[inline] |
| 333 | #[must_use] |
| 334 | fn shr_assign(&mut self, rhs: usize) { |
| 335 | self.0 = self.0.map_addr(|a| ((a as $int_ty) >> rhs) as usize); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | impl core::fmt::Display for $self_ty { |
| 340 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 341 | write!(f, "{}", self.0.addr() as $int_ty) |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | impl core::fmt::Debug for $self_ty { |
| 346 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 347 | write!(f, "{:?}", self.0.addr() as $int_ty) |
| 348 | } |
| 349 | } |
| 350 | }; |
| 351 | } |
| 352 | |
| 353 | int_impls!(uptr, usize); |
| 354 | int_impls!(iptr, isize); |
| 355 | |
| 356 | // usize can't be negated |
| 357 | impl core::ops::Neg for iptr { |
| 358 | type Output = Self; |
| 359 | #[inline] |
| 360 | #[must_use] |
| 361 | fn neg(self) -> Self::Output { |
| 362 | iptr(self.0.map_addr(|a| (-(a as isize)) as usize)) |
| 363 | } |
| 364 | } |