Skip to content

Initial version of as attribute#226

Open
coolreader18 wants to merge 1 commit intosharksforarms:masterfrom
coolreader18:deku-as
Open

Initial version of as attribute#226
coolreader18 wants to merge 1 commit intosharksforarms:masterfrom
coolreader18:deku-as

Conversation

@coolreader18
Copy link
Copy Markdown

Resolves #225.

Here's an initial draft of the "as" attribute, let me know what you think. Note that as of tomorrow, I'll be going to summer camp, so I may not be able to work on this very much; sorry about that 😅 It should really just be docs though, unless you want this initial PR to contain types that implement DekuRead/WriteAs as well.

@sharksforarms
Copy link
Copy Markdown
Owner

This is neat! Thanks for making a PR. I'll give it a review shortly!

I propose adding an example, here's something I came up with to try it out. Feel free to add it to the PR.

use deku::prelude::*;
use std::net::Ipv4Addr;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct Data {
    // Type does not implement DekuRead or DekuWrite
    #[deku(as = "MyIpAddr")]
    address: Ipv4Addr,
}

impl<'a> deku::DekuReadAs<'a, Ipv4Addr> for MyIpAddr {
    fn read_as(
        input: &'a deku::bitvec::BitSlice<deku::bitvec::Msb0, u8>,
        ctx: (),
    ) -> Result<(&'a deku::bitvec::BitSlice<deku::bitvec::Msb0, u8>, Ipv4Addr), DekuError> {
        u32::read(input, ctx).map(|(rest, v)| (rest, Ipv4Addr::from(v)))
    }
}

impl deku::DekuWriteAs<Ipv4Addr> for MyIpAddr {
    fn write_as(
        source: &Ipv4Addr,
        output: &mut deku::bitvec::BitVec<deku::bitvec::Msb0, u8>,
        ctx: (),
    ) -> Result<(), DekuError> {
        let ip: u32 = (*source).into();
        ip.write(output, ctx)
    }
}

struct MyIpAddr {}

fn main() {
    let data = Data {
        address: "127.0.0.1".parse().unwrap(),
    };

    let data_write = data.to_bytes().unwrap();
    assert_eq!(vec![1, 0, 0, 127], data_write);

    let (_rest, data_read) = Data::from_bytes((&data_write, 0)).unwrap();
    assert_eq!(data, data_read);
}

Copy link
Copy Markdown
Owner

@sharksforarms sharksforarms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a great first pass!

There's some clippy/build warnings mostly related to documentation. Docs will be important so users know how to use this awesome feature.

Comment thread tests/test_as.rs

#[derive(DekuRead, DekuWrite, PartialEq, Debug)]
struct Foo {
#[deku(as = "VecWithLen<Same>", bytes = "4", ctx = "()")]
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what you're trying to achieve with Same, however it seems slightly non-intuitive.

What about just T ?

Suggested change
#[deku(as = "VecWithLen<Same>", bytes = "4", ctx = "()")]
#[deku(as = "VecWithLen<deku::T>", bytes = "4", ctx = "()")]

Comment thread src/lib.rs
}

pub struct Same {
_priv: (),
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the intention of _priv? So that users cannot construct a Same ?

Comment thread tests/test_as.rs
output: &mut bitvec::BitVec<bitvec::Msb0, u8>,
(size, ctx): (Size, Ctx),
) -> Result<(), DekuError> {
dbg!(&output);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: leftover dbg!

@sharksforarms
Copy link
Copy Markdown
Owner

Hey @coolreader18 just a friendly ping! Are you still interested in working on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#[deku(as = "")]/#[deku(with = "")] attributes

2 participants