-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5f089f
commit ca6a8ff
Showing
37 changed files
with
357 additions
and
420 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.