use crate::object_tag::ObjectTag; use crate::types::{Generation, ObjectId}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Rights(u16); impl Rights { #[allow(dead_code)] pub const NONE: Self = Self(0); pub const READ: Self = Self(1 << 0); pub const WRITE: Self = Self(1 << 1); pub const GRANT: Self = Self(1 << 2); pub const REVOKE: Self = Self(1 << 3); pub const ALL: Self = Self(0b1111); pub const fn bits(self) -> u16 { self.0 } pub const fn contains(self, other: Self) -> bool { (self.0 & other.0) == other.0 } pub const fn from_bits(bits: u16) -> Self { Self(bits & Self::ALL.0) } } impl core::ops::BitOr for Rights { type Output = Self; fn bitor(self, rhs: Self) -> Self { Self(self.0 | rhs.0) } } impl core::ops::BitAnd for Rights { type Output = Self; fn bitand(self, rhs: Self) -> Self { Self(self.0 & rhs.0) } } impl core::ops::Not for Rights { type Output = Self; fn not(self) -> Self { Self(!self.0 & Self::ALL.0) } } impl core::ops::BitOrAssign for Rights { fn bitor_assign(&mut self, rhs: Self) { self.0 |= rhs.0; } } impl core::ops::BitAndAssign for Rights { fn bitand_assign(&mut self, rhs: Self) { self.0 &= rhs.0; } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct CapRef { tag: ObjectTag, object_id: ObjectId, rights: Rights, generation: Generation, } impl CapRef { pub const fn new( tag: ObjectTag, object_id: ObjectId, rights: Rights, generation: Generation, ) -> Self { Self { tag, object_id, rights, generation, } } pub const fn tag(&self) -> ObjectTag { self.tag } pub const fn object_id(&self) -> ObjectId { self.object_id } pub const fn rights(&self) -> Rights { self.rights } pub const fn generation(&self) -> Generation { self.generation } pub const fn with_rights(self, rights: Rights) -> Self { Self { rights, ..self } } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum CapSlot { Empty, Active(CapRef), } impl CapSlot { pub const fn is_empty(&self) -> bool { match self { Self::Empty => true, Self::Active(_) => false, } } }