Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Implement read-only and write-only fields #28

Merged
merged 3 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
Loading