From 71294b0fd1f934eee9d1ca5caff14dfdd15630f0 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Thu, 26 Feb 2026 15:50:01 +0100 Subject: [PATCH] macros: auto generate bitflag variants --- src/allocator.rs | 12 ++-- src/backend.rs | 16 ++--- src/criteria/clm.rs | 4 +- src/criteria/tlm.rs | 30 +++++----- src/gfx_api.rs | 4 +- src/gfx_apis/gl/ext.rs | 38 ++++++------ .../jay_head_manager_session_v1.rs | 28 ++++----- src/ifs/xdg_positioner.rs | 8 +-- src/macros.rs | 60 ++++++++++++++++++- 9 files changed, 129 insertions(+), 71 deletions(-) diff --git a/src/allocator.rs b/src/allocator.rs index 7d05df49..91ec23d0 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -17,12 +17,12 @@ pub struct AllocatorError(#[from] pub Box); bitflags! { BufferUsage: u32; - BO_USE_SCANOUT = 1 << 0, - BO_USE_CURSOR = 1 << 1, - BO_USE_RENDERING = 1 << 2, - BO_USE_WRITE = 1 << 3, - BO_USE_LINEAR = 1 << 4, - BO_USE_PROTECTED = 1 << 5, + BO_USE_SCANOUT, + BO_USE_CURSOR, + BO_USE_RENDERING, + BO_USE_WRITE, + BO_USE_LINEAR, + BO_USE_PROTECTED, } pub trait Allocator { diff --git a/src/backend.rs b/src/backend.rs index ddfc98c0..bf07449d 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -127,9 +127,9 @@ impl Display for ConnectorKernelId { bitflags! { ConnectorCaps: u32; - CONCAP_CONNECTOR = 1 << 0, - CONCAP_MODE_SETTING = 1 << 1, - CONCAP_PHYSICAL_DISPLAY = 1 << 2, + CONCAP_CONNECTOR, + CONCAP_MODE_SETTING, + CONCAP_PHYSICAL_DISPLAY, } pub trait Connector: Any { @@ -343,11 +343,11 @@ pub const AXIS_120: i32 = 120; bitflags! { Leds: u32; - LED_NUM_LOCK = 1 << 0, - LED_CAPS_LOCK = 1 << 1, - LED_SCROLL_LOCK = 1 << 2, - LED_COMPOSE = 1 << 3, - LED_KANA = 1 << 4, + LED_NUM_LOCK, + LED_CAPS_LOCK, + LED_SCROLL_LOCK, + LED_COMPOSE, + LED_KANA, } #[derive(Debug)] diff --git a/src/criteria/clm.rs b/src/criteria/clm.rs index 022be1a0..99b0ea8b 100644 --- a/src/criteria/clm.rs +++ b/src/criteria/clm.rs @@ -34,8 +34,8 @@ use { bitflags! { ClMatcherChange: u32; - CL_CHANGED_DESTROYED = 1 << 0, - CL_CHANGED_NEW = 1 << 1, + CL_CHANGED_DESTROYED, + CL_CHANGED_NEW, } type ClmFixedRootMatcher = FixedRootMatcher, T>; diff --git a/src/criteria/tlm.rs b/src/criteria/tlm.rs index 29cca67b..a4fe005d 100644 --- a/src/criteria/tlm.rs +++ b/src/criteria/tlm.rs @@ -45,21 +45,21 @@ use { bitflags! { TlMatcherChange: u32; - TL_CHANGED_DESTROYED = 1 << 0, - TL_CHANGED_NEW = 1 << 1, - TL_CHANGED_TITLE = 1 << 2, - TL_CHANGED_APP_ID = 1 << 3, - TL_CHANGED_FLOATING = 1 << 4, - TL_CHANGED_VISIBLE = 1 << 5, - TL_CHANGED_URGENT = 1 << 6, - TL_CHANGED_SEAT_FOCI = 1 << 7, - TL_CHANGED_FULLSCREEN = 1 << 8, - TL_CHANGED_JUST_MAPPED = 1 << 9, - TL_CHANGED_TAG = 1 << 10, - TL_CHANGED_CLASS_INST = 1 << 11, - TL_CHANGED_ROLE = 1 << 12, - TL_CHANGED_WORKSPACE = 1 << 13, - TL_CHANGED_CONTENT_TY = 1 << 14, + TL_CHANGED_DESTROYED, + TL_CHANGED_NEW, + TL_CHANGED_TITLE, + TL_CHANGED_APP_ID, + TL_CHANGED_FLOATING, + TL_CHANGED_VISIBLE, + TL_CHANGED_URGENT, + TL_CHANGED_SEAT_FOCI, + TL_CHANGED_FULLSCREEN, + TL_CHANGED_JUST_MAPPED, + TL_CHANGED_TAG, + TL_CHANGED_CLASS_INST, + TL_CHANGED_ROLE, + TL_CHANGED_WORKSPACE, + TL_CHANGED_CONTENT_TY, } type TlmFixedRootMatcher = FixedRootMatcher; diff --git a/src/gfx_api.rs b/src/gfx_api.rs index 3e5fb256..109adb28 100644 --- a/src/gfx_api.rs +++ b/src/gfx_api.rs @@ -713,8 +713,8 @@ pub trait AsyncShmGfxTextureCallback { bitflags! { StagingBufferUsecase: u32; - STAGING_UPLOAD = 1 << 0, - STAGING_DOWNLOAD = 1 << 1, + STAGING_UPLOAD, + STAGING_DOWNLOAD, } pub trait GfxStagingBuffer: Any { diff --git a/src/gfx_apis/gl/ext.rs b/src/gfx_apis/gl/ext.rs index 6eb56de8..4a815e21 100644 --- a/src/gfx_apis/gl/ext.rs +++ b/src/gfx_apis/gl/ext.rs @@ -50,11 +50,11 @@ where bitflags! { ClientExt: u32; - EXT_CLIENT_EXTENSION = 1 << 0, - EXT_PLATFORM_BASE = 1 << 1, - KHR_PLATFORM_GBM = 1 << 2, - KHR_DEBUG = 1 << 3, - EXT_DEVICE_QUERY = 1 << 4, + EXT_CLIENT_EXTENSION, + EXT_PLATFORM_BASE, + KHR_PLATFORM_GBM, + KHR_DEBUG, + EXT_DEVICE_QUERY, } pub fn get_client_ext() -> ClientExt { @@ -72,17 +72,17 @@ pub fn get_client_ext() -> ClientExt { bitflags! { DisplayExt: u32; - KHR_IMAGE_BASE = 1 << 0, - EXT_IMAGE_DMA_BUF_IMPORT = 1 << 1, - EXT_IMAGE_DMA_BUF_IMPORT_MODIFIERS = 1 << 2, - KHR_NO_CONFIG_CONTEXT = 1 << 3, - MESA_CONFIGLESS_CONTEXT = 1 << 4, - KHR_SURFACELESS_CONTEXT = 1 << 5, - IMG_CONTEXT_PRIORITY = 1 << 6, - EXT_CREATE_CONTEXT_ROBUSTNESS = 1 << 7, - KHR_FENCE_SYNC = 1 << 8, - KHR_WAIT_SYNC = 1 << 9, - ANDROID_NATIVE_FENCE_SYNC = 1 << 10, + KHR_IMAGE_BASE, + EXT_IMAGE_DMA_BUF_IMPORT, + EXT_IMAGE_DMA_BUF_IMPORT_MODIFIERS, + KHR_NO_CONFIG_CONTEXT, + MESA_CONFIGLESS_CONTEXT, + KHR_SURFACELESS_CONTEXT, + IMG_CONTEXT_PRIORITY, + EXT_CREATE_CONTEXT_ROBUSTNESS, + KHR_FENCE_SYNC, + KHR_WAIT_SYNC, + ANDROID_NATIVE_FENCE_SYNC, } pub(crate) unsafe fn get_display_ext(dpy: EGLDisplay) -> DisplayExt { @@ -113,8 +113,8 @@ pub(crate) unsafe fn get_display_ext(dpy: EGLDisplay) -> DisplayExt { bitflags! { GlExt: u32; - GL_OES_EGL_IMAGE = 1 << 0, - GL_OES_EGL_IMAGE_EXTERNAL = 1 << 1, + GL_OES_EGL_IMAGE, + GL_OES_EGL_IMAGE_EXTERNAL, } pub fn get_gl_ext() -> Result { @@ -133,7 +133,7 @@ pub fn get_gl_ext() -> Result { bitflags! { DevExt: u32; - MESA_DEVICE_SOFTWARE = 1 << 0, + MESA_DEVICE_SOFTWARE, } pub fn get_device_ext(procs: &ExtProc, dpy: EGLDisplay) -> Result { diff --git a/src/ifs/head_management/jay_head_manager_session_v1.rs b/src/ifs/head_management/jay_head_manager_session_v1.rs index 082077a0..309405e5 100644 --- a/src/ifs/head_management/jay_head_manager_session_v1.rs +++ b/src/ifs/head_management/jay_head_manager_session_v1.rs @@ -376,20 +376,20 @@ impl JayHeadManagerSessionV1RequestHandler for JayHeadManagerSessionV1 { } bitflags! { ToSend: u32; - CORE_INFO = 1 << 0, - COMPOSITOR_SPACE_INFO_FULL = 1 << 1, - COMPOSITOR_SPACE_INFO_POS = 1 << 2, - COMPOSITOR_SPACE_INFO_SIZE = 1 << 3, - COMPOSITOR_SPACE_INFO_TRANSFORM = 1 << 4, - COMPOSITOR_SPACE_INFO_SCALE = 1 << 5, - MODE_INFO = 1 << 6, - NON_DESKTOP_INFO = 1 << 7, - VRR_MODE_INFO = 1 << 8, - TEARING_MODE_INFO = 1 << 9, - FORMAT_INFO = 1 << 10, - DRM_COLOR_SPACE_INFO = 1 << 11, - BRIGHTNESS_INFO = 1 << 12, - COMPOSITOR_SPACE_INFO_ENABLED = 1 << 13, + CORE_INFO, + COMPOSITOR_SPACE_INFO_FULL, + COMPOSITOR_SPACE_INFO_POS, + COMPOSITOR_SPACE_INFO_SIZE, + COMPOSITOR_SPACE_INFO_TRANSFORM, + COMPOSITOR_SPACE_INFO_SCALE, + MODE_INFO, + NON_DESKTOP_INFO, + VRR_MODE_INFO, + TEARING_MODE_INFO, + FORMAT_INFO, + DRM_COLOR_SPACE_INFO, + BRIGHTNESS_INFO, + COMPOSITOR_SPACE_INFO_ENABLED, } for head in self.heads.lock().values() { let pending = mem::take(&mut *head.common.pending.borrow_mut()); diff --git a/src/ifs/xdg_positioner.rs b/src/ifs/xdg_positioner.rs index 76ca6764..3b0d02cf 100644 --- a/src/ifs/xdg_positioner.rs +++ b/src/ifs/xdg_positioner.rs @@ -25,10 +25,10 @@ pub const ANCHOR_BOTTOM_RIGHT: u32 = 8; bitflags! { Edge: u32; - E_TOP = 1 << 0, - E_BOTTOM = 1 << 1, - E_LEFT = 1 << 2, - E_RIGHT = 1 << 3, + E_TOP, + E_BOTTOM, + E_LEFT, + E_RIGHT, } impl Edge { diff --git a/src/macros.rs b/src/macros.rs index 5c442c26..260665c6 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -456,8 +456,34 @@ macro_rules! fatal { }} } +#[expect(clippy::allow_attributes)] +#[allow(dead_code)] +pub trait Bitflag { + type Type; +} + macro_rules! bitflags { + ($name:ident: $rep:ty; $($var:ident,)*) => { + with_builtin_macros::with_eager_expansions! { + bitflags! { + $name: $rep; + #{concat_idents!($name, Enum)}; + $( + $var = 1 << #{concat_idents!($name, Enum)}::$var as u32, + )* + } + } + }; ($name:ident: $rep:ty; $($var:ident = $val:expr,)*) => { + with_builtin_macros::with_eager_expansions! { + bitflags! { + $name: $rep; + #{concat_idents!($name, Enum)}; + $($var = $val,)* + } + } + }; + ($name:ident: $rep:ty; $enum_name:ident; $($var:ident = $val:expr,)*) => { #[derive(Copy, Clone, Eq, PartialEq, Default)] pub struct $name(pub $rep); @@ -499,6 +525,20 @@ macro_rules! bitflags { pub fn intersects(self, other: Self) -> bool { self.0 & other.0 != 0 } + + #[allow(clippy::allow_attributes, clippy::bad_bit_mask)] + pub fn to_map(self) -> linearize::StaticCopyMap<$enum_name, bool> { + let mut res = linearize::StaticCopyMap::default(); + let v = self.0; + $( + res[$enum_name::$var] = v & $val == $val; + )* + res + } + } + + impl crate::macros::Bitflag for $name { + type Type = $rep; } impl std::ops::BitOr for $name { @@ -567,7 +607,25 @@ macro_rules! bitflags { Ok(()) } } - } + + #[derive(Copy, Clone, Debug, Eq, PartialEq, linearize::Linearize)] + #[expect(clippy::allow_attributes)] + pub enum $enum_name { + $( + #[allow(non_camel_case_types, dead_code)] + $var, + )* + } + + impl $enum_name { + #[allow(clippy::allow_attributes, dead_code)] + pub fn to_bits(self) -> $name { + match self { + $(Self::$var => $var,)* + } + } + } + }; } macro_rules! pw_opcodes {