Skip to content

Commit

Permalink
Merge 0d80be6 into d045596
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a authored Aug 9, 2023
2 parents d045596 + 0d80be6 commit 42a62d8
Show file tree
Hide file tree
Showing 64 changed files with 1,784 additions and 1,012 deletions.
52 changes: 27 additions & 25 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,31 @@ jobs:
command: test
args: --all

test_miri:
name: Miri Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: miri
- run: cargo miri test

test_miri_big_endian:
name: Miri Test Big Endian
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: miri
target: mips64-unknown-linux-gnuabi64
- run: cargo miri test --target mips64-unknown-linux-gnuabi64
# TODO: Enable Miri
# test_miri:
# name: Miri Test
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - uses: actions-rs/toolchain@v1
# with:
# toolchain: nightly
# override: true
# components: miri
# - run: cargo miri test
#
# test_miri_big_endian:
# name: Miri Test Big Endian
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - uses: actions-rs/toolchain@v1
# with:
# toolchain: nightly
# override: true
# components: miri
# target: armebv7r-none-eabi
# - run: cargo miri test --target armebv7r-none-eabi

examples:
name: Examples
Expand Down Expand Up @@ -111,7 +112,8 @@ jobs:
with:
toolchain: nightly
override: true
- run: cd ensure_no_std && cargo run --release
target: thumbv7em-none-eabihf
- run: cd ensure_no_std && cargo build --release --target thumbv7em-none-eabihf

