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
use std::fmt;

use crate::borrow_tracker::BorTag;

/// An item in the per-location borrow stack.
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct Item(u64);

// An Item contains 3 bitfields:
// * Bits 0-61 store a BorTag
// * Bits 61-63 store a Permission
// * Bit 64 stores a flag which indicates if we have a protector
const TAG_MASK: u64 = u64::MAX >> 3;
const PERM_MASK: u64 = 0x3 << 61;
const PROTECTED_MASK: u64 = 0x1 << 63;

const PERM_SHIFT: u64 = 61;
const PROTECTED_SHIFT: u64 = 63;

impl Item {
    pub fn new(tag: BorTag, perm: Permission, protected: bool) -> Self {
        assert!(tag.get() <= TAG_MASK);
        let packed_tag = tag.get();
        let packed_perm = perm.to_bits() << PERM_SHIFT;
        let packed_protected = u64::from(protected) << PROTECTED_SHIFT;

        let new = Self(packed_tag | packed_perm | packed_protected);

        debug_assert!(new.tag() == tag);
        debug_assert!(new.perm() == perm);
        debug_assert!(new.protected() == protected);

        new
    }

    /// The pointers the permission is granted to.
    pub fn tag(self) -> BorTag {
        BorTag::new(self.0 & TAG_MASK).unwrap()
    }

    /// The permission this item grants.
    pub fn perm(self) -> Permission {
        Permission::from_bits((self.0 & PERM_MASK) >> PERM_SHIFT)
    }

    /// Whether or not there is a protector for this tag
    pub fn protected(self) -> bool {
        self.0 & PROTECTED_MASK > 0
    }

    /// Set the Permission stored in this Item
    pub fn set_permission(&mut self, perm: Permission) {
        // Clear the current set permission
        self.0 &= !PERM_MASK;
        // Write Permission::Disabled to the Permission bits
        self.0 |= perm.to_bits() << PERM_SHIFT;
    }
}

impl fmt::Debug for Item {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{:?} for {:?}]", self.perm(), self.tag())
    }
}

/// Indicates which permission is granted (by this item to some pointers)
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum Permission {
    /// Grants unique mutable access.
    Unique,
    /// Grants shared mutable access.
    SharedReadWrite,
    /// Grants shared read-only access.
    SharedReadOnly,
    /// Grants no access, but separates two groups of SharedReadWrite so they are not
    /// all considered mutually compatible.
    Disabled,
}

impl Permission {
    const UNIQUE: u64 = 0;
    const SHARED_READ_WRITE: u64 = 1;
    const SHARED_READ_ONLY: u64 = 2;
    const DISABLED: u64 = 3;

    fn to_bits(self) -> u64 {
        match self {
            Permission::Unique => Self::UNIQUE,
            Permission::SharedReadWrite => Self::SHARED_READ_WRITE,
            Permission::SharedReadOnly => Self::SHARED_READ_ONLY,
            Permission::Disabled => Self::DISABLED,
        }
    }

    fn from_bits(perm: u64) -> Self {
        match perm {
            Self::UNIQUE => Permission::Unique,
            Self::SHARED_READ_WRITE => Permission::SharedReadWrite,
            Self::SHARED_READ_ONLY => Permission::SharedReadOnly,
            Self::DISABLED => Permission::Disabled,
            _ => unreachable!(),
        }
    }
}