Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Aug 1, 2023
1 parent 32dc0a0 commit a1015d7
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
18 changes: 16 additions & 2 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

use std::convert::{TryFrom, TryInto};

use deku::prelude::*;
use deku::{
container::Container,
ctx::{BitSize, ByteSize, Endian},
prelude::*,
};

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct FieldF {
Expand Down Expand Up @@ -49,8 +53,18 @@ fn main() {
]
.as_ref();

let test_deku = DekuTest::try_from(test_data).unwrap();
let mut container = Container::new(std::io::Cursor::new(test_data.clone()));
let a = u8::from_reader(&mut container, (Endian::Little, ByteSize(1)));
let b = u8::from_reader(&mut container, (Endian::Little, BitSize(7)));
let c = u8::from_reader(&mut container, (Endian::Little, BitSize(1)));
let d = u16::from_reader(&mut container, (Endian::Big, BitSize(16)));
println!("{a:02x?}");
println!("{b:02x?}");
println!("{c:02x?}");
println!("{d:02x?}");

let test_deku = DekuTest::try_from(test_data).unwrap();
println!("{test_deku:02x?}");
assert_eq!(
DekuTest {
field_a: 0xab,
Expand Down
86 changes: 86 additions & 0 deletions src/impls/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use alloc::format;
use core::convert::TryInto;

use bitvec::prelude::*;
use std::io::Read;

use crate::ctx::*;
use crate::prelude::NeedSize;
Expand All @@ -23,6 +24,15 @@ impl DekuRead<'_, (Endian, ByteSize)> for u8 {
let value = input[..MAX_TYPE_BITS].load::<u8>();
Ok((MAX_TYPE_BITS, value))
}

fn from_reader<R: Read>(
container: &mut crate::container::Container<R>,

Check failure on line 29 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Build

cannot find type `Container` in module `crate::container`

Check failure on line 29 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `Container` in module `crate::container`

Check failure on line 29 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `Container` in module `crate::container`
(endian, size): (Endian, ByteSize),
) -> Result<u8, DekuError> {
let mut bits = container.read_bits(8).unwrap();
let a = <u8>::read(&mut bits, (endian, size))?;
Ok(a.1)
}
}

macro_rules! ImplDekuReadBits {
Expand Down Expand Up @@ -111,6 +121,15 @@ macro_rules! ImplDekuReadBits {
};
Ok((bit_size, value))
}

fn from_reader<R: Read>(
container: &mut crate::container::Container<R>,

Check failure on line 126 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Build

cannot find type `Container` in module `crate::container`

Check failure on line 126 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Build

cannot find type `Container` in module `crate::container`

Check failure on line 126 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `Container` in module `crate::container`

Check failure on line 126 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `Container` in module `crate::container`

Check failure on line 126 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `Container` in module `crate::container`

Check failure on line 126 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `Container` in module `crate::container`
(endian, size): (Endian, BitSize),
) -> Result<$typ, DekuError> {
let mut bits = container.read_bits(size.0).unwrap();
let a = <$typ>::read(&mut bits, (endian, size))?;
Ok(a.1)
}
}
};
}
Expand Down Expand Up @@ -182,6 +201,15 @@ macro_rules! ImplDekuReadBytes {

Ok((bit_size, value))
}

fn from_reader<R: Read>(
container: &mut crate::container::Container<R>,
(endian, size): (Endian, ByteSize),
) -> Result<$typ, DekuError> {
let mut bits = container.read_bits(size.0 * 8).unwrap();
let a = <$typ>::read(&mut bits, (endian, size))?;
Ok(a.1)
}
}
};
}
Expand All @@ -202,7 +230,17 @@ macro_rules! ImplDekuReadSignExtend {
let value = (value as $typ) << shift >> shift;
Ok((amt_read, value))
}

fn from_reader<R: Read>(
container: &mut crate::container::Container<R>,
(endian, size): (Endian, ByteSize),
) -> Result<$typ, DekuError> {
let mut bits = container.read_bits(size.0 * 8).unwrap();
let a = <$typ>::read(&mut bits, (endian, size))?;
Ok(a.1)
}
}

