Skip to main content

oxihuman_core/
capability_flags.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3#![allow(dead_code)]
4
5pub struct CapabilityFlags {
6    pub bits: u64,
7}
8
9impl CapabilityFlags {
10    pub fn new() -> Self {
11        CapabilityFlags { bits: 0 }
12    }
13}
14
15impl Default for CapabilityFlags {
16    fn default() -> Self {
17        Self::new()
18    }
19}
20
21pub fn new_capability_flags() -> CapabilityFlags {
22    CapabilityFlags::new()
23}
24
25pub fn flags_set(f: &mut CapabilityFlags, bit: u64) {
26    f.bits |= bit;
27}
28
29pub fn flags_clear(f: &mut CapabilityFlags, bit: u64) {
30    f.bits &= !bit;
31}
32
33pub fn flags_test(f: &CapabilityFlags, bit: u64) -> bool {
34    f.bits & bit != 0
35}
36
37pub fn flags_any(f: &CapabilityFlags) -> bool {
38    f.bits != 0
39}
40
41pub fn flags_all(f: &CapabilityFlags, mask: u64) -> bool {
42    f.bits & mask == mask
43}
44
45pub fn flags_count(f: &CapabilityFlags) -> u32 {
46    f.bits.count_ones()
47}
48
49pub fn flags_reset(f: &mut CapabilityFlags) {
50    f.bits = 0;
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_new_zero() {
59        /* new flags are all zero */
60        let f = new_capability_flags();
61        assert!(!flags_any(&f));
62        assert_eq!(flags_count(&f), 0);
63    }
64
65    #[test]
66    fn test_set_and_test() {
67        /* set then test a bit */
68        let mut f = new_capability_flags();
69        flags_set(&mut f, 0b0100);
70        assert!(flags_test(&f, 0b0100));
71        assert!(!flags_test(&f, 0b0010));
72    }
73
74    #[test]
75    fn test_clear() {
76        /* clear removes a specific bit */
77        let mut f = new_capability_flags();
78        flags_set(&mut f, 0b1111);
79        flags_clear(&mut f, 0b0010);
80        assert!(flags_test(&f, 0b0001));
81        assert!(!flags_test(&f, 0b0010));
82    }
83
84    #[test]
85    fn test_flags_any() {
86        /* flags_any detects any set bit */
87        let mut f = new_capability_flags();
88        assert!(!flags_any(&f));
89        flags_set(&mut f, 1);
90        assert!(flags_any(&f));
91    }
92
93    #[test]
94    fn test_flags_all() {
95        /* flags_all checks all mask bits */
96        let mut f = new_capability_flags();
97        flags_set(&mut f, 0b0011);
98        assert!(flags_all(&f, 0b0011));
99        assert!(!flags_all(&f, 0b0111));
100    }
101
102    #[test]
103    fn test_flags_count() {
104        /* count set bits */
105        let mut f = new_capability_flags();
106        flags_set(&mut f, 0b1011);
107        assert_eq!(flags_count(&f), 3);
108    }
109
110    #[test]
111    fn test_reset() {
112        /* reset clears all bits */
113        let mut f = new_capability_flags();
114        flags_set(&mut f, u64::MAX);
115        flags_reset(&mut f);
116        assert!(!flags_any(&f));
117    }
118
119    #[test]
120    fn test_default() {
121        /* Default impl works */
122        let f = CapabilityFlags::default();
123        assert_eq!(f.bits, 0);
124    }
125}