File tree Expand file tree Collapse file tree 1 file changed +8
-20
lines changed Expand file tree Collapse file tree 1 file changed +8
-20
lines changed Original file line number Diff line number Diff line change 51
51
pub use byte_struct_derive:: { ByteStruct , ByteStructBE , ByteStructLE } ;
52
52
pub use generic_array:: * ;
53
53
54
+ use std:: convert:: TryInto ;
55
+
54
56
/// A type that can be packed into or unpacked from fixed-size bytes, but the method is unknown yet.
55
57
pub trait ByteStructLen {
56
58
/// The length of the packed bytes of this type
@@ -408,17 +410,10 @@ macro_rules! byte_struct_array {
408
410
}
409
411
}
410
412
fn read_bytes_default_le( bytes: & [ u8 ] ) -> Self {
411
- let mut pos = 0 ;
412
413
let len = T :: BYTE_LEN ;
413
- let mut result: Self ;
414
- unsafe {
415
- result = std:: mem:: uninitialized( ) ;
416
- for i in 0 .. ( $x) {
417
- std:: ptr:: write( & mut result[ i] , <T >:: read_bytes_default_le( & bytes[ pos .. pos + len] ) ) ;
418
- pos += len;
419
- }
420
- }
421
- result
414
+ ( 0 .. ( $x) ) . map( |i| {
415
+ <T >:: read_bytes_default_le( & bytes[ i * len .. ( i + 1 ) * len] )
416
+ } ) . collect:: <Vec <_>>( ) . try_into( ) . map_err( |_|( ) ) . unwrap( )
422
417
}
423
418
fn write_bytes_default_be( & self , bytes: & mut [ u8 ] ) {
424
419
let mut pos = 0 ;
@@ -429,17 +424,10 @@ macro_rules! byte_struct_array {
429
424
}
430
425
}
431
426
fn read_bytes_default_be( bytes: & [ u8 ] ) -> Self {
432
- let mut pos = 0 ;
433
427
let len = T :: BYTE_LEN ;
434
- let mut result: Self ;
435
- unsafe {
436
- result = std:: mem:: uninitialized( ) ;
437
- for i in 0 .. ( $x) {
438
- std:: ptr:: write( & mut result[ i] , <T >:: read_bytes_default_be( & bytes[ pos .. pos + len] ) ) ;
439
- pos += len;
440
- }
441
- }
442
- result
428
+ ( 0 .. ( $x) ) . map( |i| {
429
+ <T >:: read_bytes_default_be( & bytes[ i * len .. ( i + 1 ) * len] )
430
+ } ) . collect:: <Vec <_>>( ) . try_into( ) . map_err( |_|( ) ) . unwrap( )
443
431
}
444
432
}
445
433
}
You can’t perform that action at this time.
0 commit comments