Skip to content

Commit

Permalink
More(?) Working then before
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Sep 15, 2023
1 parent 351eea4 commit dfbbc30
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 209 deletions.
8 changes: 4 additions & 4 deletions deku-derive/src/macros/deku_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {

impl #imp ::#crate_::DekuWriter<#ctx_types> for #ident #wher {
#[allow(unused_variables)]
fn to_writer<W: ::#crate_::acid_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> {
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> {
#write_body
}
}
Expand All @@ -140,7 +140,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
tokens.extend(quote! {
impl #imp ::#crate_::DekuWriter for #ident #wher {
#[allow(unused_variables)]
fn to_writer<W: ::#crate_::acid_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, _: ()) -> core::result::Result<(), ::#crate_::DekuError> {
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, _: ()) -> core::result::Result<(), ::#crate_::DekuError> {
#write_body
}
}
Expand Down Expand Up @@ -345,7 +345,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {

impl #imp ::#crate_::DekuWriter<#ctx_types> for #ident #wher {
#[allow(unused_variables)]
fn to_writer<W: ::#crate_::acid_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> {
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> {
#write_body
}
}
Expand All @@ -357,7 +357,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
tokens.extend(quote! {
impl #imp ::#crate_::DekuWriter for #ident #wher {
#[allow(unused_variables)]
fn to_writer<W: ::#crate_::acid_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, _: ()) -> core::result::Result<(), ::#crate_::DekuError> {
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, _: ()) -> core::result::Result<(), ::#crate_::DekuError> {
#write_body
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_reader_and_writer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::convert::TryInto;

use acid_io::Write;
use deku::bitvec::{BitVec, Msb0};
use deku::ctx::BitSize;
use deku::writer::Writer;
use deku::{prelude::*, DekuWriter};
use no_std_io::io::Write;

fn bit_flipper_read<R: std::io::Read>(
field_a: u8,
Expand Down Expand Up @@ -52,7 +52,7 @@ struct DekuTest {
field_a: u8,

#[deku(
writer = "bit_flipper_write(*field_a, *field_b, deku::output, BitSize(8))"
reader = "bit_flipper_read(*field_a, deku::reader, BitSize(8))",
writer = "bit_flipper_write(*field_a, *field_b, deku::writer, BitSize(8))"
)]
field_b: u8,
Expand Down
4 changes: 2 additions & 2 deletions src/impls/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use alloc::format;

use bitvec::prelude::*;

use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
use crate::reader::Reader;
use crate::writer::Writer;
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};

