diff --git a/Cargo.toml b/Cargo.toml index 8a911daf..4db8ca3e 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 5e3f5cf1..10fb59c5 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 ddb23cfd..8f71c63f 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 } }