From 9780724126ee1feb360c4d7f0ebc6f71cbc31ed8 Mon Sep 17 00:00:00 2001 From: shadow cat Date: Mon, 25 Aug 2025 22:36:38 -0400 Subject: [PATCH] senses are now bitflags --- Cargo.lock | 80 ++++++++++++++++++++++---------------------- TODO | 1 - src/core/sense.rs | 18 +++++----- src/layout/sense.rs | 56 +++++++++++++++---------------- src/testing/mod.rs | 45 +++++++++++-------------- src/util/bitflags.rs | 52 ++++++++++++++++++++++++++++ src/util/mod.rs | 2 ++ 7 files changed, 150 insertions(+), 104 deletions(-) create mode 100644 src/util/bitflags.rs diff --git a/Cargo.lock b/Cargo.lock index 3d1b66e..587ba29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -53,7 +53,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef6978589202a00cd7e118380c448a08b6ed394c3a8df3a430d0898e3a42d046" dependencies = [ "android-properties", - "bitflags 2.9.1", + "bitflags 2.9.3", "cc", "cesu8", "jni", @@ -196,9 +196,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.9.1" +version = "2.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" +checksum = "34efbcccd345379ca2868b2b2c9d3782e9cc58ba87bc7d79d5b53d9c9ae6f25d" dependencies = [ "serde", ] @@ -274,7 +274,7 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b99da2f8558ca23c71f4fd15dc57c906239752dd27ff3c00a1d56b685b7cbfec" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "log", "polling", "rustix 0.38.44", @@ -425,7 +425,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d44a101f213f6c4cdc1853d4b78aef6db6bdfa3468798cc1d9912f4735013eb" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "core-foundation 0.10.1", "libc", ] @@ -436,7 +436,7 @@ version = "0.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da46a9d5a8905cc538a4a5bceb6a4510de7a51049c5588c0114efce102bcbbe8" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "fontdb", "log", "rangemap", @@ -757,7 +757,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbcd2dba93594b227a1f57ee09b8b9da8892c34d55aa332e034a228d0fe6a171" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "gpu-alloc-types", ] @@ -767,7 +767,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98ff03b468aa837d70984d55f5d3f846f6ec31fe34bbb97c4f85219caeee1ca4" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", ] [[package]] @@ -788,7 +788,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b89c83349105e3732062a895becfc71a8f921bb71ecbbdd8ff99263e3b53a0ca" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "gpu-descriptor-types", "hashbrown", ] @@ -799,7 +799,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fdf242682df893b86f33a73828fb09ca4b2d3bb6cc95249707fc684d27484b91" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", ] [[package]] @@ -1018,7 +1018,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "391290121bad3d37fbddad76d8f5d1c1c314cfc646d143d7e07a3086ddff0ce3" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "libc", "redox_syscall 0.5.17", ] @@ -1106,7 +1106,7 @@ version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00c15a6f673ff72ddcc22394663290f870fb224c1bfce55734a75c414150e605" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "block", "core-graphics-types 0.2.0", "foreign-types", @@ -1139,7 +1139,7 @@ checksum = "916cbc7cb27db60be930a4e2da243cf4bc39569195f22fd8ee419cd31d5b662c" dependencies = [ "arrayvec", "bit-set", - "bitflags 2.9.1", + "bitflags 2.9.3", "cfg-if", "cfg_aliases", "codespan-reporting", @@ -1163,7 +1163,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3f42e7bbe13d351b6bead8286a43aac9534b82bd3cc43e47037f012ebfd62d4" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "jni-sys", "log", "ndk-sys", @@ -1313,7 +1313,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4e89ad9e3d7d297152b17d39ed92cd50ca8063a89a9fa569046d41568891eff" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "block2", "libc", "objc2", @@ -1329,7 +1329,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74dd3b56391c7a0596a295029734d3c1c5e7e510a4cb30245f8221ccea96b009" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "block2", "objc2", "objc2-core-location", @@ -1353,7 +1353,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "617fbf49e071c178c0b24c080767db52958f716d9eabdf0890523aeae54773ef" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "block2", "objc2", "objc2-foundation", @@ -1395,7 +1395,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee638a5da3799329310ad4cfa62fbf045d5f56e3ef5ba4149e7452dcf89d5a8" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "block2", "dispatch", "libc", @@ -1420,7 +1420,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd0cba1276f6023976a406a14ffa85e1fdd19df6b0f737b063b95f6c8c7aadd6" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "block2", "objc2", "objc2-foundation", @@ -1432,7 +1432,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e42bee7bff906b14b167da2bac5efe6b6a07e6f7c0a21a7308d40c960242dc7a" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "block2", "objc2", "objc2-foundation", @@ -1455,7 +1455,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8bb46798b20cd6b91cbd113524c490f1686f4c4e8f49502431415f3512e2b6f" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "block2", "objc2", "objc2-cloud-kit", @@ -1487,7 +1487,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76cfcbf642358e8689af64cee815d139339f3ed8ad05103ed5eaf73db8d84cb3" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "block2", "objc2", "objc2-core-location", @@ -1876,7 +1876,7 @@ version = "0.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5407465600fb0548f1442edf71dd20683c6ed326200ace4b1ef0763521bb3b77" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", ] [[package]] @@ -1909,7 +1909,7 @@ version = "0.38.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "errno", "libc", "linux-raw-sys 0.4.15", @@ -1922,7 +1922,7 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11181fbabf243db407ef8df94a6ce0b2f9a733bd8be4ad02b4eda9602296cac8" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "errno", "libc", "linux-raw-sys 0.9.4", @@ -1941,7 +1941,7 @@ version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfb9cf8877777222e4a3bc7eb247e398b56baba500c38c1c46842431adc8b55c" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "bytemuck", "libm", "smallvec", @@ -2079,7 +2079,7 @@ version = "0.19.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3457dea1f0eb631b4034d61d4d8c32074caa6cd1ab2d59f2327bd8461e2c0016" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "calloop", "calloop-wayland-source", "cursor-icon", @@ -2113,7 +2113,7 @@ version = "0.3.0+sdk-1.3.268.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eda41003dc44290527a59b13432d4a0379379fa074b70174882adfbdfd917844" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", ] [[package]] @@ -2551,7 +2551,7 @@ version = "0.31.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c66a47e840dc20793f2264eb4b3e4ecb4b75d91c0dd4af04b456128e0bdd449d" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "rustix 1.0.8", "wayland-backend", "wayland-scanner", @@ -2563,7 +2563,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "625c5029dbd43d25e6aa9615e88b829a5cad13b2819c4ae129fdbb7c31ab4c7e" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "cursor-icon", "wayland-backend", ] @@ -2585,7 +2585,7 @@ version = "0.32.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efa790ed75fbfd71283bd2521a1cfdc022aabcc28bdcff00851f9e4ae88d9901" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "wayland-backend", "wayland-client", "wayland-scanner", @@ -2597,7 +2597,7 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a07a14257c077ab3279987c4f8bb987851bf57081b93710381daea94f2c2c032" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "wayland-backend", "wayland-client", "wayland-protocols", @@ -2610,7 +2610,7 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efd94963ed43cf9938a090ca4f7da58eb55325ec8200c3848963e98dc25b78ec" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "wayland-backend", "wayland-client", "wayland-protocols", @@ -2673,7 +2673,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70b6ff82bbf6e9206828e1a3178e851f8c20f1c9028e74dd3a8090741ccd5798" dependencies = [ "arrayvec", - "bitflags 2.9.1", + "bitflags 2.9.3", "cfg-if", "cfg_aliases", "document-features", @@ -2704,7 +2704,7 @@ dependencies = [ "arrayvec", "bit-set", "bit-vec", - "bitflags 2.9.1", + "bitflags 2.9.3", "cfg_aliases", "document-features", "hashbrown", @@ -2763,7 +2763,7 @@ dependencies = [ "arrayvec", "ash", "bit-set", - "bitflags 2.9.1", + "bitflags 2.9.3", "block", "bytemuck", "cfg-if", @@ -2807,7 +2807,7 @@ version = "26.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eca7a8d8af57c18f57d393601a1fb159ace8b2328f1b6b5f80893f7d672c9ae2" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "bytemuck", "js-sys", "log", @@ -3182,7 +3182,7 @@ dependencies = [ "ahash", "android-activity", "atomic-waker", - "bitflags 2.9.1", + "bitflags 2.9.3", "block2", "bytemuck", "calloop", @@ -3240,7 +3240,7 @@ version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", ] [[package]] @@ -3287,7 +3287,7 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d039de8032a9a8856a6be89cea3e5d12fdd82306ab7c94d74e6deab2460651c5" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.3", "dlib", "log", "once_cell", diff --git a/TODO b/TODO index 461987c..64d4a37 100644 --- a/TODO +++ b/TODO @@ -3,4 +3,3 @@ images text on update for text widget (then don't need to store textures in active data) abstract sensors to work with any event, maybe associate data as well? -make current senses orable so you can select multiple diff --git a/src/core/sense.rs b/src/core/sense.rs index 5a064a0..4c4f082 100644 --- a/src/core/sense.rs +++ b/src/core/sense.rs @@ -1,27 +1,27 @@ use crate::prelude::*; pub trait Sensable { - fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W); + fn on(self, sense: Senses, f: impl SenseFn) -> WidgetIdFnRet!(W); fn id_on( self, - sense: Sense, + senses: Senses, f: impl FnMut(&WidgetId, &mut Ui) + 'static + Clone, ) -> WidgetIdFnRet!(W) where W: Widget; - fn edit_on(self, sense: Sense, f: impl FnMut(&mut W) + 'static + Clone) -> WidgetIdFnRet!(W) + fn edit_on(self, senses: Senses, f: impl FnMut(&mut W) + 'static + Clone) -> WidgetIdFnRet!(W) where W: Widget; } impl, Tag> Sensable for W { - fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W::Widget) { + fn on(self, senses: Senses, f: impl SenseFn) -> WidgetIdFnRet!(W::Widget) { move |ui| { let id = self.add(ui); ui.add_sensor( &id, Sensor { - sense, + senses, f: Box::new(f), }, ); @@ -30,7 +30,7 @@ impl, Tag> Sensable for W { } fn id_on( self, - sense: Sense, + senses: Senses, mut f: impl FnMut(&WidgetId, &mut Ui) + 'static + Clone, ) -> WidgetIdFnRet!(W::Widget) where @@ -38,17 +38,17 @@ impl, Tag> Sensable for W { { self.with_id(move |ui, id| { let id2 = id.clone(); - ui.add(id.on(sense, move |ui| f(&id2, ui))) + ui.add(id.on(senses, move |ui| f(&id2, ui))) }) } fn edit_on( self, - sense: Sense, + senses: Senses, mut f: impl FnMut(&mut W::Widget) + 'static + Clone, ) -> WidgetIdFnRet!(W::Widget) where W::Widget: Widget, { - self.id_on(sense, move |id, ui| f(&mut ui[id])) + self.id_on(senses, move |id, ui| f(&mut ui[id])) } } diff --git a/src/layout/sense.rs b/src/layout/sense.rs index 94c3e7e..272cee2 100644 --- a/src/layout/sense.rs +++ b/src/layout/sense.rs @@ -1,19 +1,17 @@ use crate::{ layout::{Ui, UiRegion, Vec2, WidgetId}, - util::HashMap, - util::Id, + util::{HashMap, Id, bitflags}, }; -#[derive(Clone, Copy, PartialEq)] -pub enum Sense { - PressStart, - Pressing, - PressEnd, - HoverStart, - Hovering, - HoverEnd, - NotHovering, -} +bitflags!(Sense, Senses, senses { + 1 << 0; PressStart, PRESS_START, + 1 << 1; Pressing, PRESSING, + 1 << 2; PressEnd, PRESS_END, + 1 << 3; HoverStart, HOVER_START, + 1 << 4; Hovering, HOVERING, + 1 << 5; HoverEnd, HOVER_END, + 1 << 6; NotHovering, NOT_HOVERING, +}); pub struct CursorState { pub pos: Vec2, @@ -31,18 +29,13 @@ pub enum ActivationState { } pub struct Sensor { - pub sense: Sense, + pub senses: Senses, pub f: Box, } pub type SensorMap = HashMap; pub type ActiveSensors = HashMap; pub type SenseShape = UiRegion; -#[derive(Clone)] -pub struct SenseTrigger { - pub shape: SenseShape, - pub sense: Sense, -} #[derive(Default)] pub struct SensorGroup { pub hover: ActivationState, @@ -78,7 +71,7 @@ impl Ui { group.cursor.update(cursor.pressed && in_shape); for sensor in &mut group.sensors { - if should_run(sensor.sense, group.cursor, group.hover) { + if should_run(sensor.senses, group.cursor, group.hover) { (sensor.f.box_clone())(self); } } @@ -88,16 +81,21 @@ impl Ui { } } -pub fn should_run(sense: Sense, cursor: ActivationState, hover: ActivationState) -> bool { - match sense { - Sense::PressStart => cursor.is_start(), - Sense::Pressing => cursor.is_on(), - Sense::PressEnd => cursor.is_end(), - Sense::HoverStart => hover.is_start(), - Sense::Hovering => hover.is_on(), - Sense::HoverEnd => hover.is_end(), - Sense::NotHovering => hover.is_off(), +pub fn should_run(senses: Senses, cursor: ActivationState, hover: ActivationState) -> bool { + for sense in senses.iter() { + if match sense { + Sense::PressStart => cursor.is_start(), + Sense::Pressing => cursor.is_on(), + Sense::PressEnd => cursor.is_end(), + Sense::HoverStart => hover.is_start(), + Sense::Hovering => hover.is_on(), + Sense::HoverEnd => hover.is_end(), + Sense::NotHovering => hover.is_off(), + } { + return true; + } } + false } impl ActivationState { @@ -147,7 +145,7 @@ impl Clone for SensorGroup { impl Clone for Sensor { fn clone(&self) -> Self { Self { - sense: self.sense, + senses: self.senses, f: self.f.box_clone(), } } diff --git a/src/testing/mod.rs b/src/testing/mod.rs index 2aff64d..0a753db 100644 --- a/src/testing/mod.rs +++ b/src/testing/mod.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use app::App; use render::Renderer; +use senses::*; use ui::prelude::*; use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window}; @@ -29,22 +30,19 @@ impl Client { let mut ui = Ui::new(); let rect = Rect { - color: UiColor::WHITE, + color: Color::WHITE, radius: 20.0, thickness: 0.0, inner_radius: 0.0, }; let pad_test = ui.add_static( ( - rect.color(UiColor::BLUE), + rect.color(Color::BLUE), ( - rect.color(UiColor::RED).center((100.0, 100.0)), - ( - rect.color(UiColor::ORANGE), - rect.color(UiColor::LIME).pad(10.0), - ) + rect.color(Color::RED).center((100.0, 100.0)), + (rect.color(Color::ORANGE), rect.color(Color::LIME).pad(10.0)) .span(Dir::RIGHT, [1, 1]), - rect.color(UiColor::YELLOW), + rect.color(Color::YELLOW), ) .span(Dir::RIGHT, [2, 2, 1]) .pad(10), @@ -53,12 +51,12 @@ impl Client { ); let span_test = ui.add_static( ( - rect.color(UiColor::GREEN), - rect.color(UiColor::ORANGE), - rect.color(UiColor::CYAN), - rect.color(UiColor::BLUE), - rect.color(UiColor::MAGENTA), - rect.color(UiColor::RED), + rect.color(Color::GREEN), + rect.color(Color::ORANGE), + rect.color(Color::CYAN), + rect.color(Color::BLUE), + rect.color(Color::MAGENTA), + rect.color(Color::RED), ) .span( Dir::LEFT, @@ -77,17 +75,14 @@ impl Client { let switch_button = |color, to, label| { let rect = Rect::new(color) - .id_on(Sense::PressStart, move |id, ui| { + .id_on(PRESS_START, move |id, ui| { ui[main].inner.set_static(to); ui[id].color = color.add_rgb(-0.2); }) - .edit_on(Sense::HoverStart, move |r| { + .edit_on(HOVER_START | PRESS_END, move |r| { r.color = color.add_rgb(0.4); }) - .edit_on(Sense::PressEnd, move |r| { - r.color = color.add_rgb(0.4); - }) - .edit_on(Sense::HoverEnd, move |r| { + .edit_on(HOVER_END, move |r| { r.color = color; }); (rect, text(label).size(30)).stack() @@ -95,15 +90,15 @@ impl Client { let tabs = ui.add( ( - switch_button(UiColor::RED, pad_test, "pad test"), - switch_button(UiColor::GREEN, span_test, "span test"), - switch_button(UiColor::BLUE, span_add, "span add test"), + switch_button(Color::RED, pad_test, "pad test"), + switch_button(Color::GREEN, span_test, "span test"), + switch_button(Color::BLUE, span_add, "span add test"), ) .span(Dir::RIGHT, [1, 1, 1]), ); let add_button = Rect::new(Color::LIME) .radius(30) - .on(Sense::PressStart, move |ui| { + .on(PRESS_START, move |ui| { let child = ui .add(image(include_bytes!("assets/sungals.png"))) .erase_type(); @@ -117,7 +112,7 @@ impl Client { let del_button = Rect::new(Color::RED) .radius(30) - .on(Sense::PressStart, move |ui| { + .on(PRESS_START, move |ui| { ui[span_add].children.pop(); }) .region( diff --git a/src/util/bitflags.rs b/src/util/bitflags.rs new file mode 100644 index 0000000..907162c --- /dev/null +++ b/src/util/bitflags.rs @@ -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 { + $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 { + 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) + } +} diff --git a/src/util/mod.rs b/src/util/mod.rs index 3bcb269..04c8895 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -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 = std::collections::HashMap; pub type HashSet = std::collections::HashSet;