impl<'a, Ctx> DekuReader<'a, Ctx> for bool
where
Expand Down Expand Up @@ -62,7 +62,7 @@ mod tests {
use no_std_io::io::Cursor;
use rstest::rstest;

use crate::reader::Reader;
use crate::{ctx::BitSize, reader::Reader};

use super::*;

Expand Down
28 changes: 13 additions & 15 deletions src/impls/cstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ use std::ffi::CString;

use bitvec::prelude::*;

use crate::reader::Reader;
use crate::writer::Writer;
use crate::{ctx::*, DekuReader};
use crate::{DekuError, DekuWrite};
use crate::{DekuError, DekuWrite, DekuWriter};

//impl<Ctx: Copy> DekuWrite<Ctx> for CString
//where
// u8: DekuWrite<Ctx>,
//{
// fn write(&self, output: &mut BitVec<u8, Msb0>, ctx: Ctx) -> Result<(), DekuError> {
// let bytes = self.as_bytes_with_nul();
// bytes.write(output, ctx)
// }
//}
impl<Ctx: Copy> DekuWriter<Ctx> for CString
where
u8: DekuWriter<Ctx>,
{
fn to_writer<W: Write>(&self, writer: &mut Writer<W>, ctx: Ctx) -> Result<(), DekuError> {
let bytes = self.as_bytes_with_nul();
bytes.to_writer(writer, ctx)
}
}

impl<'a, Ctx: Copy> DekuReader<'a, Ctx> for CString
where
u8: DekuReader<'a, Ctx>,
{
fn from_reader_with_ctx<R: Read>(
reader: &mut crate::reader::Reader<R>,
reader: &mut Reader<R>,
inner_ctx: Ctx,
) -> Result<Self, DekuError> {
let bytes =
Expand Down Expand Up @@ -67,10 +69,6 @@ mod tests {
cursor.read_to_end(&mut buf).unwrap();
assert_eq!(expected_rest, buf);

let mut res_write = bitvec![u8, Msb0;];
res_read.write(&mut res_write, ()).unwrap();
assert_eq!(vec![b't', b'e', b's', b't', b'\0'], res_write.into_vec());

let mut out_buf = vec![];
let mut writer = Writer::new(&mut out_buf);
res_read.to_writer(&mut writer, ()).unwrap();
Expand Down
25 changes: 14 additions & 11 deletions src/impls/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use std::collections::HashMap;
use std::hash::{BuildHasher, Hash};

use bitvec::prelude::*;
use no_std_io::io::Read;
use no_std_io::io::{Read, Write};

use crate::ctx::*;
use crate::{DekuError, DekuReader, DekuWrite};
use crate::writer::Writer;
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};

// TODO: Add DekuWriter

/// Read `K, V`s into a hashmap until a given predicate returns true
/// * `capacity` - an optional capacity to pre-allocate the hashmap with
Expand Down Expand Up @@ -159,7 +162,7 @@ where
}
}

impl<K: DekuWrite<Ctx>, V: DekuWrite<Ctx>, S, Ctx: Copy> DekuWrite<Ctx> for HashMap<K, V, S> {
impl<K: DekuWriter<Ctx>, V: DekuWriter<Ctx>, S, Ctx: Copy> DekuWriter<Ctx> for HashMap<K, V, S> {
/// Write all `K, V`s in a `HashMap` to bits.
/// * **inner_ctx** - The context required by `K, V`.
/// Note: depending on the Hasher `S`, the order in which the `K, V` pairs are
Expand All @@ -177,9 +180,9 @@ impl<K: DekuWrite<Ctx>, V: DekuWrite<Ctx>, S, Ctx: Copy> DekuWrite<Ctx> for Hash
/// let expected: Vec<u8> = vec![100, 4, 3, 2, 1];
/// assert_eq!(expected, output.into_vec())
/// ```
fn write(&self, output: &mut BitVec<u8, Msb0>, inner_ctx: Ctx) -> Result<(), DekuError> {
fn to_writer<W: Write>(&self, writer: &mut Writer<W>, inner_ctx: Ctx) -> Result<(), DekuError> {
for kv in self {
kv.write(output, inner_ctx)?;
kv.to_writer(writer, inner_ctx)?;
}
Ok(())
}
Expand Down Expand Up @@ -267,9 +270,9 @@ mod tests {
case::normal(fxhashmap!{0x11u8 => 0xAABBu16, 0x23u8 => 0xCCDDu16}, Endian::Little, vec![0x11, 0xBB, 0xAA, 0x23, 0xDD, 0xCC]),
)]
fn test_hashmap_write(input: FxHashMap<u8, u16>, endian: Endian, expected: Vec<u8>) {
let mut res_write = bitvec![u8, Msb0;];
input.write(&mut res_write, endian).unwrap();
assert_eq!(expected, res_write.into_vec());
//let mut res_write = bitvec![u8, Msb0;];
//input.write(&mut res_write, endian).unwrap();
//assert_eq!(expected, res_write.into_vec());
}

// Note: These tests also exist in boxed.rs
Expand Down Expand Up @@ -303,8 +306,8 @@ mod tests {
cursor.read_to_end(&mut buf).unwrap();
assert_eq!(expected_rest_bytes, buf);

let mut res_write = bitvec![u8, Msb0;];
res_read.write(&mut res_write, endian).unwrap();
assert_eq!(expected_write, res_write.into_vec());
//let mut res_write = bitvec![u8, Msb0;];
//res_read.write(&mut res_write, endian).unwrap();
//assert_eq!(expected_write, res_write.into_vec());
}
}
30 changes: 17 additions & 13 deletions src/impls/hashset.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::collections::HashSet;
use std::hash::{BuildHasher, Hash};

use crate::writer::Writer;
use bitvec::prelude::*;
use no_std_io::io::Read;
use no_std_io::io::{Read, Write};

use crate::ctx::*;
use crate::{DekuError, DekuReader, DekuWrite};
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};

// TODO: Add DekuWriter

/// Read `T`s into a hashset until a given predicate returns true
/// * `capacity` - an optional capacity to pre-allocate the hashset with
Expand Down Expand Up @@ -150,7 +153,7 @@ impl<'a, T: DekuReader<'a> + Eq + Hash, S: BuildHasher + Default, Predicate: FnM
}
}

