From 1a6a74d3a13de562bfa7d465f413894597f08810 Mon Sep 17 00:00:00 2001 From: GrayJack Date: Tue, 19 Nov 2024 06:13:28 -0300 Subject: [PATCH] feat: Make generating const function with mutable reference behind a feature flag Turns out it was just stabilized, and will be on Rust 1.83.0 --- Cargo.toml | 3 ++- bitflags-attr-macros/Cargo.toml | 2 ++ bitflags-attr-macros/src/lib.rs | 12 +++++++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8a911da..4db8ca3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,8 @@ default = [] serde = ["bitflags-attr-macros/serde"] # Allows to use custom types as parameter for the bitflags macro custom-types = ["bitflags-attr-macros/custom-types"] - +# Generate as const functions some functions that take `&mut` (Only stable on rust 1.83.0: release date: 28 November, 2024) +const-mut-ref = ["bitflags-attr-macros/const-mut-ref"] [workspace] members = ["bitflags-attr-macros"] diff --git a/bitflags-attr-macros/Cargo.toml b/bitflags-attr-macros/Cargo.toml index 5e3f5cf..10fb59c 100644 --- a/bitflags-attr-macros/Cargo.toml +++ b/bitflags-attr-macros/Cargo.toml @@ -32,3 +32,5 @@ default = [] serde = [] # Allows to use custom types as parameter for the bitflags macro custom-types = [] +# Generate as const functions some functions that take `&mut` (Only stable on rust 1.83.0: release date: 28 November, 2024) +const-mut-ref = [] diff --git a/bitflags-attr-macros/src/lib.rs b/bitflags-attr-macros/src/lib.rs index ddb23cf..8f71c63 100644 --- a/bitflags-attr-macros/src/lib.rs +++ b/bitflags-attr-macros/src/lib.rs @@ -133,6 +133,12 @@ fn bitflag_impl(attr: TokenStream, item: TokenStream) -> Result { Ident::new(&ty_name, span) }; + let const_mut = if cfg!(feature = "const-mut-ref") { + quote!(mut) + } else { + quote!() + }; + let number_flags = item.variants.len(); let mut all_flags = Vec::with_capacity(number_flags); @@ -607,20 +613,20 @@ fn bitflag_impl(attr: TokenStream, item: TokenStream) -> Result { /// Set the flags in `other` in the value. #[inline] #[doc(alias = "insert")] - pub const fn set(&mut self, other: Self) { + pub #const_mut fn set(&mut self, other: Self) { self.0 = self.or(other).0 } /// Unset the flags in `other` in the value. #[inline] #[doc(alias = "remove")] - pub const fn unset(&mut self, other: Self) { + pub #const_mut fn unset(&mut self, other: Self) { self.0 = self.difference(other).0 } /// Toggle the flags in `other` in the value. #[inline] - pub const fn toggle(&mut self, other: Self) { + pub #const_mut fn toggle(&mut self, other: Self) { self.0 = self.xor(other).0 } }