Skip to content

Commit 9c41996

Browse files
committed
remove unsafe usage for GenericArray
and re-export generic array
1 parent d7841de commit 9c41996

File tree

4 files changed

+8
-29
lines changed

4 files changed

+8
-29
lines changed

byte_struct/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "byte_struct"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
authors = ["Weiyi Wang <[email protected]>"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"
@@ -12,6 +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"
15+
generic-array = ">=0.11"
1616

1717
[dev-dependencies]

byte_struct/src/lib.rs

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

5454
/// A type that can be packed into or unpacked from fixed-size bytes, but the method is unknown yet.
5555
pub trait ByteStructLen {
@@ -77,7 +77,8 @@ pub trait ByteStruct: ByteStructLen {
7777
/// except for `bool`, `char`, `isize` and `usize`.
7878
///
7979
/// 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.
8182
///
8283
/// This trait is automatically implemented for all types that implements [`ByteStruct`].
8384
/// In this case, all members of `ByteStructUnspecifiedByteOrder` are direct wrappers of [`ByteStruct`] members.
@@ -455,7 +456,7 @@ bsa5!(1);
455456
byte_struct_array!(100);
456457
byte_struct_array!(3000);
457458

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> {
459460
const BYTE_LEN: usize = T::BYTE_LEN * U::USIZE;
460461
}
461462

@@ -469,17 +470,7 @@ impl<T: ByteStructUnspecifiedByteOrder, U: ArrayLength<T>> ByteStructUnspecified
469470
}
470471
}
471472
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()
483474
}
484475
fn write_bytes_default_be(&self, bytes: &mut [u8]) {
485476
let mut pos = 0;
@@ -490,17 +481,7 @@ impl<T: ByteStructUnspecifiedByteOrder, U: ArrayLength<T>> ByteStructUnspecified
490481
}
491482
}
492483
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()
504485
}
505486
}
506487

tests/Cargo.toml

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

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

tests/src/main.rs

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

43
bitfields!(
54
#[derive(PartialEq, Debug)]

0 commit comments

Comments
 (0)