Nothing to see here, move along
at main 120 lines 2.4 kB view raw
1use crate::object_tag::ObjectTag; 2use crate::types::{Generation, ObjectId}; 3 4#[derive(Debug, Clone, Copy, PartialEq, Eq)] 5pub struct Rights(u16); 6 7impl Rights { 8 #[allow(dead_code)] 9 pub const NONE: Self = Self(0); 10 pub const READ: Self = Self(1 << 0); 11 pub const WRITE: Self = Self(1 << 1); 12 pub const GRANT: Self = Self(1 << 2); 13 pub const REVOKE: Self = Self(1 << 3); 14 pub const ALL: Self = Self(0b1111); 15 16 pub const fn bits(self) -> u16 { 17 self.0 18 } 19 20 pub const fn contains(self, other: Self) -> bool { 21 (self.0 & other.0) == other.0 22 } 23 24 pub const fn from_bits(bits: u16) -> Self { 25 Self(bits & Self::ALL.0) 26 } 27} 28 29impl core::ops::BitOr for Rights { 30 type Output = Self; 31 fn bitor(self, rhs: Self) -> Self { 32 Self(self.0 | rhs.0) 33 } 34} 35 36impl core::ops::BitAnd for Rights { 37 type Output = Self; 38 fn bitand(self, rhs: Self) -> Self { 39 Self(self.0 & rhs.0) 40 } 41} 42 43impl core::ops::Not for Rights { 44 type Output = Self; 45 fn not(self) -> Self { 46 Self(!self.0 & Self::ALL.0) 47 } 48} 49 50impl core::ops::BitOrAssign for Rights { 51 fn bitor_assign(&mut self, rhs: Self) { 52 self.0 |= rhs.0; 53 } 54} 55 56impl core::ops::BitAndAssign for Rights { 57 fn bitand_assign(&mut self, rhs: Self) { 58 self.0 &= rhs.0; 59 } 60} 61 62#[derive(Debug, Clone, Copy, PartialEq, Eq)] 63pub struct CapRef { 64 tag: ObjectTag, 65 object_id: ObjectId, 66 rights: Rights, 67 generation: Generation, 68} 69 70impl CapRef { 71 pub const fn new( 72 tag: ObjectTag, 73 object_id: ObjectId, 74 rights: Rights, 75 generation: Generation, 76 ) -> Self { 77 Self { 78 tag, 79 object_id, 80 rights, 81 generation, 82 } 83 } 84 85 pub const fn tag(&self) -> ObjectTag { 86 self.tag 87 } 88 89 pub const fn object_id(&self) -> ObjectId { 90 self.object_id 91 } 92 93 pub const fn rights(&self) -> Rights { 94 self.rights 95 } 96 97 pub const fn generation(&self) -> Generation { 98 self.generation 99 } 100 101 pub const fn with_rights(self, rights: Rights) -> Self { 102 Self { rights, ..self } 103 } 104} 105 106#[derive(Debug, Clone, Copy, PartialEq, Eq)] 107pub enum CapSlot { 108 Empty, 109 Active(CapRef), 110} 111 112impl CapSlot { 113 pub const fn is_empty(&self) -> bool { 114 match self { 115 Self::Empty => true, 116 Self::Active(_) => false, 117 } 118 } 119} 120