pointdexter 1.0.0-rc1

unifies *const/*mut pointers using the trait system
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! Implementation details for [`NonNullPointer`]. See its type documentation..

use core::{
	any,
	cmp,
	fmt,
	hash,
	marker::PhantomData,
	num::NonZero,
	ptr::NonNull,
};

use crate::{
	NonNullPointer,
	NullPointerError,
	Permission,
	Pointer,
	Reference,
	Shared,
	Unique,
};

// region NEW_API

impl<T, P> NonNullPointer<T, P>
where
	T: ?Sized,
	P: Permission,
{
	#[inline(always)]
	const fn from_nonnull(nonnull: NonNull<T>) -> Self {
		Self {
			inner: nonnull,
			_perm: PhantomData,
		}
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.as_reference.md")]
	pub const unsafe fn as_reference<'a>(self) -> Reference<'a, T, P> {
		unsafe { core::mem::transmute_copy(&self.inner) }
	}
}

impl<T, P> NonNullPointer<T, P>
where P: Permission
{
	#[inline(always)]
	#[doc = include_str!("../doc/fn.slice_from_raw_parts.md")]
	pub const fn make_slice(self, len: usize) -> NonNullPointer<[T], P> {
		NonNullPointer::slice_from_raw_parts(self, len)
	}
}

// endregion NEW_API

// region STD_UNSIZED

impl<T> NonNullPointer<T, Shared>
where T: ?Sized
{
	/// Converts a reference to a `NonNullPointer`.
	#[inline(always)]
	#[cfg(feature = "rust_189")]
	pub const fn from_ref(r: &T) -> Self {
		Self::from_nonnull(NonNull::from_ref(r))
	}
}

impl<T> NonNullPointer<T, Unique>
where T: ?Sized
{
	/// Converts a unique reference to a `NonNullPointer`.
	#[inline(always)]
	#[cfg(feature = "rust_189")]
	pub const fn from_mut(r: &mut T) -> Self {
		Self::from_nonnull(NonNull::from_mut(r))
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.as_reference.md")]
	pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
		unsafe { self.inner.as_mut() }
	}
}

impl<T, P> NonNullPointer<T, P>
where
	T: ?Sized,
	P: Permission,
{
	#[inline(always)]
	#[doc = include_str!("../doc/struct.NonNullPointer/method.new_unchecked.md")]
	pub const unsafe fn new_unchecked(ptr: Pointer<T, P>) -> Self {
		Self::from_nonnull(unsafe {
			NonNull::new_unchecked(ptr.into_raw_const().cast_mut())
		})
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.NonNullPointer/method.new.md")]
	pub const fn new(
		ptr: Pointer<T, P>,
	) -> Result<Self, NullPointerError<T, P>> {
		match NonNull::new(ptr.into_raw_const().cast_mut()) {
			| Some(nn) => Ok(Self::from_nonnull(nn)),
			| None => Err(NullPointerError::new()),
		}
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.addr.md")]
	///[`map_addr`]: Self::map_addr
	///[`with_addr`]: Self::with_addr
	pub fn addr(self) -> NonZero<usize> {
		self.inner.addr()
	}

	#[inline(always)]
	#[cfg(feature = "rust_189")]
	#[doc = include_str!("../doc/struct.Pointer/method.expose_provenance.md")]
	pub fn expose_provenance(self) -> NonZero<usize> {
		self.inner.expose_provenance()
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.with_addr.md")]
	///[`wrapping_offset`]: Pointer::wrapping_offset
	pub fn with_addr(self, addr: NonZero<usize>) -> Self {
		Self::from_nonnull(self.inner.with_addr(addr))
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.map_addr.md")]
	///[`with_addr`]: Self::with_addr
	pub fn map_addr(
		self,
		func: impl FnOnce(NonZero<usize>) -> NonZero<usize>,
	) -> Self {
		Self::from_nonnull(self.inner.map_addr(func))
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.NonNullPointer/method.as_ptr.md")]
	pub const fn as_ptr(self) -> Pointer<T, P> {
		Pointer::rewrap_mut(self.inner.as_ptr())
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.as_reference.md")]
	pub const unsafe fn as_ref<'a>(&self) -> &'a T {
		unsafe { self.inner.as_ref() }
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.cast.md")]
	pub const fn cast<U>(self) -> NonNullPointer<U, P> {
		NonNullPointer::from_nonnull(self.inner.cast::<U>())
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.byte_offset.md")]
	pub const unsafe fn byte_offset(self, count: isize) -> Self {
		Self::from_nonnull(unsafe { self.inner.byte_offset(count) })
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.byte_add.md")]
	pub const unsafe fn byte_add(self, count: usize) -> Self {
		Self::from_nonnull(unsafe { self.inner.byte_add(count) })
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.byte_sub.md")]
	pub const unsafe fn byte_sub(self, count: usize) -> Self {
		Self::from_nonnull(unsafe { self.inner.byte_sub(count) })
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.byte_offset_from.md")]
	pub const unsafe fn byte_offset_from<U, Q>(
		self,
		origin: NonNullPointer<U, Q>,
	) -> isize
	where
		U: ?Sized,
		Q: Permission,
	{
		unsafe { self.inner.byte_offset_from(origin.inner) }
	}

	#[inline(always)]
	#[cfg(feature = "rust_187")]
	#[doc = include_str!("../doc/struct.Pointer/method.byte_offset_from_unsigned.md")]
	pub const unsafe fn byte_offset_from_unsigned<Q>(
		self,
		subtracted: NonNullPointer<T, Q>,
	) -> usize
	where
		Q: Permission,
	{
		unsafe { self.inner.byte_offset_from_unsigned(subtracted.inner) }
	}
}

impl<T> NonNullPointer<T, Unique>
where T: ?Sized
{
	#[inline(always)]
	#[doc = include_str!("../doc/fn.drop_in_place.md")]
	pub unsafe fn drop_in_place(self) {
		unsafe {
			self.inner.drop_in_place();
		}
	}
}

// endregion STD_UNSIZED

// region STD_SIZED

impl<T, P> NonNullPointer<T, P>
where P: Permission
{
	#[inline(always)]
	#[doc = include_str!("../doc/fn.dangling.md")]
	pub const fn dangling() -> Self {
		Self::from_nonnull(NonNull::dangling())
	}

	#[inline(always)]
	#[cfg(feature = "rust_189")]
	#[doc = include_str!("../doc/fn.without_provenance.md")]
	///[`with_exposed_provenance`]: Self::with_exposed_provenance
	pub const fn without_provenance(addr: NonZero<usize>) -> Self {
		Self::from_nonnull(NonNull::without_provenance(addr))
	}

	#[inline(always)]
	#[cfg(feature = "rust_189")]
	#[doc = include_str!("../doc/fn.with_exposed_provenance.md")]
	///[`expose_provenance`]: Self::expose_provenance
	///[`with_addr`]: Self::with_addr
	///[`without_provenance`]: Self::without_provenance
	pub fn with_exposed_provenance(addr: NonZero<usize>) -> Self {
		Self::from_nonnull(NonNull::with_exposed_provenance(addr))
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.offset.md")]
	///[`wrapping_offset`]: Pointer::wrapping_offset
	pub const unsafe fn offset(self, count: isize) -> Self {
		Self::from_nonnull(unsafe { self.inner.offset(count) })
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.add.md")]
	///[`offset`]: Self::offset
	///[`wrapping_add`]: Pointer::wrapping_add
	pub const unsafe fn add(self, count: usize) -> Self {
		Self::from_nonnull(unsafe { self.inner.add(count) })
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.sub.md")]
	///[`offset`]: Self::offset
	///[`wrapping_sub`]: Pointer::wrapping_sub
	pub const unsafe fn sub(self, count: usize) -> Self {
		Self::from_nonnull(unsafe { self.inner.sub(count) })
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.offset_from.md")]
	pub const unsafe fn offset_from<Q>(
		self,
		origin: NonNullPointer<T, Q>,
	) -> isize
	where
		Q: Permission,
	{
		unsafe { self.inner.offset_from(origin.inner) }
	}

	#[inline(always)]
	#[cfg(feature = "rust_187")]
	#[doc = include_str!("../doc/struct.Pointer/method.offset_from_unsigned.md")]
	pub const unsafe fn offset_from_unsigned<Q>(
		self,
		subtracted: NonNullPointer<T, Q>,
	) -> usize
	where
		Q: Permission,
	{
		unsafe { self.inner.offset_from_unsigned(subtracted.inner) }
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.read.md")]
	///[`read_unaligned`]: Self::read_unaligned
	pub const unsafe fn read(self) -> T {
		unsafe { self.inner.read() }
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.read_volatile.md")]
	///[`read`]: Self::read
	#[cfg_attr(
		not(feature = "rust_189"),
		doc = "[`without_provenance`]: Pointer::without_provenance"
	)]
	#[cfg_attr(
		feature = "rust_189",
		doc = "[`without_provenance`]: Self::without_provenance"
	)]
	pub unsafe fn read_volatile(self) -> T {
		unsafe { self.inner.read_volatile() }
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.read_unaligned.md")]
	///[`read`]: Self::read
	pub const unsafe fn read_unaligned(self) -> T {
		unsafe { self.inner.read_unaligned() }
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.copy.md")]
	///[`copy_nonoverlapping`]: Self::copy_to_nonoverlapping
	///[`read`]: Self::read
	pub const unsafe fn copy_to(
		self,
		dest: NonNullPointer<T, Unique>,
		count: usize,
	) {
		unsafe {
			self.inner.copy_to(dest.inner, count);
		}
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.copy_nonoverlapping.md")]
	///[`copy`]: Self::copy_to`
	///[`read`]: Self::read
	pub const unsafe fn copy_to_nonoverlapping(
		self,
		dest: NonNullPointer<T, Unique>,
		count: usize,
	) {
		unsafe {
			self.inner.copy_to_nonoverlapping(dest.inner, count);
		}
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.align_offset.md")]
	///[`wrapping_add`]: Pointer::wrapping_add
	pub fn align_offset(self, align: usize) -> usize {
		self.inner.align_offset(align)
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.is_aligned.md")]
	pub fn is_aligned(self) -> bool {
		self.inner.is_aligned()
	}
}

impl<T> NonNullPointer<T, Unique> {
	#[inline(always)]
	#[doc = include_str!("../doc/fn.copy.md")]
	///[`copy_nonoverlapping`]: Self::copy_from_nonoverlapping
	///[`read`]: Self::read
	pub const unsafe fn copy_from<P>(
		self,
		src: NonNullPointer<T, P>,
		count: usize,
	) where
		P: Permission,
	{
		unsafe {
			self.inner.copy_from(src.inner, count);
		}
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.copy_nonoverlapping.md")]
	///[`copy`]: Self::copy_from`
	///[`read`]: Self::read
	pub const unsafe fn copy_from_nonoverlapping<P>(
		self,
		src: NonNullPointer<T, P>,
		count: usize,
	) where
		P: Permission,
	{
		unsafe {
			self.inner.copy_from_nonoverlapping(src.inner, count);
		}
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.write.md")]
	///[`read`]: Self::read
	///[`write_unaligned`]: Self::write_unaligned
	pub const unsafe fn write(self, val: T) {
		unsafe {
			self.inner.write(val);
		}
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.write_bytes.md")]
	pub const unsafe fn write_bytes(self, val: u8, count: usize) {
		unsafe {
			self.inner.write_bytes(val, count);
		}
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.write_volatile.md")]
	pub unsafe fn write_volatile(self, val: T) {
		unsafe {
			self.inner.write_volatile(val);
		}
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.write_unaligned.md")]
	///[`read_unaligned`]: Self::read_unaligned
	pub const unsafe fn write_unaligned(self, val: T) {
		unsafe {
			self.inner.write_unaligned(val);
		}
	}

	#[inline(always)]
	#[cfg(not(feature = "rust_188"))]
	#[doc = include_str!("../doc/fn.replace.md")]
	pub unsafe fn replace(self, src: T) -> T {
		unsafe { self.inner.replace(src) }
	}

	#[inline(always)]
	#[cfg(feature = "rust_188")]
	#[doc = include_str!("../doc/fn.replace.md")]
	pub const unsafe fn replace(self, src: T) -> T {
		unsafe { self.inner.replace(src) }
	}

	#[inline(always)]
	#[doc = include_str!("../doc/fn.swap.md")]
	pub const unsafe fn swap(self, with: Self) {
		unsafe {
			self.inner.swap(with.inner);
		}
	}
}

// endregion STD_SIZED

// region STD_SLICE

impl<T, P> NonNullPointer<[T], P>
where P: Permission
{
	#[inline(always)]
	#[doc = include_str!("../doc/fn.slice_from_raw_parts.md")]
	pub const fn slice_from_raw_parts(
		data: NonNullPointer<T, P>,
		len: usize,
	) -> Self {
		Self::from_nonnull(NonNull::slice_from_raw_parts(data.inner, len))
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.len.md")]
	pub const fn len(self) -> usize {
		self.inner.len()
	}

	#[inline(always)]
	#[doc = include_str!("../doc/struct.Pointer/method.is_empty.md")]
	pub const fn is_empty(self) -> bool {
		self.inner.is_empty()
	}
}

// endregion STD

// region TRAITS

impl<T, P> Clone for NonNullPointer<T, P>
where
	T: ?Sized,
	P: Permission,
{
	#[inline]
	fn clone(&self) -> Self {
		*self
	}
}

impl<T, P> Eq for NonNullPointer<T, P>
where
	T: ?Sized,
	P: Permission,
{
}

impl<T, P> Ord for NonNullPointer<T, P>
where
	T: ?Sized,
	P: Permission,
{
	#[inline]
	#[expect(
		ambiguous_wide_pointer_comparisons,
		reason = "the standard library allows it"
	)]
	fn cmp(&self, other: &Self) -> cmp::Ordering {
		self.inner.cmp(&other.inner)
	}
}

impl<T, P1, P2> PartialEq<NonNullPointer<T, P2>> for NonNullPointer<T, P1>
where
	T: ?Sized,
	P1: Permission,
	P2: Permission,
{
	#[inline]
	#[expect(
		ambiguous_wide_pointer_comparisons,
		reason = "the standard library allows it"
	)]
	fn eq(&self, other: &NonNullPointer<T, P2>) -> bool {
		self.inner.eq(&other.inner)
	}
}

impl<T, P1, P2> PartialOrd<NonNullPointer<T, P2>> for NonNullPointer<T, P1>
where
	T: ?Sized,
	P1: Permission,
	P2: Permission,
{
	#[inline]
	#[expect(
		ambiguous_wide_pointer_comparisons,
		reason = "the standard library allows it"
	)]
	fn partial_cmp(
		&self,
		other: &NonNullPointer<T, P2>,
	) -> Option<cmp::Ordering> {
		self.inner.partial_cmp(&other.inner)
	}
}

impl<T> From<&T> for NonNullPointer<T, Shared> {
	#[inline]
	fn from(src: &T) -> Self {
		Self::from_nonnull(NonNull::from(src))
	}
}
impl<T> From<&mut T> for NonNullPointer<T, Unique> {
	#[inline]
	fn from(src: &mut T) -> Self {
		Self::from_nonnull(NonNull::from(src))
	}
}

impl<T, P> fmt::Debug for NonNullPointer<T, P>
where
	T: ?Sized,
	P: Permission,
{
	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
		write!(fmt, "NonNull<*{} {}>", P::NAME, any::type_name::<T>())?;
		fmt.debug_tuple("").field(&self.inner).finish()
	}
}

impl<T, P> fmt::Pointer for NonNullPointer<T, P>
where
	T: ?Sized,
	P: Permission,
{
	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
		fmt::Pointer::fmt(&self.as_ptr().into_raw_const(), fmt)
	}
}

impl<T, P> hash::Hash for NonNullPointer<T, P>
where
	T: ?Sized,
	P: Permission,
{
	#[inline]
	fn hash<H: hash::Hasher>(&self, state: &mut H) {
		self.inner.hash(state);
	}
}

impl<T, P> Copy for NonNullPointer<T, P>
where
	T: ?Sized,
	P: Permission,
{
}

// All pointers are !Send and !Sync, so no overrides may be done here.

// endregion TRAITS