diff options
author | Chirantan Ekbote <chirantan@chromium.org> | 2018-03-12 15:40:48 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-03-20 00:00:28 -0700 |
commit | fec86cc3aeb09ffb9b9c1f4165a822378ce64200 (patch) | |
tree | 514a88605598938920f06965d6bdd23bdcb50927 | |
parent | ed517d1bfe028564076ee45bd9412882c85ebbe7 (diff) | |
download | crosvm-fec86cc3aeb09ffb9b9c1f4165a822378ce64200.tar crosvm-fec86cc3aeb09ffb9b9c1f4165a822378ce64200.tar.gz crosvm-fec86cc3aeb09ffb9b9c1f4165a822378ce64200.tar.bz2 crosvm-fec86cc3aeb09ffb9b9c1f4165a822378ce64200.tar.lz crosvm-fec86cc3aeb09ffb9b9c1f4165a822378ce64200.tar.xz crosvm-fec86cc3aeb09ffb9b9c1f4165a822378ce64200.tar.zst crosvm-fec86cc3aeb09ffb9b9c1f4165a822378ce64200.zip |
poll_token_derive: Calculate variant bits without sizeof_val
Calculate the number of bits necessary to represent the enum variant using the next_power_of_two() and trailing_zeros() functions from the primitive usize type. Also add a test to ensure that the returned value is correct when there is only one variant in the enum. BUG=none TEST=unit tests Change-Id: Ibd15efd4f06e17a74489fee04ff19aca0dde68b2 Signed-off-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/959624 Reviewed-by: Zach Reizner <zachr@chromium.org>
-rw-r--r-- | sys_util/poll_token_derive/poll_token_derive.rs | 3 | ||||
-rw-r--r-- | sys_util/poll_token_derive/tests.rs | 22 |
2 files changed, 15 insertions, 10 deletions
diff --git a/sys_util/poll_token_derive/poll_token_derive.rs b/sys_util/poll_token_derive/poll_token_derive.rs index 7af2de5..3506b4d 100644 --- a/sys_util/poll_token_derive/poll_token_derive.rs +++ b/sys_util/poll_token_derive/poll_token_derive.rs @@ -326,8 +326,7 @@ impl EnumModel { if self.variants.is_empty() { return 0; } - let variant_count = self.variants.len(); - (mem::size_of_val(&variant_count) as u32 * 8) - (variant_count - 1).leading_zeros() + self.variants.len().next_power_of_two().trailing_zeros() } // Generates the function body for `as_raw_token`. diff --git a/sys_util/poll_token_derive/tests.rs b/sys_util/poll_token_derive/tests.rs index 8c81f66..0eacf6d 100644 --- a/sys_util/poll_token_derive/tests.rs +++ b/sys_util/poll_token_derive/tests.rs @@ -197,16 +197,22 @@ mod enum_model { variants: vec![EnumVariant { name: "A".to_owned(), data: None, - }, - EnumVariant { - name: "B".to_owned(), - data: None, - }, - EnumVariant { - name: "C".to_owned(), - data: None, }], }; + assert_eq!(model.variant_bits(), 0); + + model.variants.append( + &mut vec![ + EnumVariant { + name: "B".to_owned(), + data: None, + }, + EnumVariant { + name: "C".to_owned(), + data: None, + } + ] + ); assert_eq!(model.variant_bits(), 2); for _ in 0..1021 { model |