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
use core::{
    cmp, fmt,
    hash::{Hash, Hasher},
    marker::PhantomData,
    ptr::{self, NonNull},
};

use crate::{TagNonNull, TagPtr};

/********** impl Clone ****************************************************************************/

impl<T, const N: usize> Clone for TagPtr<T, N> {
    impl_clone!();
}

/********** impl Copy *****************************************************************************/

impl<T, const N: usize> Copy for TagPtr<T, N> {}

/********** impl inherent *************************************************************************/

impl<T, const N: usize> TagPtr<T, N> {
    doc_comment! {
        doc_tag_bits!(),
        pub const TAG_BITS: usize = N;
    }

    doc_comment! {
        doc_tag_mask!(),
        pub const TAG_MASK: usize = crate::mark_mask(Self::TAG_BITS);
    }

    doc_comment! {
        doc_ptr_mask!(),
        pub const POINTER_MASK: usize = !Self::TAG_MASK;
    }

    doc_comment! {
        doc_null!(),
        ///
        /// # Examples
        ///
        /// ```
        /// use core::ptr;
        ///
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let ptr = TagPtr::null();
        /// assert_eq!(ptr.decompose(), (ptr::null_mut(), 0));
        /// ```
        #[inline]
        pub const fn null() -> Self {
            Self::new(ptr::null_mut())
        }
    }

    doc_comment! {
        doc_new!(),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let reference = &mut 1;
        /// let ptr = TagPtr::new(reference);
        /// assert_eq!(ptr.decompose(), (reference as *mut _, 0));
        /// ```
        #[inline]
        pub const fn new(ptr: *mut T) -> Self {
            Self { inner: ptr, _marker: PhantomData }
        }
    }

    doc_comment! {
        doc_from_usize!(),
        ///
        /// # Examples
        ///
        /// ```
        /// use core::ptr;
        ///
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let ptr = TagPtr::from_usize(0b11);
        /// assert_eq!(ptr.decompose(), (ptr::null_mut(), 0b11));
        /// ```
        #[inline]
        pub const fn from_usize(val: usize) -> Self {
            Self::new(val as _)
        }
    }

    doc_comment! {
        doc_into_raw!(),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let ptr = TagPtr::from_usize(0b11);
        /// assert_eq!(ptr.into_raw(), 0b11 as *mut _);
        /// ```
        #[inline]
        pub const fn into_raw(self) -> *mut T {
            self.inner
        }
    }

    doc_comment! {
        doc_cast!(),
        pub const fn cast<U>(self) -> TagPtr<U, N> {
            TagPtr { inner: self.inner.cast(), _marker: PhantomData }
        }
    }

    doc_comment! {
        doc_into_usize!(),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let ptr = TagPtr::from_usize(0b11);
        /// assert_eq!(ptr.into_usize(), 0b11);
        /// ```
        #[inline]
        pub fn into_usize(self) -> usize {
            self.inner as usize
        }
    }

    doc_comment! {
        doc_compose!(),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let raw = &1 as *const i32 as *mut i32;
        /// let ptr = TagPtr::compose(raw, 0b11);
        /// assert_eq!(ptr.decompose(), (raw, 0b11));
        /// // excess bits are silently truncated
        /// let ptr = TagPtr::compose(raw, 0b101);
        /// assert_eq!(ptr.decompose(), (raw, 0b01));
        /// ```
        #[inline]
        pub fn compose(ptr: *mut T, tag: usize) -> Self {
            Self::new(crate::compose::<T, N>(ptr, tag))
        }
    }

    /// Returns `true` if the marked pointer is `null`.
    ///
    /// # Examples
    ///
    /// ```
    /// use core::ptr;
    ///
    /// type TagPtr = tagptr::TagPtr<i32, 2>;
    ///
    /// let ptr = TagPtr::compose(ptr::null_mut(), 0b11);
    /// assert!(ptr.is_null());
    /// ```
    #[inline]
    pub fn is_null(self) -> bool {
        self.decompose_ptr().is_null()
    }

