senses are now bitflags
This commit is contained in:
52
src/util/bitflags.rs
Normal file
52
src/util/bitflags.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
macro_rules! bitflags {
|
||||
($enum:ident, $struct:ident, $mod:ident {$($val:expr; $ename:ident, $sname:ident,)*}) => {
|
||||
#[repr(u32)]
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub enum $enum {
|
||||
$($ename = $val,)*
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub struct $struct(u32);
|
||||
#[allow(non_upper_case_globals)]
|
||||
impl $struct {
|
||||
$(pub const $sname: Self = Self($enum::$ename as u32);)*
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = $enum> {
|
||||
$crate::util::Biterator::new(self.0).map(|v| unsafe {std::mem::transmute(v)})
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::BitOr for $struct {
|
||||
type Output = Self;
|
||||
fn bitor(self, rhs: $struct) -> Self {
|
||||
Self(self.0 | rhs.0)
|
||||
}
|
||||
}
|
||||
pub mod $mod {
|
||||
use super::*;
|
||||
$(pub const $sname: $struct = $struct::$sname;)*
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use bitflags;
|
||||
|
||||
pub struct Biterator(u32);
|
||||
|
||||
impl Iterator for Biterator {
|
||||
type Item = u32;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.0 == 0 {
|
||||
return None;
|
||||
}
|
||||
let val = 1 << self.0.trailing_zeros();
|
||||
self.0 &= !val;
|
||||
Some(val)
|
||||
}
|
||||
}
|
||||
|
||||
impl Biterator {
|
||||
pub fn new(val: u32) -> Self {
|
||||
Self(val)
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
mod id;
|
||||
mod math;
|
||||
mod refcount;
|
||||
mod bitflags;
|
||||
|
||||
pub(crate) use id::*;
|
||||
pub(crate) use math::*;
|
||||
pub(crate) use refcount::*;
|
||||
pub(crate) use bitflags::*;
|
||||
|
||||
pub type HashMap<K, V> = std::collections::HashMap<K, V>;
|
||||
pub type HashSet<K> = std::collections::HashSet<K>;
|
||||
|
||||
Reference in New Issue
Block a user