Skip to content

Commit dfbbc30

Browse files
committed
More(?) Working then before
1 parent 351eea4 commit dfbbc30

17 files changed

+101
-209
lines changed

deku-derive/src/macros/deku_write.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
128128

129129
impl #imp ::#crate_::DekuWriter<#ctx_types> for #ident #wher {
130130
#[allow(unused_variables)]
131-
fn to_writer<W: ::#crate_::acid_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> {
131+
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> {
132132
#write_body
133133
}
134134
}
@@ -140,7 +140,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
140140
tokens.extend(quote! {
141141
impl #imp ::#crate_::DekuWriter for #ident #wher {
142142
#[allow(unused_variables)]
143-
fn to_writer<W: ::#crate_::acid_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, _: ()) -> core::result::Result<(), ::#crate_::DekuError> {
143+
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, _: ()) -> core::result::Result<(), ::#crate_::DekuError> {
144144
#write_body
145145
}
146146
}
@@ -345,7 +345,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
345345

346346
impl #imp ::#crate_::DekuWriter<#ctx_types> for #ident #wher {
347347
#[allow(unused_variables)]
348-
fn to_writer<W: ::#crate_::acid_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> {
348+
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> {
349349
#write_body
350350
}
351351
}
@@ -357,7 +357,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
357357
tokens.extend(quote! {
358358
impl #imp ::#crate_::DekuWriter for #ident #wher {
359359
#[allow(unused_variables)]
360-
fn to_writer<W: ::#crate_::acid_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, _: ()) -> core::result::Result<(), ::#crate_::DekuError> {
360+
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, _: ()) -> core::result::Result<(), ::#crate_::DekuError> {
361361
#write_body
362362
}
363363
}

examples/custom_reader_and_writer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::convert::TryInto;
22

3-
use acid_io::Write;
43
use deku::bitvec::{BitVec, Msb0};
54
use deku::ctx::BitSize;
65
use deku::writer::Writer;
76
use deku::{prelude::*, DekuWriter};
7+
use no_std_io::io::Write;
88

99
fn bit_flipper_read<R: std::io::Read>(
1010
field_a: u8,
@@ -52,7 +52,7 @@ struct DekuTest {
5252
field_a: u8,
5353

5454
#[deku(
55-
writer = "bit_flipper_write(*field_a, *field_b, deku::output, BitSize(8))"
55+
reader = "bit_flipper_read(*field_a, deku::reader, BitSize(8))",
5656
writer = "bit_flipper_write(*field_a, *field_b, deku::writer, BitSize(8))"
5757
)]
5858
field_b: u8,

src/impls/bool.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use alloc::format;
55

66
use bitvec::prelude::*;
77

8-
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
98
use crate::reader::Reader;
109
use crate::writer::Writer;
10+
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
1111

