Skip to content

Commit

Permalink
fix nightly and move to zerocopy
Browse files Browse the repository at this point in the history
  • Loading branch information
heinrich5991 committed Feb 27, 2024
1 parent d5f089f commit ca6a8ff
Show file tree
Hide file tree
Showing 37 changed files with 357 additions and 420 deletions.
63 changes: 52 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "MIT/Apache-2.0"
arrayvec = "0.5.2"
file_offset = { version = "0.1.0", optional = true }
serde = { version = "1.0.23", optional = true }
zerocopy = "0.7.32"

[dev-dependencies]
bencher = "0.1.5"
Expand Down
54 changes: 54 additions & 0 deletions common/src/bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::array::TryFromSliceError;
use std::convert::TryInto;
use std::mem;
use zerocopy::byteorder::big_endian;

pub trait TryFromByteSlice {
fn try_from_byte_slice(bytes: &[u8]) -> Result<&Self, TryFromSliceError>;
}

impl<const N: usize> TryFromByteSlice for [u8; N] {
fn try_from_byte_slice(bytes: &[u8]) -> Result<&[u8; N], TryFromSliceError> {
bytes.try_into()
}
}

pub trait ByteArray {
type ByteArray: AsRef<[u8]> + TryFromByteSlice;
}

pub trait AsBytesExt: ByteArray + zerocopy::AsBytes {
fn as_byte_array(&self) -> &Self::ByteArray {
TryFromByteSlice::try_from_byte_slice(self.as_bytes()).unwrap()
}
}

pub trait FromBytesExt: ByteArray + zerocopy::FromBytes {
fn ref_and_rest_from(bytes: &[u8]) -> Option<(&Self, &[u8])>
where
Self: Sized,
{
if bytes.len() < mem::size_of::<Self>() {
return None;
}
let (result, rest) = bytes.split_at(mem::size_of::<Self>());
Some((Self::ref_from(result).unwrap(), rest))
}
fn ref_from_array(bytes: &Self::ByteArray) -> &Self
where
Self: Sized,
{
Self::ref_from(bytes.as_ref()).unwrap()
}
fn from_array(bytes: Self::ByteArray) -> Self
where
Self: Copy + Sized,
{
*Self::ref_from_array(&bytes)
}
}

impl<T: ByteArray + zerocopy::AsBytes> AsBytesExt for T {}
impl<T: ByteArray + zerocopy::FromBytes> FromBytesExt for T {}

boilerplate_packed_internal!(big_endian::U32, 4, test_be_u32_size);
2 changes: 2 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate arrayvec;
extern crate file_offset;
#[cfg(feature = "serde")]
extern crate serde;
extern crate zerocopy;

pub use map_iter::MapIterator;
pub use slice::relative_size_of;
Expand All @@ -16,6 +17,7 @@ pub use takeable::Takeable;
#[macro_use]
mod macros;

pub mod bytes;
pub mod digest;
pub mod io;
pub mod map_iter;
Expand Down
36 changes: 15 additions & 21 deletions common/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,27 @@ macro_rules! unwrap_or {
}

#[macro_export]
macro_rules! unsafe_boilerplate_packed {
($t:ty, $size:expr, $ts:ident, $ta:ident) => {
macro_rules! boilerplate_packed {
($t:ty, $size:expr, $ts:ident) => {
#[test]
fn $ts() {
assert_eq!(::std::mem::size_of::<$t>(), $size);
}
impl ::common::bytes::ByteArray for $t {
type ByteArray = [u8; $size];
}
};
}

#[macro_export]
macro_rules! boilerplate_packed_internal {
($t:ty, $size:expr, $ts:ident) => {
#[test]
fn $ta() {
assert_eq!(::std::mem::align_of::<$t>(), 1);
fn $ts() {
assert_eq!(::std::mem::size_of::<$t>(), $size);
}
impl $t {
pub fn as_bytes(&self) -> &[u8; $size] {
unsafe { &*(self as *const _ as *const [u8; $size]) }
}
pub fn from_bytes(bytes: &[u8; $size]) -> &$t {
unsafe { &*(bytes as *const _ as *const $t) }
}
pub fn from_byte_slice(bytes: &[u8]) -> Option<(&$t, &[u8])> {
if bytes.len() < $size {
return None;
}
let (struct_bytes, rest) = bytes.split_at($size);
let struct_ = <$t>::from_bytes(unsafe {
&*(&struct_bytes[0] as *const _ as *const [u8; $size])
});
Some((struct_, rest))
}
impl crate::bytes::ByteArray for $t {
type ByteArray = [u8; $size];
}
};
}
Loading

0 comments on commit ca6a8ff

Please sign in to comment.