crystal_api/
bitflags.rs

1use std::{
2    fmt::Debug,
3    ops::{BitAnd, BitOr, BitXor},
4};
5
6macro_rules! wrap_bit_ops {
7    ($bitstruct:tt) => {
8        impl BitAnd for $bitstruct {
9            type Output = Self;
10            fn bitand(self, rhs: Self) -> Self::Output {
11                Self(self.0 & rhs.0)
12            }
13        }
14
15        impl BitOr for $bitstruct {
16            type Output = Self;
17            fn bitor(self, rhs: Self) -> Self::Output {
18                Self(self.0 | rhs.0)
19            }
20        }
21
22        impl BitXor for $bitstruct {
23            type Output = Self;
24            fn bitxor(self, rhs: Self) -> Self::Output {
25                Self(self.0 ^ rhs.0)
26            }
27        }
28
29        impl PartialEq for $bitstruct {
30            fn eq(&self, other: &Self) -> bool {
31                self.0 == other.0
32            }
33        }
34
35        impl Eq for $bitstruct {}
36
37        impl Debug for $bitstruct {
38            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39                f.write_str(format!("{:b}", self.0).as_str())
40            }
41        }
42
43        impl $bitstruct {
44            /// Returns true if all bits are zero
45            pub fn is_none(&self) -> bool {
46                self.0 == 0
47            }
48
49            /// Creates zeroed bitflag
50            pub fn null() -> Self {
51                Self(0)
52            }
53        }
54    };
55}
56
57/// Buffer flags used to specify buffer usage
58#[repr(transparent)]
59#[derive(Clone, Copy)]
60pub struct BufferFlags(u8);
61wrap_bit_ops!(BufferFlags);
62
63impl BufferFlags {
64    /// Uniform buffers are used to store small amount of data, non indexing
65    pub const UNIFORM: Self = Self(0b001);
66    /// Transfer buffers are used to read data from it.
67    /// For example, computation result.
68    pub const TRANSFER: Self = Self(0b010);
69    /// Indicates that the buffer would be synced along the frame
70    pub const SYNCED: Self = Self(0b100);
71}