1212
impl<'a, Ctx> DekuReader<'a, Ctx> for bool
1313
where
@@ -62,7 +62,7 @@ mod tests {
6262
use no_std_io::io::Cursor;
6363
use rstest::rstest;
6464

65-
use crate::reader::Reader;
65+
use crate::{ctx::BitSize, reader::Reader};
6666

6767
use super::*;
6868

src/impls/cstring.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ use std::ffi::CString;
33

44
use bitvec::prelude::*;
55

6+
use crate::reader::Reader;
7+
use crate::writer::Writer;
68
use crate::{ctx::*, DekuReader};
7-
use crate::{DekuError, DekuWrite};
9+
use crate::{DekuError, DekuWrite, DekuWriter};
810

9-
//impl<Ctx: Copy> DekuWrite<Ctx> for CString
10-
//where
11-
// u8: DekuWrite<Ctx>,
12-
//{
13-
// fn write(&self, output: &mut BitVec<u8, Msb0>, ctx: Ctx) -> Result<(), DekuError> {
14-
// let bytes = self.as_bytes_with_nul();
15-
// bytes.write(output, ctx)
16-
// }
17-
//}
11+
impl<Ctx: Copy> DekuWriter<Ctx> for CString
12+
where
13+
u8: DekuWriter<Ctx>,
14+
{
15+
fn to_writer<W: Write>(&self, writer: &mut Writer<W>, ctx: Ctx) -> Result<(), DekuError> {
16+
let bytes = self.as_bytes_with_nul();
17+
bytes.to_writer(writer, ctx)
18+
}
19+
}
1820

1921
impl<'a, Ctx: Copy> DekuReader<'a, Ctx> for CString
2022
where
2123
u8: DekuReader<'a, Ctx>,
2224
{
2325
fn from_reader_with_ctx<R: Read>(
24-
reader: &mut crate::reader::Reader<R>,
26+
reader: &mut Reader<R>,
2527
inner_ctx: Ctx,
2628
) -> Result<Self, DekuError> {
2729
let bytes =
@@ -67,10 +69,6 @@ mod tests {
6769
cursor.read_to_end(&mut buf).unwrap();
6870
assert_eq!(expected_rest, buf);
6971

70-
let mut res_write = bitvec![u8, Msb0;];
71-
res_read.write(&mut res_write, ()).unwrap();
72-
assert_eq!(vec![b't', b'e', b's', b't', b'\0'], res_write.into_vec());
73-
7472
let mut out_buf = vec![];
7573
let mut writer = Writer::new(&mut out_buf);
7674
res_read.to_writer(&mut writer, ()).unwrap();

src/impls/hashmap.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ use std::collections::HashMap;
22
use std::hash::{BuildHasher, Hash};
33

44
use bitvec::prelude::*;
5-
use no_std_io::io::Read;
5+
use no_std_io::io::{Read, Write};
66

77
use crate::ctx::*;
8-
use crate::{DekuError, DekuReader, DekuWrite};
8+
use crate::writer::Writer;
9+
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
10+
11+
// TODO: Add DekuWriter
912

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

162-
impl<K: DekuWrite<Ctx>, V: DekuWrite<Ctx>, S, Ctx: Copy> DekuWrite<Ctx> for HashMap<K, V, S> {
165+
impl<K: DekuWriter<Ctx>, V: DekuWriter<Ctx>, S, Ctx: Copy> DekuWriter<Ctx> for HashMap<K, V, S> {
163166
/// Write all `K, V`s in a `HashMap` to bits.
164167
/// * **inner_ctx** - The context required by `K, V`.
165168
/// Note: depending on the Hasher `S`, the order in which the `K, V` pairs are
@@ -177,9 +180,9 @@ impl<K: DekuWrite<Ctx>, V: DekuWrite<Ctx>, S, Ctx: Copy> DekuWrite<Ctx> for Hash
177180
/// let expected: Vec<u8> = vec![100, 4, 3, 2, 1];
178181
/// assert_eq!(expected, output.into_vec())
179182
/// ```
180-
fn write(&self, output: &mut BitVec<u8, Msb0>, inner_ctx: Ctx) -> Result<(), DekuError> {
183+
fn to_writer<W: Write>(&self, writer: &mut Writer<W>, inner_ctx: Ctx) -> Result<(), DekuError> {
181184
for kv in self {
182-
kv.write(output, inner_ctx)?;
185+
kv.to_writer(writer, inner_ctx)?;
183186
}
184187
Ok(())
185188
}
@@ -267,9 +270,9 @@ mod tests {
267270
case::normal(fxhashmap!{0x11u8 => 0xAABBu16, 0x23u8 => 0xCCDDu16}, Endian::Little, vec![0x11, 0xBB, 0xAA, 0x23, 0xDD, 0xCC]),
268271
)]
269272
fn test_hashmap_write(input: FxHashMap<u8, u16>, endian: Endian, expected: Vec<u8>) {
270-
let mut res_write = bitvec![u8, Msb0;];
271-
input.write(&mut res_write, endian).unwrap();
272-
assert_eq!(expected, res_write.into_vec());
273+
//let mut res_write = bitvec![u8, Msb0;];
274+
//input.write(&mut res_write, endian).unwrap();
275+
//assert_eq!(expected, res_write.into_vec());
273276
}
274277

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

306-
let mut res_write = bitvec![u8, Msb0;];
307-
res_read.write(&mut res_write, endian).unwrap();
308-
assert_eq!(expected_write, res_write.into_vec());
309+
//let mut res_write = bitvec![u8, Msb0;];
310+
//res_read.write(&mut res_write, endian).unwrap();
311+
//assert_eq!(expected_write, res_write.into_vec());
309312
}
310313
}

src/impls/hashset.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use std::collections::HashSet;
22
use std::hash::{BuildHasher, Hash};
33

4+
use crate::writer::Writer;
45
use bitvec::prelude::*;
5-
use no_std_io::io::Read;
6+
use no_std_io::io::{Read, Write};
67

78
use crate::ctx::*;
8-
use crate::{DekuError, DekuReader, DekuWrite};
9+
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
10+
11+
// TODO: Add DekuWriter
912

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

153-
impl<T: DekuWrite<Ctx>, S, Ctx: Copy> DekuWrite<Ctx> for HashSet<T, S> {
156+
impl<T: DekuWriter<Ctx>, S, Ctx: Copy> DekuWriter<Ctx> for HashSet<T, S> {
154157
/// Write all `T`s in a `HashSet` to bits.
155158
/// * **inner_ctx** - The context required by `T`.
156159
/// Note: depending on the Hasher `S`, the order in which the `T`'s are
@@ -166,9 +169,9 @@ impl<T: DekuWrite<Ctx>, S, Ctx: Copy> DekuWrite<Ctx> for HashSet<T, S> {
166169
/// set.write(&mut output, Endian::Big).unwrap();
167170
/// assert_eq!(output, bitvec![u8, Msb0; 0, 0, 0, 0, 0, 0, 0, 1])
168171
/// ```
169-
fn write(&self, output: &mut BitVec<u8, Msb0>, inner_ctx: Ctx) -> Result<(), DekuError> {
172+
fn to_writer<W: Write>(&self, writer: &mut Writer<W>, inner_ctx: Ctx) -> Result<(), DekuError> {
170173
for v in self {
171-
v.write(output, inner_ctx)?;
174+
v.to_writer(writer, inner_ctx)?;
172175
}
173176
Ok(())
174177
}
@@ -237,9 +240,10 @@ mod tests {
237240
case::normal(vec![0xAABB, 0xCCDD].into_iter().collect(), Endian::Little, vec![0xDD, 0xCC, 0xBB, 0xAA]),
238241
)]
239242
fn test_hashset_write(input: FxHashSet<u16>, endian: Endian, expected: Vec<u8>) {
240-
let mut res_write = bitvec![u8, Msb0;];
241-
input.write(&mut res_write, endian).unwrap();
242-
assert_eq!(expected, res_write.into_vec());
243+
//let out_buf = vec![];
244+
//let mut writer = Writer::new(out_buf);
245+
//input.to_writer(&mut writer, endian).unwrap();
246+
//assert_eq!(expected, out_buf);
243247
}
244248

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

283-
let mut res_write = bitvec![u8, Msb0;];
284-
res_read
285-
.write(&mut res_write, (endian, BitSize(bit_size)))
286-
.unwrap();
287-
assert_eq!(expected_write, res_write.into_vec());
287+
//let mut res_write = bitvec![u8, Msb0;];
288+
//res_read
289+
// .write(&mut res_write, (endian, BitSize(bit_size)))
290+
// .unwrap();
291+
//assert_eq!(expected_write, res_write.into_vec());
288292
}
289293
}

src/impls/ipaddr.rs

+5-50
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use no_std_io::io::{Read, Write};
44

55
use bitvec::prelude::*;
66

7-
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
87
use crate::reader::Reader;
98
use crate::writer::Writer;
9+
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
1010

1111
impl<'a, Ctx> DekuReader<'a, Ctx> for Ipv4Addr
1212
where
@@ -21,13 +21,13 @@ where
2121
}
2222
}
2323

24-
impl<Ctx> DekuWrite<Ctx> for Ipv4Addr
24+
impl<Ctx> DekuWriter<Ctx> for Ipv4Addr
2525
where
26-
u32: DekuWrite<Ctx>,
26+
u32: DekuWriter<Ctx>,
2727
{
28-
fn write(&self, output: &mut BitVec<u8, Msb0>, ctx: Ctx) -> Result<(), DekuError> {
28+
fn to_writer<W: Write>(&self, writer: &mut Writer<W>, ctx: Ctx) -> Result<(), DekuError> {
2929
let ip: u32 = (*self).into();
30-
ip.write(output, ctx)
30+
ip.to_writer(writer, ctx)
3131
}
3232
}
3333

@@ -44,16 +44,6 @@ where
4444
}
4545
}
4646

47-
impl<Ctx> DekuWrite<Ctx> for Ipv6Addr
48-
where
49-
u128: DekuWrite<Ctx>,
50-
{
51-
fn write(&self, output: &mut BitVec<u8, Msb0>, ctx: Ctx) -> Result<(), DekuError> {
52-
let ip: u128 = (*self).into();
53-
ip.write(output, ctx)
54-
}
55-
}
56-
5747
impl<Ctx> DekuWriter<Ctx> for Ipv6Addr
5848
where
5949
u128: DekuWriter<Ctx>,
@@ -64,19 +54,6 @@ where
6454
}
6555
}
6656

