From 4af40a001f4bc30277861a0447148223841809ec Mon Sep 17 00:00:00 2001 From: Paul Bergmann Date: Thu, 7 Dec 2023 12:42:24 +0100 Subject: [PATCH] :memo: Add documentation for field access mode --- README.md | 19 +++++++++++++++++-- src/lib.rs | 21 ++++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a0404b1..96ed985 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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(); @@ -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); diff --git a/src/lib.rs b/src/lib.rs index 6877766..6abd3d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, @@ -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(); @@ -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); @@ -519,7 +534,7 @@ impl Member { let ident_str = inner.ident.to_string(); let ident = &inner.ident; quote!(.field(#ident_str, &self.#ident())) - }, + } _ => { quote!() }