Skip to content

Commit 8b0a1b3

Browse files
committed
lsbu64 type, byte positioning bug fix
1 parent 3bf5bfa commit 8b0a1b3

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

packed_struct/src/packing/types_ints.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,63 @@ impl Display for MsbU64 {
390390
}
391391
}
392392

393+
/// The Least Significant Byte is the first one, unsigned.
394+
#[derive(Copy, Clone, Debug, Default, PartialEq)]
395+
pub struct LsbU64(pub u64);
396+
397+
impl Deref for LsbU64 {
398+
type Target = u64;
399+
#[inline]
400+
fn deref(&self) -> &u64 {
401+
&self.0
402+
}
403+
}
404+
405+
impl PackedStruct<[u8; 8]> for LsbU64 {
406+
#[inline]
407+
fn pack(&self) -> [u8; 8] {
408+
[
409+
(self.0 & 0x00000000000000FF) as u8,
410+
((self.0 & 0x000000000000FF00) >> 8) as u8,
411+
((self.0 & 0x0000000000FF0000) >> 16) as u8,
412+
((self.0 & 0x00000000FF000000) >> 24) as u8,
413+
((self.0 & 0x000000FF00000000) >> 32) as u8,
414+
((self.0 & 0x0000FF0000000000) >> 40) as u8,
415+
((self.0 & 0x00FF000000000000) >> 48) as u8,
416+
((self.0 & 0xFF00000000000000) >> 56) as u8
417+
]
418+
}
419+
420+
#[inline]
421+
fn unpack(src: &[u8; 8]) -> Result<LsbU64, PackingError> {
422+
Ok(LsbU64(
423+
src[0] as u64 |
424+
((src[1] as u64) << 8) |
425+
((src[2] as u64) << 16) |
426+
((src[3] as u64) << 24) |
427+
((src[4] as u64) << 32) |
428+
((src[5] as u64) << 40) |
429+
((src[6] as u64) << 48) |
430+
((src[7] as u64) << 56)
431+
))
432+
}
433+
}
434+
435+
impl PackedStructInfo for LsbU64 {
436+
#[inline]
437+
fn packed_bits() -> usize {
438+
64
439+
}
440+
}
441+
442+
packing_slice!(LsbU64; 8);
443+
444+
#[cfg(any(feature="core_collections", feature="std"))]
445+
impl Display for LsbU64 {
446+
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
447+
write!(f, "{}", self.0)
448+
}
449+
}
393450

394451

395452

packed_struct_codegen/src/pack_parse_attributes.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ pub fn parse_position_val(v: &str, multiplier: usize) -> BitsPositionParsed {
166166
}
167167
} else {
168168
let start = parse_num(v);
169-
return BitsPositionParsed::Range(start * multiplier, start * multiplier);
169+
if multiplier > 1 {
170+
return BitsPositionParsed::Range(start * multiplier, ((start+1) * multiplier)-1);
171+
} else {
172+
return BitsPositionParsed::Range(start * multiplier, start * multiplier);
173+
}
170174
}
171175

172176
panic!("invalid bits position")

packed_struct_tests/tests/packing_primitives.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,61 @@ fn test_packed_primitives() {
2828
let packed = i.pack();
2929
assert_eq!(&packed, &[0b00101010, 0b10101010, 0b10111010, 0b10101010, 0b10100000, 0]);
3030
}
31+
32+
#[test]
33+
#[cfg(test)]
34+
fn test_packed_int_msb_endian() {
35+
36+
#[derive(Copy, Clone, Debug, Default, PartialEq, PackedStruct)]
37+
#[packed_struct(endian="msb", bit_numbering="msb0")]
38+
pub struct IntsMsb {
39+
#[packed_field(bytes="0")]
40+
pub a: u8,
41+
#[packed_field(bytes="1")]
42+
pub b: i8,
43+
pub c: u16,
44+
pub d: i16,
45+
pub e: u32,
46+
pub f: i32,
47+
pub g: u64
48+
}
49+
50+
let i = IntsMsb {
51+
a: 85,
52+
b: 85,
53+
..Default::default()
54+
};
55+
56+
let packed = i.pack();
57+
let unpacked = IntsMsb::unpack(&packed).unwrap();
58+
assert_eq!(i, unpacked);
59+
}
60+
61+
#[test]
62+
#[cfg(test)]
63+
fn test_packed_int_lsb_endian() {
64+
65+
#[derive(Copy, Clone, Debug, Default, PartialEq, PackedStruct)]
66+
#[packed_struct(endian="lsb", bit_numbering="msb0")]
67+
pub struct IntsLsb {
68+
#[packed_field(bytes="0")]
69+
pub a: u8,
70+
#[packed_field(bytes="1")]
71+
pub b: i8,
72+
pub c: u16,
73+
pub d: i16,
74+
pub e: u32,
75+
pub f: i32,
76+
pub g: u64
77+
}
78+
79+
let i = IntsLsb {
80+
a: 85,
81+
b: 85,
82+
..Default::default()
83+
};
84+
85+
let packed = i.pack();
86+
let unpacked = IntsLsb::unpack(&packed).unwrap();
87+
assert_eq!(i, unpacked);
88+
}

0 commit comments

Comments
 (0)