Skip to content

Commit

Permalink
remove unsafe usage for GenericArray
Browse files Browse the repository at this point in the history
and re-export generic array
  • Loading branch information
wwylele committed Aug 24, 2019
1 parent d7841de commit 9c41996
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 29 deletions.
4 changes: 2 additions & 2 deletions byte_struct/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "byte_struct"
version = "0.5.0"
version = "0.6.0"
authors = ["Weiyi Wang <wwylele@gmail.com>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand All @@ -12,6 +12,6 @@ readme = "../README.md"

[dependencies]
byte_struct_derive = { version = "0.4.2", path = "../byte_struct_derive" }
generic-array = "0.13"
generic-array = ">=0.11"

[dev-dependencies]
31 changes: 6 additions & 25 deletions byte_struct/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
//! ```

pub use byte_struct_derive::{ByteStruct, ByteStructBE, ByteStructLE};
use generic_array::*;
pub use generic_array::*;

/// A type that can be packed into or unpacked from fixed-size bytes, but the method is unknown yet.
pub trait ByteStructLen {
Expand Down Expand Up @@ -77,7 +77,8 @@ pub trait ByteStruct: ByteStructLen {
/// except for `bool`, `char`, `isize` and `usize`.
///
/// This is also implemented for array types whose element type implements `ByteStructUnspecifiedByteOrder`
/// and whose size is between 1 and 32 (inclusive).
/// and whose size is between 1 and 32 (inclusive). Similarly, this is implemented for `GenericArray` types (
/// re-expored from generic-array crate) for the same element type and for all sizes.
///
/// This trait is automatically implemented for all types that implements [`ByteStruct`].
/// In this case, all members of `ByteStructUnspecifiedByteOrder` are direct wrappers of [`ByteStruct`] members.
Expand Down Expand Up @@ -455,7 +456,7 @@ bsa5!(1);
byte_struct_array!(100);
byte_struct_array!(3000);

impl<T: ByteStructLen, U: ArrayLength<T>> ByteStructLen for GenericArray<T, U> {
impl<T: ByteStructLen, U: ArrayLength<T>> ByteStructLen for generic_array::GenericArray<T, U> {
const BYTE_LEN: usize = T::BYTE_LEN * U::USIZE;
}

Expand All @@ -469,17 +470,7 @@ impl<T: ByteStructUnspecifiedByteOrder, U: ArrayLength<T>> ByteStructUnspecified
}
}
fn read_bytes_default_le(bytes: &[u8]) -> Self {
let mut pos = 0;
let len = T::BYTE_LEN;
let mut result: Self;
unsafe {
result = std::mem::uninitialized();
for i in 0 .. U::USIZE {
std::ptr::write(&mut result[i], <T>::read_bytes_default_le(&bytes[pos .. pos + len]));
pos += len;
}
}
result
Self::from_exact_iter(bytes.chunks_exact(T::BYTE_LEN).map(T::read_bytes_default_le)).unwrap()
}
fn write_bytes_default_be(&self, bytes: &mut [u8]) {
let mut pos = 0;
Expand All @@ -490,17 +481,7 @@ impl<T: ByteStructUnspecifiedByteOrder, U: ArrayLength<T>> ByteStructUnspecified
}
}
fn read_bytes_default_be(bytes: &[u8]) -> Self {
let mut pos = 0;
let len = T::BYTE_LEN;
let mut result: Self;
unsafe {
result = std::mem::uninitialized();
for i in 0 .. U::USIZE {
std::ptr::write(&mut result[i], <T>::read_bytes_default_be(&bytes[pos .. pos + len]));
pos += len;
}
}
result
Self::from_exact_iter(bytes.chunks_exact(T::BYTE_LEN).map(T::read_bytes_default_be)).unwrap()
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ publish = false

[dependencies]
byte_struct = {path = "../byte_struct"}
generic-array = "0.13"
1 change: 0 additions & 1 deletion tests/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use byte_struct::*;
use generic_array::*;

bitfields!(
#[derive(PartialEq, Debug)]
Expand Down

0 comments on commit 9c41996

Please sign in to comment.