Skip to content

Commit d7841de

Browse files
committed
add generic-array support
1 parent 86668b0 commit d7841de

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

byte_struct/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "byte_struct"
3-
version = "0.4.2"
3+
version = "0.5.0"
44
authors = ["Weiyi Wang <[email protected]>"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"
@@ -12,5 +12,6 @@ readme = "../README.md"
1212

1313
[dependencies]
1414
byte_struct_derive = { version = "0.4.2", path = "../byte_struct_derive" }
15+
generic-array = "0.13"
1516

1617
[dev-dependencies]

byte_struct/src/lib.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
//! ```
5050
5151
pub use byte_struct_derive::{ByteStruct, ByteStructBE, ByteStructLE};
52+
use generic_array::*;
5253

5354
/// A type that can be packed into or unpacked from fixed-size bytes, but the method is unknown yet.
5455
pub trait ByteStructLen {
@@ -454,6 +455,55 @@ bsa5!(1);
454455
byte_struct_array!(100);
455456
byte_struct_array!(3000);
456457

458+
impl<T: ByteStructLen, U: ArrayLength<T>> ByteStructLen for GenericArray<T, U> {
459+
const BYTE_LEN: usize = T::BYTE_LEN * U::USIZE;
460+
}
461+
462+
impl<T: ByteStructUnspecifiedByteOrder, U: ArrayLength<T>> ByteStructUnspecifiedByteOrder for GenericArray<T, U> {
463+
fn write_bytes_default_le(&self, bytes: &mut [u8]) {
464+
let mut pos = 0;
465+
let len = T::BYTE_LEN;
466+
for i in 0 .. U::USIZE {
467+
self[i].write_bytes_default_le(&mut bytes[pos .. pos + len]);
468+
pos += len;
469+
}
470+
}
471+
fn read_bytes_default_le(bytes: &[u8]) -> Self {
472+
let mut pos = 0;
473+
let len = T::BYTE_LEN;
474+
let mut result: Self;
475+
unsafe {
476+
result = std::mem::uninitialized();
477+
for i in 0 .. U::USIZE {
478+
std::ptr::write(&mut result[i], <T>::read_bytes_default_le(&bytes[pos .. pos + len]));
479+
pos += len;
480+
}
481+
}
482+
result
483+
}
484+
fn write_bytes_default_be(&self, bytes: &mut [u8]) {
485+
let mut pos = 0;
486+
let len = T::BYTE_LEN;
487+
for i in 0 .. U::USIZE {
488+
self[i].write_bytes_default_be(&mut bytes[pos .. pos + len]);
489+
pos += len;
490+
}
491+
}
492+
fn read_bytes_default_be(bytes: &[u8]) -> Self {
493+
let mut pos = 0;
494+
let len = T::BYTE_LEN;
495+
let mut result: Self;
496+
unsafe {
497+
result = std::mem::uninitialized();
498+
for i in 0 .. U::USIZE {
499+
std::ptr::write(&mut result[i], <T>::read_bytes_default_be(&bytes[pos .. pos + len]));
500+
pos += len;
501+
}
502+
}
503+
result
504+
}
505+
}
506+
457507
/// Generates a structure that implements [`ByteStructUnspecifiedByteOrder`] with bit field semantics.
458508
///
459509
/// The bit fields are packed to / unpacked from the base integer type,
@@ -468,7 +518,7 @@ byte_struct_array!(3000);
468518
/// [`ByteStructUnspecifiedByteOrder`]: trait.ByteStructUnspecifiedByteOrder.html
469519
///
470520
/// # Example
471-
/// ```
521+
/// ```ignore
472522
/// bitfields!(
473523
/// // Specifies the struct name and the base type.
474524
/// // The base type must be one of unsigned integer types.

byte_struct_derive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ enum Endianness {
4848
/// [`ByteStructUnspecifiedByteOrder`]: https://docs.rs/byte_struct/*/byte_struct/trait.ByteStructUnspecifiedByteOrder.html
4949
///
5050
/// ## Example
51-
/// ```
51+
/// ```ignore
5252
/// #[derive(ByteStruct)]
5353
/// #[byte_struct_le]
5454
/// struct Struct1 {

tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ publish = false
77

88
[dependencies]
99
byte_struct = {path = "../byte_struct"}
10+
generic-array = "0.13"

tests/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use byte_struct::*;
2+
use generic_array::*;
23

34
bitfields!(
45
#[derive(PartialEq, Debug)]
@@ -33,7 +34,7 @@ struct TestSubStruct3 {
3334
struct TestStruct {
3435
a: u8,
3536
s: TestSubStruct3,
36-
d: [u16; 3],
37+
d: GenericArray<u16, typenum::U3>,
3738
e: u32,
3839
f: u64,
3940
#[byte_struct_be] g: u128,
@@ -68,7 +69,7 @@ fn main() {
6869
v: 0x5040
6970
},
7071
},
71-
d: [0x1020, 0x3040, 0x5060],
72+
d: *GenericArray::from_slice(&[0x1020, 0x3040, 0x5060]),
7273
e: 0x9abcdef0,
7374
f: 0x0123456789ABCDEF,
7475
g: 0xffeeddccbbaa99887766554433221100,
@@ -129,7 +130,7 @@ fn main() {
129130
v: 0xfedc,
130131
},
131132
},
132-
d: [0x5544, 0x7766, 0x9988],
133+
d: *GenericArray::from_slice(&[0x5544, 0x7766, 0x9988]),
133134
e: 0xddccbbaa,
134135
f: 0x8171615140302010,
135136
g: 0x11121314252627283132333445464748,

0 commit comments

Comments
 (0)