use std::ops::BitOr;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct StatCategory {
pub raw_value: i64,
}
impl StatCategory {
pub const PROCESS: Self = Self::new(1 << 0);
pub const MEMORY: Self = Self::new(1 << 1);
pub const CPU: Self = Self::new(1 << 2);
pub const BLOCK_IO: Self = Self::new(1 << 3);
pub const NETWORK: Self = Self::new(1 << 4);
pub const MEMORY_EVENTS: Self = Self::new(1 << 5);
pub const FILESYSTEM: Self = Self::new(1 << 6);
pub const ALL: Self = Self::new(0x7f);
pub const fn new(raw_value: i64) -> Self {
Self { raw_value }
}
pub const fn contains(self, member: Self) -> bool {
self.raw_value & member.raw_value == member.raw_value
}
}
impl BitOr for StatCategory {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
Self::new(self.raw_value | rhs.raw_value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn all_is_every_category() {
let every = StatCategory::PROCESS
| StatCategory::MEMORY
| StatCategory::CPU
| StatCategory::BLOCK_IO
| StatCategory::NETWORK
| StatCategory::MEMORY_EVENTS
| StatCategory::FILESYSTEM;
assert_eq!(StatCategory::ALL, every);
assert!(StatCategory::ALL.contains(StatCategory::MEMORY_EVENTS));
assert!(!StatCategory::MEMORY.contains(StatCategory::ALL));
}
}