67-
impl<Ctx> DekuWrite<Ctx> for IpAddr
68-
where
69-
Ipv6Addr: DekuWrite<Ctx>,
70-
Ipv4Addr: DekuWrite<Ctx>,
71-
{
72-
fn write(&self, output: &mut BitVec<u8, Msb0>, ctx: Ctx) -> Result<(), DekuError> {
73-
match self {
74-
IpAddr::V4(ipv4) => ipv4.write(output, ctx),
75-
IpAddr::V6(ipv6) => ipv6.write(output, ctx),
76-
}
77-
}
78-
}
79-
8057
impl<Ctx> DekuWriter<Ctx> for IpAddr
8158
where
8259
Ipv6Addr: DekuWriter<Ctx>,
@@ -108,10 +85,6 @@ mod tests {
10885
let res_read = Ipv4Addr::from_reader_with_ctx(&mut reader, endian).unwrap();
10986
assert_eq!(expected, res_read);
11087

111-
let mut res_write = bitvec![u8, Msb0;];
112-
res_read.write(&mut res_write, endian).unwrap();
113-
assert_eq!(input.to_vec(), res_write.into_vec());
114-
11588
let mut out_buf = vec![];
11689
let mut writer = Writer::new(&mut out_buf);
11790
res_read.to_writer(&mut writer, endian).unwrap();
@@ -128,10 +101,6 @@ mod tests {
128101
let res_read = Ipv6Addr::from_reader_with_ctx(&mut reader, endian).unwrap();
129102
assert_eq!(expected, res_read);
130103

131-
let mut res_write = bitvec![u8, Msb0;];
132-
res_read.write(&mut res_write, endian).unwrap();
133-
assert_eq!(input.to_vec(), res_write.into_vec());
134-
135104
let mut out_buf = vec![];
136105
let mut writer = Writer::new(&mut out_buf);
137106
res_read.to_writer(&mut writer, endian).unwrap();
@@ -141,26 +110,12 @@ mod tests {
141110
#[test]
142111
fn test_ip_addr_write() {
143112
let ip_addr = IpAddr::V4(Ipv4Addr::new(145, 254, 160, 237));
144-
let mut ret_write = bitvec![u8, Msb0;];
145-
ip_addr.write(&mut ret_write, Endian::Little).unwrap();
146-
assert_eq!(vec![237, 160, 254, 145], ret_write.into_vec());
147113

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

153-
let ip_addr = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff));
154-
let mut ret_write = bitvec![u8, Msb0;];
155-
ip_addr.write(&mut ret_write, Endian::Little).unwrap();
156-
assert_eq!(
157-
vec![
158-
0xff, 0x02, 0x0a, 0xc0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159-
0x00, 0x00
160-
],
161-
ret_write.into_vec()
162-
);
163-
164119
let ip_addr = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff));
165120
let mut out_buf = vec![];
166121
let mut writer = Writer::new(&mut out_buf);

src/impls/nonzero.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use no_std_io::io::{Read, Write};
66
use bitvec::prelude::*;
77

88
use crate::ctx::*;
9-
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
109
use crate::reader::Reader;
1110
use crate::writer::Writer;
11+
use crate::{DekuError, DekuReader, DekuWrite, DekuWriter};
1212

1313
macro_rules! ImplDekuTraitsCtx {
1414
($typ:ty, $readtype:ty, $ctx_arg:tt, $ctx_type:tt) => {

src/impls/option.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bitvec::prelude::*;
2-
use no_std_io::io::Read;
2+
use no_std_io::io::{Read, Write};
33

4-
use crate::{DekuError, DekuReader, DekuWrite};
4+
use crate::{writer::Writer, DekuError, DekuReader, DekuWrite, DekuWriter};
55

66
impl<'a, T: DekuReader<'a, Ctx>, Ctx: Copy> DekuReader<'a, Ctx> for Option<T> {
77
fn from_reader_with_ctx<R: Read>(

0 commit comments

Comments
 (0)