impl DekuRead<'_, (Endian, BitSize)> for $typ {
fn read(
input: &BitSlice<u8, Msb0>,
Expand All @@ -217,6 +255,14 @@ macro_rules! ImplDekuReadSignExtend {
let value = (value as $typ) << shift >> shift;
Ok((amt_read, value))
}
fn from_reader<R: Read>(
container: &mut crate::container::Container<R>,
(endian, size): (Endian, BitSize),
) -> Result<$typ, DekuError> {
let mut bits = container.read_bits(size.0).unwrap();
let a = <$typ>::read(&mut bits, (endian, size))?;
Ok(a.1)
}
}
};
}
Expand All @@ -238,6 +284,17 @@ macro_rules! ForwardDekuRead {
<$typ>::read(input, (endian, bit_size))
}
}

fn from_reader<R: Read>(
container: &mut crate::container::Container<R>,

Check failure on line 289 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Build

cannot find type `Container` in module `crate::container`

Check failure on line 289 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Build

cannot find type `Container` in module `crate::container`

Check failure on line 289 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `Container` in module `crate::container`

Check failure on line 289 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `Container` in module `crate::container`

Check failure on line 289 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `Container` in module `crate::container`

Check failure on line 289 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `Container` in module `crate::container`
endian: Endian,
) -> Result<$typ, DekuError> {
let bit_size = BitSize::of::<$typ>();

let mut bits = container.read_bits(bit_size.0).unwrap();
let a = <$typ>::read(&mut bits, endian)?;
Ok(a.1)
}
}

// Only have `bit_size`, set `endian` to `Endian::default`.
Expand All @@ -250,6 +307,17 @@ macro_rules! ForwardDekuRead {

<$typ>::read(input, (endian, byte_size))
}

fn from_reader<R: Read>(
container: &mut crate::container::Container<R>,

Check failure on line 312 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Build

cannot find type `Container` in module `crate::container`

Check failure on line 312 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Build

cannot find type `Container` in module `crate::container`

Check failure on line 312 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `Container` in module `crate::container`

Check failure on line 312 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `Container` in module `crate::container`

Check failure on line 312 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `Container` in module `crate::container`

Check failure on line 312 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `Container` in module `crate::container`
byte_size: ByteSize,
) -> Result<$typ, DekuError> {
let endian = Endian::default();

let mut bits = container.read_bits(byte_size.0 * 8).unwrap();
let a = <$typ>::read(&mut bits, (endian, byte_size))?;
Ok(a.1)
}
}

// Only have `bit_size`, set `endian` to `Endian::default`.
Expand All @@ -267,12 +335,30 @@ macro_rules! ForwardDekuRead {
<$typ>::read(input, (endian, bit_size))
}
}

fn from_reader<R: Read>(
container: &mut crate::container::Container<R>,

Check failure on line 340 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Build

cannot find type `Container` in module `crate::container`

Check failure on line 340 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `Container` in module `crate::container`

Check failure on line 340 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `Container` in module `crate::container`
bit_size: BitSize,
) -> Result<$typ, DekuError> {
let endian = Endian::default();

let mut bits = container.read_bits(bit_size.0).unwrap();
let a = <$typ>::read(&mut bits, bit_size)?;
Ok(a.1)
}
}

impl DekuRead<'_> for $typ {
fn read(input: &BitSlice<u8, Msb0>, _: ()) -> Result<(usize, Self), DekuError> {
<$typ>::read(input, Endian::default())
}

fn from_reader<R: Read>(
container: &mut crate::container::Container<R>,

Check failure on line 357 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Build

cannot find type `Container` in module `crate::container`

Check failure on line 357 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `Container` in module `crate::container`

Check failure on line 357 in src/impls/primitive.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `Container` in module `crate::container`
_: (),
) -> Result<$typ, DekuError> {
<$typ>::from_reader(container, Endian::default())
}
}
};
}
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ pub struct EncodedString {
#[cfg(feature = "alloc")]
extern crate alloc;

use std::io::Read;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

Expand All @@ -281,6 +283,7 @@ pub mod bitvec {
pub use deku_derive::*;

pub mod attributes;
pub mod container;

Check failure on line 286 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build

file not found for module `container`

Check failure on line 286 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

file not found for module `container`

Check failure on line 286 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

file not found for module `container`
pub mod ctx;
pub mod error;
mod impls;
Expand All @@ -302,6 +305,16 @@ pub trait DekuRead<'a, Ctx = ()> {
) -> Result<(usize, Self), DekuError>
where
Self: Sized;

fn from_reader<R: Read>(
container: &mut crate::container::Container<R>,
ctx: Ctx,
) -> Result<Self, DekuError>
where
Self: Sized,
{
todo!();
}
}

/// "Reader" trait: implemented on DekuRead struct and enum containers. A `container` is a type which
Expand Down

0 comments on commit a1015d7

Please sign in to comment.