diff options
author | David Tolnay <dtolnay@chromium.org> | 2019-03-12 17:12:28 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-03-17 14:38:45 -0700 |
commit | c324429b467d530fbeadef1fc9b527bb23ce1632 (patch) | |
tree | 0bd748676eaa4c97dc6647c7d25a8ce1abdb3d86 /bit_field/tests/test_enum.rs | |
parent | fba396e42f07ed4b49479117a6f4646fed32c467 (diff) | |
download | crosvm-c324429b467d530fbeadef1fc9b527bb23ce1632.tar crosvm-c324429b467d530fbeadef1fc9b527bb23ce1632.tar.gz crosvm-c324429b467d530fbeadef1fc9b527bb23ce1632.tar.bz2 crosvm-c324429b467d530fbeadef1fc9b527bb23ce1632.tar.lz crosvm-c324429b467d530fbeadef1fc9b527bb23ce1632.tar.xz crosvm-c324429b467d530fbeadef1fc9b527bb23ce1632.tar.zst crosvm-c324429b467d530fbeadef1fc9b527bb23ce1632.zip |
bitfield: Support BitFieldSpecifier for enums
Previously, the getter and setter functions generated for a bitfield struct by #[bitfield] all operated on primitive types like bool, u8, u16 etc. This CL adds support for getters and setters defined in terms of user-defined enums. We make an enum bitfield-compatible by adding #[bitfield]. The number of variants must be a power of 2. #[bitfield] enum TwoBits { Zero = 0b00, One = 0b01, Two = 0b10, Three = 0b11, } And then it may be used to specify a field in a bitfield struct. #[bitfield] struct Struct { prefix: BitField1, two_bits: TwoBits, suffix: BitField5, } The generated getters and setters for this struct would have the following signatures: impl Struct { fn get_prefix(&self) -> u8; fn set_prefix(&mut self, val: u8); fn get_two_bits(&self) -> TwoBits; fn set_two_bits(&mut self, val: TwoBits); fn get_suffix(&self) -> u8; fn set_suffix(&mut self, val: u8); } TEST=`cargo test` the bit_field and bit_field_derive crates TEST=`cargo check` crosvm Change-Id: Ibc8923e2877fda6ae8da5767731edcb68721a434 Reviewed-on: https://chromium-review.googlesource.com/1519686 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: David Tolnay <dtolnay@chromium.org>
Diffstat (limited to 'bit_field/tests/test_enum.rs')
-rw-r--r-- | bit_field/tests/test_enum.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/bit_field/tests/test_enum.rs b/bit_field/tests/test_enum.rs new file mode 100644 index 0000000..d926d9c --- /dev/null +++ b/bit_field/tests/test_enum.rs @@ -0,0 +1,34 @@ +extern crate bit_field; + +use bit_field::*; + +#[bitfield] +#[derive(Debug, PartialEq)] +enum TwoBits { + Zero = 0b00, + One = 0b01, + Two = 0b10, + Three = 0b11, +} + +#[bitfield] +struct Struct { + prefix: BitField1, + two_bits: TwoBits, + suffix: BitField5, +} + +#[test] +fn test_enum() { + let mut s = Struct::new(); + assert_eq!(s.get(0, 8), 0b_0000_0000); + assert_eq!(s.get_two_bits(), TwoBits::Zero); + + s.set_two_bits(TwoBits::Three); + assert_eq!(s.get(0, 8), 0b_0000_0110); + assert_eq!(s.get_two_bits(), TwoBits::Three); + + s.set(0, 8, 0b_1010_1010); + // ^^ TwoBits + assert_eq!(s.get_two_bits(), TwoBits::One); +} |