Skip to content

Commit

Permalink
Replace rkyv with zerocopy
Browse files Browse the repository at this point in the history
Turns out `rkyv` is more for defining a new binary encoding scheme
rather than reusing an existing one (which is the main purpose of
zorua). So, replacing it with the `zerocopy` crate.
- Reexported items from the zerocopy crate for to/from bytes traits.
- Also reexported endian-aware int/float types via the byteorder
feature of zerocopy.
- Unfortunately, until google/zerocopy#11 is resolved, downstream must
also import the zerocopy crate (ideally of the same minor version).
  • Loading branch information
prof64 committed Dec 10, 2023
1 parent d23da13 commit 5e3a795
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ edition = "2021"
[dependencies]
paste = "1.0.14"
arbitrary-int = "1.2.6"
rkyv = { version = "0.7.42", features = ["rend"] }
zerocopy = { version = "~0.7.30", features = ["byteorder"] }
88 changes: 64 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,60 @@
pub mod int {
#![allow(non_camel_case_types)]

pub mod transmute {
pub use zerocopy::{AsBytes, FromBytes, FromZeroes};
}

pub mod data_type {
pub use arbitrary_int::*;
pub use rkyv::rend::{
char_be, char_le,
f32_be, f32_le, f64_be, f64_le,
i16_be, i32_be, i64_be, i128_be,
i16_le, i32_le, i64_le, i128_le,
u16_be, u32_be, u64_be, u128_be,
u16_le, u32_le, u64_le, u128_le,
NonZeroI16_be, NonZeroI32_be, NonZeroI64_be, NonZeroI128_be,
NonZeroI16_le, NonZeroI32_le, NonZeroI64_le, NonZeroI128_le,
NonZeroU16_be, NonZeroU32_be, NonZeroU64_be, NonZeroU128_be,
NonZeroU16_le, NonZeroU32_le, NonZeroU64_le, NonZeroU128_le,
};

// Little Endian
use zerocopy::little_endian;
pub type u16_le = little_endian::U16;
pub type u32_le = little_endian::U32;
pub type u64_le = little_endian::U64;
pub type u128_le = little_endian::U128;
pub type i16_le = little_endian::I16;
pub type i32_le = little_endian::I32;
pub type i64_le = little_endian::I64;
pub type i128_le = little_endian::I128;
pub type f32_le = little_endian::F32;
pub type f64_le = little_endian::F64;

// Big Endian
use zerocopy::big_endian;
pub type u16_be = big_endian::U16;
pub type u32_be = big_endian::U32;
pub type u64_be = big_endian::U64;
pub type u128_be = big_endian::U128;
pub type i16_be = big_endian::I16;
pub type i32_be = big_endian::I32;
pub type i64_be = big_endian::I64;
pub type i128_be = big_endian::I128;
pub type f32_be = big_endian::F32;
pub type f64_be = big_endian::F64;
}

#[macro_use]
pub mod prelude {
pub use rkyv::Archive;
pub use crate::int::*;
pub use crate::data_type::*;
pub use crate::transmute::*;
pub use paste::paste;

#[macro_export]
macro_rules! zorua_wrapper {
($item: item) => {
#[repr(transparent)]
#[derive(
zerocopy::AsBytes, zerocopy::FromZeroes, zerocopy::FromBytes, Debug, PartialEq,
)]
$item
};
}
pub use zorua_wrapper;

//TODO: currently unable to have bitfields in a different size range...
#[macro_export]
macro_rules! zorua {
macro_rules! zorua_w_bitfields {
(impl "$sf_impl", $f:ident, $sfv:vis, $sf:ident, $sfs:expr, bool) => {
paste! {
$sfv fn $sf(&self) -> bool {
Expand Down Expand Up @@ -52,24 +84,32 @@ pub mod prelude {
};
(
$(#[$struct_meta:meta])*
$sv:vis $struct_name:ident {
$($fv:vis $f:ident : $ft:ty,
$($(|$sfv:vis $sf:ident : $sfs:expr;$sft:tt,)+)?
)*
}) => {
$sv:vis struct $struct_name:ident {
$($fv:vis $f:ident : $ft:ty,
$($(|$sfv:vis $sf:ident : $sfs:expr;$sft:tt,)+)?
)*
};
) => {
// Define the struct
#[repr(C)]
#[derive(
zerocopy::AsBytes,
zerocopy::FromZeroes,
zerocopy::FromBytes,
Debug,
PartialEq
)]
$(#[$struct_meta])*
$sv struct $struct_name {
$($fv $f: $ft),*
}
// Generate the impl block
impl $struct_name {
$($($(
zorua!(impl "$sf_impl", $f, $sfv, $sf, $sfs, $sft);
zorua_w_bitfields!(impl "$sf_impl", $f, $sfv, $sf, $sfs, $sft);
)+)?)*
}
};
}
pub use zorua;
}
pub use zorua_w_bitfields;
}

0 comments on commit 5e3a795

Please sign in to comment.