1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
//@ extern: bitflags
// `bitflags!` is lowered directly rather than expanded. Expanding it does
// not help: on a one-flag user it produces 869 lines that still call into
// `bitflags::{Bits, Flag, Flags, iter, parser}`, which are themselves 26
// further `macro_rules!` across five files. What the macro *means* is small
// and stable, so it is implemented; this case is what checks it, against
// bitflags 2.13.2 rather than against anyone's memory of it.
//
// Note `!rw` is complemented and masked to `all()`, and `from_bits` is None
// for any bit outside `all()`. Neither is what a reimplementation guesses.
use bitflags::bitflags;
bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Perms: u32 {
const READ = 0b0000_0001;
const WRITE = 0b0000_0010;
const EXEC = 0b0000_0100;
}
}
fn main() {
let r = Perms::READ;
let rw = Perms::READ | Perms::WRITE;
let all = Perms::all();
println!("bits {} {} {} {}", r.bits(), rw.bits(), all.bits(), Perms::empty().bits());
println!("contains {} {}", rw.contains(Perms::READ), rw.contains(Perms::EXEC));
println!("intersects {} {}", rw.intersects(Perms::EXEC), rw.intersects(Perms::READ));
println!("empty {} {}", rw.is_empty(), Perms::empty().is_empty());
println!("all {} {}", rw.is_all(), all.is_all());
println!("ops {} {} {} {}", (rw & Perms::WRITE).bits(), (rw | Perms::EXEC).bits(), (rw ^ Perms::READ).bits(), (!rw).bits());
println!("diff {} {}", all.difference(Perms::WRITE).bits(), rw.symmetric_difference(Perms::EXEC).bits());
println!("union {} inter {}", r.union(Perms::EXEC).bits(), all.intersection(rw).bits());
let mut m = Perms::empty();
m.insert(Perms::READ); m.insert(Perms::EXEC); m.remove(Perms::READ); m.toggle(Perms::WRITE);
println!("mut {}", m.bits());
m.set(Perms::EXEC, false);
println!("set {}", m.bits());
println!("from_bits {:?} {:?}", Perms::from_bits(3), Perms::from_bits(8));
println!("truncate {}", Perms::from_bits_truncate(9).bits());
println!("debug {:?} {:?} {:?}", r, rw, Perms::empty());
println!("eq {} {}", r == Perms::READ, r == rw);
}
|