-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
base: master
Are you sure you want to change the base?
Conversation
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);
} |
There was a problem hiding this 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 = "()")] |
There was a problem hiding this comment.
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
?
#[deku(as = "VecWithLen<Same>", bytes = "4", ctx = "()")] | |
#[deku(as = "VecWithLen<deku::T>", bytes = "4", ctx = "()")] |
} | ||
|
||
pub struct Same { | ||
_priv: (), |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: leftover dbg!
Hey @coolreader18 just a friendly ping! Are you still interested in working on this? |
99a33f8
to
75edd42
Compare
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.