crosvm/bit_field/tests/test_tuple_struct.rs
Jingkui Wang 1612ff7a83 improve bitfield type safety by allowing tuple struct field
user-defined tuple struct could be used to improve type safety.

TEST=cargo test
BUG=None

Change-Id: I8ce10fc51b79c277ab23029513b707f3dd621af5
Reviewed-on: https://chromium-review.googlesource.com/1546432
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-04-03 18:13:52 -07:00

25 lines
463 B
Rust

extern crate bit_field;
use bit_field::*;
#[bitfield]
#[bits = 5]
#[derive(Debug, PartialEq)]
pub struct FiveBits(u8);
#[bitfield]
struct Struct {
prefix: BitField1,
five_bits: FiveBits,
suffix: BitField2,
}
#[test]
fn test_enum() {
let mut s = Struct::new();
assert_eq!(s.get(0, 8), 0b_0000_0000);
s.set_five_bits(FiveBits(0b10101));
assert_eq!(s.get(0, 8), 0b_0010_1010);
assert_eq!(s.get_five_bits(), FiveBits(0b10101));
}