49
49
//! ```
50
50
51
51
pub use byte_struct_derive:: { ByteStruct , ByteStructBE , ByteStructLE } ;
52
- use generic_array:: * ;
52
+ pub use generic_array:: * ;
53
53
54
54
/// A type that can be packed into or unpacked from fixed-size bytes, but the method is unknown yet.
55
55
pub trait ByteStructLen {
@@ -77,7 +77,8 @@ pub trait ByteStruct: ByteStructLen {
77
77
/// except for `bool`, `char`, `isize` and `usize`.
78
78
///
79
79
/// This is also implemented for array types whose element type implements `ByteStructUnspecifiedByteOrder`
80
- /// and whose size is between 1 and 32 (inclusive).
80
+ /// and whose size is between 1 and 32 (inclusive). Similarly, this is implemented for `GenericArray` types (
81
+ /// re-expored from generic-array crate) for the same element type and for all sizes.
81
82
///
82
83
/// This trait is automatically implemented for all types that implements [`ByteStruct`].
83
84
/// In this case, all members of `ByteStructUnspecifiedByteOrder` are direct wrappers of [`ByteStruct`] members.
@@ -455,7 +456,7 @@ bsa5!(1);
455
456
byte_struct_array ! ( 100 ) ;
456
457
byte_struct_array ! ( 3000 ) ;
457
458
458
- impl < T : ByteStructLen , U : ArrayLength < T > > ByteStructLen for GenericArray < T , U > {
459
+ impl < T : ByteStructLen , U : ArrayLength < T > > ByteStructLen for generic_array :: GenericArray < T , U > {
459
460
const BYTE_LEN : usize = T :: BYTE_LEN * U :: USIZE ;
460
461
}
461
462
@@ -469,17 +470,7 @@ impl<T: ByteStructUnspecifiedByteOrder, U: ArrayLength<T>> ByteStructUnspecified
469
470
}
470
471
}
471
472
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
473
+ Self :: from_exact_iter ( bytes. chunks_exact ( T :: BYTE_LEN ) . map ( T :: read_bytes_default_le) ) . unwrap ( )
483
474
}
484
475
fn write_bytes_default_be ( & self , bytes : & mut [ u8 ] ) {
485
476
let mut pos = 0 ;
@@ -490,17 +481,7 @@ impl<T: ByteStructUnspecifiedByteOrder, U: ArrayLength<T>> ByteStructUnspecified
490
481
}
491
482
}
492
483
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
484
+ Self :: from_exact_iter ( bytes. chunks_exact ( T :: BYTE_LEN ) . map ( T :: read_bytes_default_be) ) . unwrap ( )
504
485
}
505
486
}
506
487
0 commit comments