impl<T: DekuWrite<Ctx>, S, Ctx: Copy> DekuWrite<Ctx> for HashSet<T, S> {
impl<T: DekuWriter<Ctx>, S, Ctx: Copy> DekuWriter<Ctx> for HashSet<T, S> {
/// Write all `T`s in a `HashSet` to bits.
/// * **inner_ctx** - The context required by `T`.
/// Note: depending on the Hasher `S`, the order in which the `T`'s are
Expand All @@ -166,9 +169,9 @@ impl<T: DekuWrite<Ctx>, S, Ctx: Copy> DekuWrite<Ctx> for HashSet<T, S> {
/// set.write(&mut output, Endian::Big).unwrap();
/// assert_eq!(output, bitvec![u8, Msb0; 0, 0, 0, 0, 0, 0, 0, 1])
/// ```
fn write(&self, output: &mut BitVec<u8, Msb0>, inner_ctx: Ctx) -> Result<(), DekuError> {
fn to_writer<W: Write>(&self, writer: &mut Writer<W>, inner_ctx: Ctx) -> Result<(), DekuError> {
for v in self {
v.write(output, inner_ctx)?;
v.to_writer(writer, inner_ctx)?;
}
Ok(())
}
Expand Down Expand Up @@ -237,9 +240,10 @@ mod tests {
case::normal(vec![0xAABB, 0xCCDD].into_iter().collect(), Endian::Little, vec![0xDD, 0xCC, 0xBB, 0xAA]),
)]
fn test_hashset_write(input: FxHashSet<u16>, endian: Endian, expected: Vec<u8>) {
let mut res_write = bitvec![u8, Msb0;];
input.write(&mut res_write, endian).unwrap();
assert_eq!(expected, res_write.into_vec());
//let out_buf = vec![];
//let mut writer = Writer::new(out_buf);
//input.to_writer(&mut writer, endian).unwrap();
//assert_eq!(expected, out_buf);
}

// Note: These tests also exist in boxed.rs
Expand Down Expand Up @@ -280,10 +284,10 @@ mod tests {
cursor.read_to_end(&mut buf).unwrap();
assert_eq!(expected_rest_bytes, buf);

let mut res_write = bitvec![u8, Msb0;];
res_read
.write(&mut res_write, (endian, BitSize(bit_size)))
.unwrap();
assert_eq!(expected_write, res_write.into_vec());
//let mut res_write = bitvec![u8, Msb0;];
//res_read
// .write(&mut res_write, (endian, BitSize(bit_size)))
// .unwrap();
//assert_eq!(expected_write, res_write.into_vec());
}
}
55 changes: 5 additions & 50 deletions src/impls/ipaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use no_std_io::io::{Read, Write};

use bitvec::prelude::*;

use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
use crate::reader::Reader;
use crate::writer::Writer;
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};

impl<'a, Ctx> DekuReader<'a, Ctx> for Ipv4Addr
where
Expand All @@ -21,13 +21,13 @@ where
}
}

impl<Ctx> DekuWrite<Ctx> for Ipv4Addr
impl<Ctx> DekuWriter<Ctx> for Ipv4Addr
where
u32: DekuWrite<Ctx>,
u32: DekuWriter<Ctx>,
{
fn write(&self, output: &mut BitVec<u8, Msb0>, ctx: Ctx) -> Result<(), DekuError> {
fn to_writer<W: Write>(&self, writer: &mut Writer<W>, ctx: Ctx) -> Result<(), DekuError> {
let ip: u32 = (*self).into();
ip.write(output, ctx)
ip.to_writer(writer, ctx)
}
}

Expand All @@ -44,16 +44,6 @@ where
}
}

