Initial version of as attribute#226
Open
coolreader18 wants to merge 1 commit intosharksforarms:masterfrom
Open
Initial version of as attribute#226coolreader18 wants to merge 1 commit intosharksforarms:masterfrom
as attribute#226coolreader18 wants to merge 1 commit intosharksforarms:masterfrom
Conversation
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);
} |
Owner
sharksforarms
left a comment
There was a problem hiding this comment.
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 = "()")] |
Owner
There was a problem hiding this comment.
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: (), |
Owner
There was a problem hiding this comment.
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); |
Owner
|
Hey @coolreader18 just a friendly ping! Are you still interested in working on this? |
99a33f8 to
75edd42
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/WriteAsas well.