    doc_comment! {
        doc_clear_tag!(),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let reference = &mut 1;
        /// let ptr = TagPtr::compose(reference, 0b11);
        ///
        /// assert_eq!(ptr.clear_tag().decompose(), (reference as *mut _, 0));
        /// ```
        #[inline]
        pub fn clear_tag(self) -> Self {
            Self::new(self.decompose_ptr())
        }
    }

    doc_comment! {
        doc_split_tag!(),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let reference = &mut 1;
        /// let ptr = TagPtr::compose(reference, 0b11);
        ///
        /// assert_eq!(ptr.split_tag(), (TagPtr::new(reference), 0b11));
        /// ```
        #[inline]
        pub fn split_tag(self) -> (Self, usize) {
            let (ptr, tag) = self.decompose();
            (Self::new(ptr), tag)
        }
    }

    doc_comment! {
        doc_set_tag!(),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let reference = &mut 1;
        /// let ptr = TagPtr::compose(reference, 0b11);
        ///
        /// assert_eq!(ptr.set_tag(0b01).decompose(), (reference as *mut _, 0b01));
        /// ```
        #[inline]
        pub fn set_tag(self, tag: usize) -> Self {
            let ptr = self.decompose_ptr();
            Self::compose(ptr, tag)
        }
    }

    doc_comment! {
        doc_update_tag!(),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let reference = &mut 1;
        /// let ptr = TagPtr::compose(reference, 0b11);
        ///
        /// assert_eq!(ptr.update_tag(|tag| tag - 1).decompose(), (reference as *mut _, 0b10));
        /// ```
        #[inline]
        pub fn update_tag(self, func: impl FnOnce(usize) -> usize) -> Self {
            let (ptr, tag) = self.decompose();
            Self::compose(ptr, func(tag))
        }
    }

    doc_comment! {
        doc_add_tag!(),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let reference = &mut 1;
        /// let ptr = TagPtr::compose(reference, 0b10);
        ///
        /// assert_eq!(ptr.add_tag(1).decompose(), (reference as *mut _, 0b11));
        /// ```
        #[inline]
        pub fn add_tag(self, value: usize) -> Self {
            Self::from_usize(self.into_usize().wrapping_add(value))
        }
    }

    doc_comment! {
        doc_sub_tag!(),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let reference = &mut 1;
        /// let ptr = TagPtr::compose(reference, 0b10);
        ///
        /// assert_eq!(ptr.sub_tag(1).decompose(), (reference as *mut _, 0b01));
        /// ```
        #[inline]
        pub fn sub_tag(self, value: usize) -> Self {
            Self::from_usize(self.into_usize().wrapping_sub(value))
        }
    }

    doc_comment! {
        doc_decompose!(),
        #[inline]
        pub fn decompose(self) -> (*mut T, usize) {
            (self.decompose_ptr(), self.decompose_tag())
        }
    }

    doc_comment! {
        doc_decompose_ptr!(),
        #[inline]
        pub fn decompose_ptr(self) -> *mut T {
            crate::decompose_ptr::<T>(self.inner as usize, Self::TAG_BITS)
        }
    }

    doc_comment! {
        doc_decompose_tag!(),
        #[inline]
        pub fn decompose_tag(self) -> usize {
            crate::decompose_tag::<T>(self.inner as usize, Self::TAG_BITS)
        }
    }

