1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 16:44:06 +02:00

Merge pull request #5 from jbangelo/fix-unchecked-bool-endianness

Fix unchecked reading boolean values
This commit is contained in:
Robin Appelman 2022-05-01 12:50:17 +00:00 committed by GitHub
commit 5e9f0e7a59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

@ -314,8 +314,13 @@ where
let bit_offset = position & 7;
let byte = self.slice.get_unchecked(byte_index);
let shifted = byte >> bit_offset;
shifted & 1u8 == 1
if E::is_le() {
let shifted = byte >> bit_offset;
shifted & 1u8 == 1
} else {
let shifted = byte << bit_offset;
shifted & 0b1000_0000u8 == 0b1000_0000u8
}
}
/// Read a sequence of bits from the buffer as integer

View file

@ -82,6 +82,13 @@ fn test_bare_enum() {
#[test]
fn test_field_enum() {
#[derive(Debug, PartialEq, BitRead, BitWrite)]
struct CompoundVariant(
#[size = 15]
u16,
bool,
);
#[derive(Debug, PartialEq, BitRead, BitWrite)]
#[discriminant_bits = 4]
enum Enum {
@ -89,11 +96,13 @@ fn test_field_enum() {
B(String),
C(f32),
D(#[size = 15] i64),
E(CompoundVariant),
}
roundtrip(Enum::A);
roundtrip(Enum::B("foobar".into()));
roundtrip(Enum::C(12.0));
roundtrip(Enum::D(-12345));
roundtrip(Enum::E(CompoundVariant(6789, true)));
}
#[test]