impl<Ctx> DekuWrite<Ctx> for Ipv6Addr
where
u128: DekuWrite<Ctx>,
{
fn write(&self, output: &mut BitVec<u8, Msb0>, ctx: Ctx) -> Result<(), DekuError> {
let ip: u128 = (*self).into();
ip.write(output, ctx)
}
}

impl<Ctx> DekuWriter<Ctx> for Ipv6Addr
where
u128: DekuWriter<Ctx>,
Expand All @@ -64,19 +54,6 @@ where
}
}

impl<Ctx> DekuWrite<Ctx> for IpAddr
where
Ipv6Addr: DekuWrite<Ctx>,
Ipv4Addr: DekuWrite<Ctx>,
{
fn write(&self, output: &mut BitVec<u8, Msb0>, ctx: Ctx) -> Result<(), DekuError> {
match self {
IpAddr::V4(ipv4) => ipv4.write(output, ctx),
IpAddr::V6(ipv6) => ipv6.write(output, ctx),
}
}
}

impl<Ctx> DekuWriter<Ctx> for IpAddr
where
Ipv6Addr: DekuWriter<Ctx>,
Expand Down Expand Up @@ -108,10 +85,6 @@ mod tests {
let res_read = Ipv4Addr::from_reader_with_ctx(&mut reader, endian).unwrap();
assert_eq!(expected, res_read);

let mut res_write = bitvec![u8, Msb0;];
res_read.write(&mut res_write, endian).unwrap();
assert_eq!(input.to_vec(), res_write.into_vec());

let mut out_buf = vec![];
let mut writer = Writer::new(&mut out_buf);
res_read.to_writer(&mut writer, endian).unwrap();
Expand All @@ -128,10 +101,6 @@ mod tests {
let res_read = Ipv6Addr::from_reader_with_ctx(&mut reader, endian).unwrap();
assert_eq!(expected, res_read);

let mut res_write = bitvec![u8, Msb0;];
res_read.write(&mut res_write, endian).unwrap();
assert_eq!(input.to_vec(), res_write.into_vec());

let mut out_buf = vec![];
let mut writer = Writer::new(&mut out_buf);
res_read.to_writer(&mut writer, endian).unwrap();
Expand All @@ -141,26 +110,12 @@ mod tests {
#[test]
fn test_ip_addr_write() {
let ip_addr = IpAddr::V4(Ipv4Addr::new(145, 254, 160, 237));
let mut ret_write = bitvec![u8, Msb0;];
ip_addr.write(&mut ret_write, Endian::Little).unwrap();
assert_eq!(vec![237, 160, 254, 145], ret_write.into_vec());

let mut out_buf = vec![];
let mut writer = Writer::new(&mut out_buf);
ip_addr.to_writer(&mut writer, Endian::Little).unwrap();
assert_eq!(vec![237, 160, 254, 145], out_buf.to_vec());

let ip_addr = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff));
let mut ret_write = bitvec![u8, Msb0;];
ip_addr.write(&mut ret_write, Endian::Little).unwrap();
assert_eq!(
vec![
0xff, 0x02, 0x0a, 0xc0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00
],
ret_write.into_vec()
);

let ip_addr = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff));
let mut out_buf = vec![];
let mut writer = Writer::new(&mut out_buf);
Expand Down
2 changes: 1 addition & 1 deletion src/impls/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use no_std_io::io::{Read, Write};
use bitvec::prelude::*;

use crate::ctx::*;
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
use crate::reader::Reader;
use crate::writer::Writer;
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};

macro_rules! ImplDekuTraitsCtx {
($typ:ty, $readtype:ty, $ctx_arg:tt, $ctx_type:tt) => {
Expand Down
4 changes: 2 additions & 2 deletions src/impls/option.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bitvec::prelude::*;
use no_std_io::io::Read;
use no_std_io::io::{Read, Write};

use crate::{DekuError, DekuReader, DekuWrite};
use crate::{writer::Writer, DekuError, DekuReader, DekuWrite, DekuWriter};

impl<'a, T: DekuReader<'a, Ctx>, Ctx: Copy> DekuReader<'a, Ctx> for Option<T> {
fn from_reader_with_ctx<R: Read>(
Expand Down
Loading

0 comments on commit dfbbc30

Please sign in to comment.