mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2024-11-24 20:48:55 +00:00
85fa41f7b2
If we know the width of an enum type, we don't need 'power of 2' number of variants. BUG=None TEST=cargo test Change-Id: I8148b28f86bb8e4fd4f67d8a6382fc713dad1439 Reviewed-on: https://chromium-review.googlesource.com/1530455 Commit-Ready: Jingkui Wang <jkwang@google.com> Tested-by: Jingkui Wang <jkwang@google.com> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: David Tolnay <dtolnay@chromium.org>
50 lines
1,018 B
Rust
50 lines
1,018 B
Rust
extern crate bit_field;
|
|
|
|
use bit_field::*;
|
|
|
|
#[bitfield]
|
|
#[derive(Debug, PartialEq)]
|
|
enum TwoBits {
|
|
Zero = 0b00,
|
|
One = 0b01,
|
|
Two = 0b10,
|
|
Three = 0b11,
|
|
}
|
|
|
|
#[bitfield]
|
|
#[bits = 3]
|
|
#[derive(Debug, PartialEq)]
|
|
enum ThreeBits {
|
|
Zero = 0b00,
|
|
One = 0b01,
|
|
Two = 0b10,
|
|
Three = 0b111,
|
|
}
|
|
|
|
#[bitfield]
|
|
struct Struct {
|
|
prefix: BitField1,
|
|
two_bits: TwoBits,
|
|
three_bits: ThreeBits,
|
|
suffix: BitField2,
|
|
}
|
|
|
|
#[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
|
|
// ^^_^ Three Bits.
|
|
assert_eq!(s.get_two_bits(), TwoBits::One);
|
|
assert_eq!(s.get_three_bits().unwrap_err().raw_val(), 0b101);
|
|
|
|
s.set_three_bits(ThreeBits::Two);
|
|
assert_eq!(s.get(0, 8), 0b_1001_0010);
|
|
}
|