Skip to content

Commit

Permalink
📝 Add documentation for field access mode
Browse files Browse the repository at this point in the history
  • Loading branch information
paul1999 committed Dec 10, 2023
1 parent 59e2ba6 commit 4af40a0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,17 @@ struct MyBitfield {
#[bits(16)]
custom: CustomEnum,
/// public field -> public accessor functions
#[bits(12)]
#[bits(9)]
pub public: usize,
/// Can specify the access mode for fields, Read Write being the default
#[bits(1, access = RW)]
read_write: bool,
/// Can also specify read only fields...
#[bits(1, access = RO)]
read_only: bool,
/// ...and write only fields
#[bits(1, access = WO)]
write_only: bool,
/// padding
#[bits(5)]
__: u8,
Expand Down Expand Up @@ -139,7 +148,11 @@ let mut val = MyBitfield::new()
.with_tiny(1)
.with_negative(-3)
.with_custom(CustomEnum::B)
.with_public(2);
.with_public(2)
.with_read_write(true)
// Would not compile
// .with_read_only(true)
.with_write_only(false);

println!("{val:?}");
let raw: u64 = val.into();
Expand All @@ -151,6 +164,8 @@ assert_eq!(val.negative(), -3);
assert_eq!(val.tiny(), 1);
assert_eq!(val.custom(), CustomEnum::B);
assert_eq!(val.public(), 2);
assert_eq!(val.read_write(), true);
assert_eq!(val.read_only(), false);

// const members
assert_eq!(MyBitfield::FLAG_BITS, 1);
Expand Down
21 changes: 18 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,17 @@
//! #[bits(16)]
//! custom: CustomEnum,
//! /// public field -> public accessor functions
//! #[bits(12)]
//! #[bits(9)]
//! pub public: usize,
//! /// Can specify the access mode for fields, Read Write being the default
//! #[bits(1, access = RW)]
//! read_write: bool,
//! /// Can also specify read only fields...
//! #[bits(1, access = RO)]
//! read_only: bool,
//! /// ...and write only fields
//! #[bits(1, access = WO)]
//! write_only: bool,
//! /// padding
//! #[bits(5)]
//! __: u8,
Expand Down Expand Up @@ -130,7 +139,11 @@
//! .with_tiny(1)
//! .with_negative(-3)
//! .with_custom(CustomEnum::B)
//! .with_public(2);
//! .with_public(2)
//! .with_read_write(true)
//! // Would not compile
//! // .with_read_only(true)
//! .with_write_only(false);
//!
//! println!("{val:?}");
//! let raw: u64 = val.into();
Expand All @@ -142,6 +155,8 @@
//! assert_eq!(val.tiny(), 1);
//! assert_eq!(val.custom(), CustomEnum::B);
//! assert_eq!(val.public(), 2);
//! assert_eq!(val.read_write(), true);
//! assert_eq!(val.read_only(), false);
//!
//! // const members
//! assert_eq!(MyBitfield::FLAG_BITS, 1);
Expand Down Expand Up @@ -519,7 +534,7 @@ impl Member {
let ident_str = inner.ident.to_string();
let ident = &inner.ident;
quote!(.field(#ident_str, &self.#ident()))
},
}
_ => {
quote!()
}
Expand Down

0 comments on commit 4af40a0

Please sign in to comment.