Skip to content

Commit

Permalink
feat: Make generating const function with mutable reference behind a …
Browse files Browse the repository at this point in the history
…feature flag

Turns out it was just stabilized, and will be on Rust 1.83.0
  • Loading branch information
GrayJack committed Nov 19, 2024
1 parent 798badb commit 1a6a74d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 2 additions & 0 deletions bitflags-attr-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
12 changes: 9 additions & 3 deletions bitflags-attr-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ fn bitflag_impl(attr: TokenStream, item: TokenStream) -> Result<TokenStream> {
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);
Expand Down Expand Up @@ -607,20 +613,20 @@ fn bitflag_impl(attr: TokenStream, item: TokenStream) -> Result<TokenStream> {
/// 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
}
}
Expand Down

0 comments on commit 1a6a74d

Please sign in to comment.