    doc_comment! {
        doc_as_ref!("nullable"),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let reference = &1;
        /// let ptr = TagPtr::compose(reference as *const _ as *mut _, 0b11);
        ///
        /// unsafe {
        ///     assert_eq!(ptr.as_ref(), Some(&1));
        /// }
        /// ```
        #[inline]
        pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
            self.decompose_ptr().as_ref()
        }
    }

    doc_comment! {
        doc_as_mut!("nullable", TagPtr),
        ///
        /// # Examples
        ///
        /// ```
        /// type TagPtr = tagptr::TagPtr<i32, 2>;
        ///
        /// let mut val = 1;
        /// let ptr = TagPtr::compose(&mut val, 0b11);
        ///
        /// unsafe {
        ///     assert_eq!(ptr.as_mut(), Some(&mut 1));
        /// }
        /// ```
        #[inline]
        pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
            self.decompose_ptr().as_mut()
        }
    }

    /// Decomposes the marked pointer, returning an optional reference and the
    /// separated tag.
    ///
    /// # Safety
    ///
    /// The same safety caveats as with [`as_ref`][TagPtr::as_ref] apply.
    ///
    /// # Examples
    ///
    /// ```
    /// type TagPtr = tagptr::TagPtr<i32, 2>;
    ///
    /// let reference = &1;
    /// let ptr = TagPtr::compose(reference as *const _ as *mut _, 0b11);
    ///
    /// unsafe {
    ///     assert_eq!(ptr.decompose_ref(), (Some(&1), 0b11));
    /// }
    /// ```
    #[inline]
    pub unsafe fn decompose_ref<'a>(self) -> (Option<&'a T>, usize) {
        (self.as_ref(), self.decompose_tag())
    }

    /// Decomposes the marked pointer, returning an optional *mutable* reference
    /// and the separated tag.
    ///
    /// # Safety
    ///
    /// The same safety caveats as with [`as_mut`][TagPtr::as_mut] apply.
    ///
    /// # Examples
    ///
    /// ```
    /// type TagPtr = tagptr::TagPtr<i32, 2>;
    ///
    /// let mut val = 1;
    /// let ptr = TagPtr::compose(&mut val, 0b11);
    ///
    /// unsafe {
    ///     assert_eq!(ptr.decompose_mut(), (Some(&mut 1), 0b11));
    /// }
    /// ```
    #[inline]
    pub unsafe fn decompose_mut<'a>(self) -> (Option<&'a mut T>, usize) {
        (self.as_mut(), self.decompose_tag())
    }
}

/********** impl Debug ****************************************************************************/

impl<T, const N: usize> fmt::Debug for TagPtr<T, N> {
    impl_debug!("TagPtr");
}

/********** impl Default **************************************************************************/

impl<T, const N: usize> Default for TagPtr<T, N> {
    impl_default!();
}

/********** impl From (*mut T) ********************************************************************/

impl<T, const N: usize> From<*mut T> for TagPtr<T, N> {
    #[inline]
    fn from(ptr: *mut T) -> Self {
        Self::new(ptr)
    }
}

/********** impl From (*const T) ******************************************************************/

impl<T, const N: usize> From<*const T> for TagPtr<T, N> {
    #[inline]
    fn from(ptr: *const T) -> Self {
        Self::new(ptr as _)
    }
}

/********** impl From (&T) ************************************************************************/

impl<T, const N: usize> From<&T> for TagPtr<T, N> {
    #[inline]
    fn from(reference: &T) -> Self {
        Self::from(reference as *const _)
    }
}

/********** impl From ((&T, usize)) ***************************************************************/

impl<T, const N: usize> From<(&T, usize)> for TagPtr<T, N> {
    #[inline]
    fn from((reference, tag): (&T, usize)) -> Self {
        Self::compose(reference as *const T as *mut T, tag)
    }
}

/********** impl From (&mut T) ********************************************************************/

impl<T, const N: usize> From<&mut T> for TagPtr<T, N> {
    #[inline]
    fn from(reference: &mut T) -> Self {
        Self::from(reference as *const _)
    }
}

/********** impl From ((&mut T, usize)) ***********************************************************/

impl<T, const N: usize> From<(&mut T, usize)> for TagPtr<T, N> {
    #[inline]
    fn from((reference, tag): (&mut T, usize)) -> Self {
        Self::compose(reference, tag)
    }
}

/********** impl From (NonNull) *******************************************************************/

impl<T, const N: usize> From<NonNull<T>> for TagPtr<T, N> {
    #[inline]
    fn from(ptr: NonNull<T>) -> Self {
        Self::new(ptr.as_ptr())
    }
}

/********** impl From (TagNonNull) *************************************************************/

impl<T, const N: usize> From<TagNonNull<T, N>> for TagPtr<T, N> {
    #[inline]
    fn from(ptr: TagNonNull<T, N>) -> Self {
        ptr.into_marked_ptr()
    }
}

/********** impl PartialEq ************************************************************************/

