#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Debug)]
pub struct MemoryFlags {
bits: usize,
}
impl MemoryFlags {
pub const FREE: Self = Self { bits: 0b0000_0000 };
pub const RESERVE: Self = Self { bits: 0b0000_0001 };
pub const R: Self = Self { bits: 0b0000_0010 };
pub const W: Self = Self { bits: 0b0000_0100 };
pub const X: Self = Self { bits: 0b0000_1000 };
pub fn bits(&self) -> usize {
self.bits
}
pub fn from_bits(raw: usize) -> Option<MemoryFlags> {
if raw > 16 {
None
} else {
Some(MemoryFlags { bits: raw })
}
}
pub fn is_empty(&self) -> bool {
self.bits == 0
}
pub fn empty() -> MemoryFlags {
MemoryFlags { bits: 0 }
}
pub fn all() -> MemoryFlags {
MemoryFlags { bits: 15 }
}
}
impl core::fmt::Binary for MemoryFlags {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Binary::fmt(&self.bits, f)
}
}
impl core::fmt::Octal for MemoryFlags {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Octal::fmt(&self.bits, f)
}
}
impl core::fmt::LowerHex for MemoryFlags {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::LowerHex::fmt(&self.bits, f)
}
}
impl core::fmt::UpperHex for MemoryFlags {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::UpperHex::fmt(&self.bits, f)
}
}
impl core::ops::BitOr for MemoryFlags {
type Output = Self;
#[inline]
fn bitor(self, other: MemoryFlags) -> Self {
Self {
bits: self.bits | other.bits,
}
}
}
impl core::ops::BitOrAssign for MemoryFlags {
#[inline]
fn bitor_assign(&mut self, other: Self) {
self.bits |= other.bits;
}
}
impl core::ops::BitXor for MemoryFlags {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self {
Self {
bits: self.bits ^ other.bits,
}
}
}
impl core::ops::BitXorAssign for MemoryFlags {
#[inline]
fn bitxor_assign(&mut self, other: Self) {
self.bits ^= other.bits;
}
}
impl core::ops::BitAnd for MemoryFlags {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self {
Self {
bits: self.bits & other.bits,
}
}
}
impl core::ops::BitAndAssign for MemoryFlags {
#[inline]
fn bitand_assign(&mut self, other: Self) {
self.bits &= other.bits;
}
}
impl core::ops::Sub for MemoryFlags {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
Self {
bits: self.bits & !other.bits,
}
}
}
impl core::ops::SubAssign for MemoryFlags {
#[inline]
fn sub_assign(&mut self, other: Self) {
self.bits &= !other.bits;
}
}
impl core::ops::Not for MemoryFlags {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self { bits: !self.bits } & MemoryFlags { bits: 15 }
}
}