ensure_wasm:
name: Ensure wasm
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ members = [

[features]
default = ["std", "const_generics"]
std = ["deku_derive/std", "bitvec/std", "alloc"]
alloc = ["bitvec/alloc"]
std = ["deku_derive/std", "bitvec/std", "alloc", "acid_io/std", "acid_io/alloc"]
alloc = ["bitvec/alloc", "acid_io/alloc"]
logging = ["deku_derive/logging", "log"]
const_generics = []

[dependencies]
deku_derive = { version = "^0.16.0", path = "deku-derive", default-features = false}
bitvec = { version = "1.0.1", default-features = false }
log = { version = "0.4.17", optional = true }
acid_io = { git = "https://github.com/dataphract/acid_io.git", default-features = false, features = ["alloc"] }

[dev-dependencies]
rstest = "0.16.0"
Expand Down
51 changes: 35 additions & 16 deletions benches/deku.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::Read;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use deku::prelude::*;

Expand All @@ -10,8 +12,13 @@ struct DekuBits {
}

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuByte {
data: u8,
struct DekuBytes {
// #[deku(bytes = "1")] <=== This should be emitted!
data_00: u8,
// #[deku(bytes = "2")] <=== This should be emitted!
data_01: u16,
// #[deku(bytes = "4")] <=== This should be emitted!
data_02: u32,
}

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
Expand All @@ -38,55 +45,67 @@ struct DekuVec {
data: Vec<u8>,
}

fn deku_read_bits(input: &[u8]) {
let (_rest, _v) = DekuBits::from_bytes((input, 0)).unwrap();
fn deku_read_bits(reader: impl Read) {
let mut container = Container::new(reader);
let _v = DekuBits::from_reader(&mut container, ()).unwrap();
}

fn deku_write_bits(input: &DekuBits) {
let _v = input.to_bytes().unwrap();
}

fn deku_read_byte(input: &[u8]) {
let (_rest, _v) = DekuByte::from_bytes((input, 0)).unwrap();
fn deku_read_byte(reader: impl Read) {
let mut container = Container::new(reader);
let _v = DekuBytes::from_reader(&mut container, ()).unwrap();
}

fn deku_write_byte(input: &DekuByte) {
fn deku_write_byte(input: &DekuBytes) {
let _v = input.to_bytes().unwrap();
}

fn deku_read_enum(input: &[u8]) {
let (_rest, _v) = DekuEnum::from_bytes((input, 0)).unwrap();
let mut container = Container::new(input);
let _v = DekuEnum::from_reader(&mut container, ()).unwrap();
}

fn deku_write_enum(input: &DekuEnum) {
let _v = input.to_bytes().unwrap();
}

fn deku_read_vec(input: &[u8]) {
let (_rest, _v) = DekuVec::from_bytes((input, 0)).unwrap();
let mut container = Container::new(input);
let _v = DekuVec::from_reader(&mut container, ()).unwrap();
}

fn deku_write_vec(input: &DekuVec) {
let _v = input.to_bytes().unwrap();
}

fn deku_read_vec_perf(input: &[u8]) {
let (_rest, _v) = DekuVecPerf::from_bytes((input, 0)).unwrap();
let mut container = Container::new(std::io::Cursor::new(input));
let _v = DekuVecPerf::from_reader(&mut container, ()).unwrap();
}

fn deku_write_vec_perf(input: &DekuVecPerf) {
let _v = input.to_bytes().unwrap();
}

fn criterion_benchmark(c: &mut Criterion) {
let mut reader = std::io::repeat(0b101);
c.bench_function("deku_read_byte", |b| {
b.iter(|| deku_read_byte(black_box([0x01].as_ref())))
b.iter(|| deku_read_byte(black_box(&mut reader)))
});
c.bench_function("deku_write_byte", |b| {
b.iter(|| deku_write_byte(black_box(&DekuByte { data: 0x01 })))
b.iter(|| {
deku_write_byte(black_box(&DekuBytes {
data_00: 0x00,
data_01: 0x02,
data_02: 0x03,
}))
})
});
c.bench_function("deku_read_bits", |b| {
b.iter(|| deku_read_bits(black_box([0xf1].as_ref())))
b.iter(|| deku_read_bits(black_box(&mut reader)))
});
c.bench_function("deku_write_bits", |b| {
b.iter(|| {
Expand All @@ -105,13 +124,13 @@ fn criterion_benchmark(c: &mut Criterion) {
});

let deku_read_vec_input = {
let mut v = [0xFFu8; 101].to_vec();
let mut v = [0xffu8; 101].to_vec();
v[0] = 100u8;
v
};
let deku_write_vec_input = DekuVec {
count: 100,
data: vec![0xFF; 100],
data: vec![0xff; 100],
};
c.bench_function("deku_read_vec", |b| {
b.iter(|| deku_read_vec(black_box(&deku_read_vec_input)))
Expand All @@ -122,7 +141,7 @@ fn criterion_benchmark(c: &mut Criterion) {

let deku_write_vec_input = DekuVecPerf {
count: 100,
data: vec![0xFF; 100],
data: vec![0xff; 100],
};
c.bench_function("deku_read_vec_perf", |b| {
b.iter(|| deku_read_vec_perf(black_box(&deku_read_vec_input)))
Expand Down
20 changes: 12 additions & 8 deletions deku-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ Procedural macros that implement `DekuRead` and `DekuWrite` traits

#![warn(missing_docs)]

use crate::macros::{deku_read::emit_deku_read, deku_write::emit_deku_write};
use std::borrow::Cow;
use std::convert::TryFrom;

use darling::{ast, FromDeriveInput, FromField, FromMeta, FromVariant, ToTokens};
use proc_macro2::TokenStream;
use quote::quote;
use std::borrow::Cow;
use std::convert::TryFrom;
use syn::{punctuated::Punctuated, spanned::Spanned, AttributeArgs};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::AttributeArgs;

use crate::macros::deku_read::emit_deku_read;
use crate::macros::deku_write::emit_deku_write;

mod macros;

Expand Down Expand Up @@ -662,10 +667,8 @@ fn apply_replacements(input: &syn::LitStr) -> Result<Cow<'_, syn::LitStr>, Repla
}

let input_str = input_value
.replace("deku::input", "__deku_input") // part of the public API `from_bytes`
.replace("deku::input_bits", "__deku_input_bits") // part of the public API `read`
.replace("deku::container", "__deku_container")
.replace("deku::output", "__deku_output") // part of the public API `write`
.replace("deku::rest", "__deku_rest")
.replace("deku::bit_offset", "__deku_bit_offset")
.replace("deku::byte_offset", "__deku_byte_offset");

Expand Down Expand Up @@ -1006,10 +1009,11 @@ pub fn deku_derive(

#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
use syn::parse_str;

use super::*;

#[rstest(input,
// Valid struct
case::struct_empty(r#"struct Test {}"#),
Expand Down
Loading

0 comments on commit 42a62d8

Please sign in to comment.