impl<T, const N: usize> PartialEq for TagPtr<T, N> {
    impl_partial_eq!();
}

/********** impl PartialOrd ***********************************************************************/

impl<T, const N: usize> PartialOrd for TagPtr<T, N> {
    impl_partial_ord!();
}

/********** impl Pointer **************************************************************************/

impl<T, const N: usize> fmt::Pointer for TagPtr<T, N> {
    impl_pointer!();
}

/********** impl Eq *******************************************************************************/

impl<T, const N: usize> Eq for TagPtr<T, N> {}

/********** impl Ord ******************************************************************************/

impl<T, const N: usize> Ord for TagPtr<T, N> {
    impl_ord!();
}

/********** impl Hash *****************************************************************************/

impl<T, const N: usize> Hash for TagPtr<T, N> {
    impl_hash!();
}

#[cfg(test)]
mod tests {
    type TagPtr = crate::TagPtr<i32, 2>;

    #[test]
    fn test_debug() {
        let reference = &mut 1;
        let ptr = TagPtr::compose(reference, 0b11);
        assert_eq!(
            std::format!("{:?}", ptr),
            std::format!("TagPtr {{ ptr: {:0p}, tag: {} }}", reference as *mut _, 0b11)
        );
    }

    #[test]
    fn test_cast() {
        type ErasedPtr = crate::TagPtr<(), 2>;

        let reference = &mut 1;
        let ptr = TagPtr::compose(reference, 0b11);
        let cast: ErasedPtr = ptr.cast().set_tag(0b10);

        assert_eq!(cast.into_usize(), reference as *mut _ as usize | 0b10);
        assert_eq!(cast.cast(), TagPtr::compose(reference, 0b10));
    }

    #[test]
    fn test_from_usize() {
        let reference = &1;
        let ptr = TagPtr::from_usize(reference as *const i32 as usize | 0b1);
        assert_eq!(ptr.decompose(), (reference as *const _ as *mut _, 0b1));
    }

    #[test]
    fn test_compose() {
        let reference = &mut 1;
        let ptr1 = TagPtr::compose(reference, 0b11);
        let ptr2 = TagPtr::compose(reference, 0b111);
        // compose silently truncates excess bits, so ptr1 and ptr2 are identical
        assert_eq!(ptr1, ptr2);
        assert_eq!(ptr2.decompose(), (reference as *mut _, 0b11));
    }

    #[test]
    fn test_set_tag() {
        let reference = &mut 1;
        let ptr = TagPtr::compose(reference, 0b11);
        // set_tag must silently truncate excess tag bits
        assert_eq!(ptr, ptr.set_tag(0b111));
    }

    #[test]
    fn test_overflow_tag() {
        let reference = &mut 1;
        let ptr = TagPtr::compose(reference, 0b11);

        // add must cause overflow (corrupt the pointer)
        assert_eq!(ptr.add_tag(1).into_usize(), reference as *mut _ as usize + 0b11 + 1);
        // update must only overflow the tag bits
        assert_eq!(ptr.update_tag(|tag| tag + 1).decompose(), (reference as *mut _, 0));
    }

    #[test]
    fn test_underflow_tag() {
        let reference = &mut 1;
        let ptr = TagPtr::new(reference);

        // sub_tag must underflow the entire pointer
        assert_eq!(ptr.sub_tag(1).into_usize(), reference as *mut _ as usize - 1);
        // update_tag must only underflow the tag value
        assert_eq!(
            ptr.update_tag(|tag| tag.wrapping_sub(1)).decompose(),
            (reference as *mut _, 0b11)
        );
    }

    #[test]
    fn test_erase() {
        #[repr(align(64))]
        struct Aligned64(i32);

        let reference = &Aligned64(1);
        let ptr = crate::TagPtr::<Aligned64, 6>::from((reference, 55));
        let mut erased: crate::TagPtr<(), 6> = ptr.cast();
        erased = erased.update_tag(|tag| tag + 3);
        let ptr: crate::TagPtr<Aligned64, 6> = erased.cast();

        assert_eq!(ptr.decompose(), (reference as *const _ as *mut _, 58));
    }
}