Skip to content

Commit c6882b2

Browse files
Add #[bitmap_attr] attribute macro
- Implements feature requested in issue #8 - Provides alternative syntax to bitmap! macro - Both macros generate identical code and functionality - Added comprehensive tests to verify compatibility - Updated README with usage examples
1 parent 61477ea commit c6882b2

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ assert_eq!(player.finished_tasks(), 5);
6464
assert_eq!(player.kills(), 3);
6565
assert_eq!(*player, 0b01101011);
6666
```
67+
## `#[bitmap_attr]` Attribute Macro
68+
69+
If you prefer attribute macro syntax, you can use `#[bitmap_attr]` instead of `bitmap!`. Both work exactly the same way.
70+
71+
```rust
72+
use bitmap::bitmap_attr;
73+
74+
#[bitmap_attr]
75+
struct Player {
76+
imposter: u1,
77+
finished_tasks: u3,
78+
kills: u3,
79+
}
80+
81+
let mut player = Player(0);
82+
player.set_imposter(1)
83+
.set_finished_tasks(5)
84+
.set_kills(3);
85+
86+
assert_eq!(player.imposter(), 1);
87+
assert_eq!(player.finished_tasks(), 5);
88+
assert_eq!(player.kills(), 3);
89+
}
6790

6891
### Accessing fields
6992

macros/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use proc_macro::TokenStream;
22
use syn::parse_macro_input;
33
use syn::DeriveInput;
4-
use quote::quote;
4+
55
use quote::ToTokens;
66

77
mod generator;
@@ -133,7 +133,7 @@ pub fn bitmap(input: TokenStream) -> TokenStream {
133133
pub fn bitmap_attr(_args: TokenStream, input: TokenStream) -> TokenStream {
134134
let input = parse_macro_input!(input as DeriveInput);
135135

136-
// Convert the attribute macro input to the existing BitmapInput format
136+
// Converting the attribute macro input to the existing BitmapInput format
137137
let bitmap_input = convert_derive_to_bitmap_input(input);
138138

139139
// Use the EXACT SAME expansion logic as the bitmap! macro
@@ -144,9 +144,9 @@ pub fn bitmap_attr(_args: TokenStream, input: TokenStream) -> TokenStream {
144144
}
145145

146146
fn convert_derive_to_bitmap_input(input: DeriveInput) -> parser::BitmapInput {
147-
let name = input.ident; // Use 'name' not 'struct_name'
147+
let name = input.ident;
148148

149-
// Extract struct fields
149+
// Extracting struct fields
150150
let syn::Data::Struct(data_struct) = input.data else {
151151
panic!("#[bitmap_attr] can only be used on structs");
152152
};
@@ -155,12 +155,12 @@ fn convert_derive_to_bitmap_input(input: DeriveInput) -> parser::BitmapInput {
155155
panic!("#[bitmap_attr] struct must have named fields");
156156
};
157157

158-
// Convert each field to the format expected by the existing parser
158+
// Converting each field to the format expected by the existing parser
159159
let fields = fields_named.named.into_iter().map(|field| {
160160
let field_name = field.ident.expect("Field must have a name");
161161
let field_type = field.ty;
162162

163-
// Extract the size from the type (e.g., u1 -> 1, u7 -> 7)
163+
// Extracting the size from the type (e.g., u1 -> 1, u7 -> 7)
164164
let type_str = field_type.to_token_stream().to_string();
165165

166166
if !type_str.starts_with("u") {
@@ -180,7 +180,7 @@ fn convert_derive_to_bitmap_input(input: DeriveInput) -> parser::BitmapInput {
180180
}).collect();
181181

182182
parser::BitmapInput {
183-
name, // Use 'name' not 'struct_name'
183+
name,
184184
fields,
185185
}
186186
}

tests/compare_macros.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// tests/compare_macros.rs
21
use bitmap::{bitmap, bitmap_attr};
32

43
// Function-like macro (original)

tests/test_attr.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// tests/test_attr.rs
21
use bitmap::bitmap_attr;
32

43
#[bitmap_attr]
@@ -11,15 +10,15 @@ struct TestBits {
1110
fn test_attribute_macro_creates_correct_api() {
1211
let mut bits = TestBits(0);
1312

14-
// Test that the macro generated the correct methods
13+
// Testing that the macro generated the correct methods
1514
bits.set_flag(1);
1615
bits.set_counter(42);
1716

1817
assert_eq!(bits.flag(), 1);
1918
assert_eq!(bits.counter(), 42);
2019

21-
// Test the raw value
22-
assert_eq!(*bits, 0b10101010); // flag=1 (MSB), counter=42
20+
// Testing the raw value
21+
assert_eq!(*bits, 0b10101010);
2322

2423
println!("Attribute macro creates correct bitmap API!");
2524
}

0 commit comments

Comments
 (0)