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
use crate::tag::{read_tag, set_tag, strip, Tag};
use std::marker::PhantomData;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct Shared<'shield, V, T>
where
    V: 'shield,
    T: Tag,
{
    pub(crate) data: usize,
    _m0: PhantomData<&'shield ()>,
    _m1: PhantomData<V>,
    _m2: PhantomData<T>,
}

impl<'shield, V, T> Shared<'shield, V, T>
where
    V: 'shield,
    T: Tag,
{
    pub fn null() -> Self {
        unsafe { Self::from_raw(0) }
    }

    /// # Safety
    /// The alignment of `V` must free up sufficient low bits so that `T` fits.
    pub unsafe fn from_ptr(ptr: *mut V) -> Self {
        Self::from_raw(ptr as usize)
    }

    /// This function constructs a `Shared<'shield, V, T>` from a raw tagged pointer.
    ///
    /// # Safety
    /// This is marked unsafe because extreme caution must be taken to
    /// supply correct data and ensure the lifetime is what you expect.
    pub unsafe fn from_raw(data: usize) -> Self {
        Self {
            data,
            _m0: PhantomData,
            _m1: PhantomData,
            _m2: PhantomData,
        }
    }

    pub fn into_raw(self) -> usize {
        self.data
    }

    pub fn as_ptr(&self) -> *mut V {
        strip::<T>(self.data) as *mut V
    }

    /// # Safety
    /// - The pointer must either be null or point to a valid instance of `V`.
    /// - You must ensure the instance of `V` is not borrowed mutably.
    pub unsafe fn as_ref(&self) -> Option<&'shield V> {
        self.as_ptr().as_ref()
    }

    /// # Safety
    /// - The pointer must either be null or point to a valid instance of `V`.
    /// - You must ensure the instance of `V` is not borrowed.
    pub unsafe fn as_mut_ref(&mut self) -> Option<&'shield mut V> {
        let ptr = self.as_ptr();

        if !ptr.is_null() {
            Some(&mut *ptr)
        } else {
            None
        }
    }

    /// # Safety
    /// - The pointer must point to a valid instance of `V`.
    /// - You must ensure the instance of `V` is not borrowed mutably.
    pub unsafe fn as_ref_unchecked(&self) -> &'shield V {
        &*self.as_ptr()
    }

    /// # Safety
    /// - The pointer must point to a valid instance of `V`.
    /// - You must ensure the instance of `V` is not borrowed.
    pub unsafe fn as_mut_ref_unchecked(&mut self) -> &'shield mut V {
        &mut *self.as_ptr()
    }

    pub fn is_null(&self) -> bool {
        self.as_ptr().is_null()
    }

    pub fn tag(&self) -> T {
        let bits = read_tag::<T>(self.data);
        Tag::deserialize(bits)
    }

    pub fn with_tag(&self, tag: T) -> Self {
        let bits = tag.serialize();
        let data = set_tag::<T>(self.data, bits);
        unsafe { Self::from_raw(data) }
    }
}