Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial version of as attribute #226

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

coolreader18
Copy link

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
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
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.


#[derive(DekuRead, DekuWrite, PartialEq, Debug)]
struct Foo {
#[deku(as = "VecWithLen<Same>", bytes = "4", ctx = "()")]
Copy link
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 = "()")]

}

pub struct Same {
_priv: (),
Copy link
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 ?

output: &mut bitvec::BitVec<bitvec::Msb0, u8>,
(size, ctx): (Size, Ctx),
) -> Result<(), DekuError> {
dbg!(&output);
Copy link
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
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