Skip to content

Commit

Permalink
Merge 99888af into d045596
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a authored Aug 6, 2023
2 parents d045596 + 99888af commit 0682ede
Show file tree
Hide file tree
Showing 64 changed files with 1,704 additions and 976 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ jobs:
toolchain: nightly
override: true
components: miri
target: mips64-unknown-linux-gnuabi64
- run: cargo miri test --target mips64-unknown-linux-gnuabi64
target: armebv7r-none-eabi
- run: cargo miri test --target armebv7r-none-eabi

examples:
name: Examples
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
52 changes: 36 additions & 16 deletions benches/deku.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::io::Read;

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

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
Expand All @@ -10,8 +13,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 +46,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 +125,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 +142,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 0682ede

Please sign in to comment.