Skip to content

Commit

Permalink
Added from_bits and into_bits functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frostie314159 authored and wrenger committed Feb 16, 2024
1 parent edd4aa9 commit 90d1ff3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
#( #defaults )*
this
}
#vis const fn from_bits(bits: #ty) -> Self {
Self(bits)
}
#vis const fn into_bits(self) -> #ty {
self.0
}

#( #members )*
}
Expand Down
19 changes: 19 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,22 @@ fn msb_order() {

assert_eq!(v.0, 0xe11e_00_f0);
}

#[test]
fn nested() {
#[bitfield(u8)]
#[derive(PartialEq)]
struct Child {
contents: u8,
}
#[bitfield(u8)]
#[derive(PartialEq)]
struct Parent {
#[bits(8)]
child: Child,
}
let child = Child::new().with_contents(0xff);
let parent = Parent::new().with_child(child);
assert_eq!(child.into_bits(), 0xff);
assert_eq!(parent.into_bits(), 0xff);
}

0 comments on commit 90d1ff3

Please sign in to comment.