-
-
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
#[deku(as = "")]
/#[deku(with = "")]
attributes
#225
Comments
Same when writing the luajit bytecode parser. I'd appreciate if we have the as/with mechanism in deku. |
I'm doing something very similar to what deku does in wasmbin - self-generating parser/serializer for Wasm - where I'm using custom derive implementation. Example: https://github.com/GoogleChromeLabs/wasmbin/blob/3508d13398c59a337d42eaa8a49f9f02434fee9d/src/sections.rs#L197-L237 I was thinking of what would it take to try and reimplement it on top of deku instead, and I think lack of I think I'd like something even more general though - ideally I'd want to be able to define custom parsers for built-in types (e.g. LEB128 for integers, custom parser for floats, etc.) somewhere in a single place, and then somehow tell Deku to use those implementations for parsing, rather than putting UPD: I see |
Something like this, perhaps? EDIT: Can you show an example please of what you have in mind when you say you can get by with |
My idea was to use a custom marker to implement custom parsing for built-in types. Thanks to struct MyCtx;
impl DekuRead<'_, MyCtx> for u8 {
fn read(
input: &'a deku::bitvec::BitSlice<deku::bitvec::Msb0, u8>,
ctx: MyCtx,
) -> Result<(&'a deku::bitvec::BitSlice<deku::bitvec::Msb0, u8>, Self), DekuError>
where
Self: Sized {
todo!()
}
}
impl DekuRead<'_, MyCtx> for u16 { ... }
impl DekuRead<'_, MyCtx> for u32 { ... } Then, I can derive #[derive(DekuRead)]
#[deku(ctx = "_: MyCtx")]
struct MyStruct {
a: u8,
b: u16,
y: u32,
} This ensures that my structures get At this point the only missing bit is making sure that the same context is passed to all fields, so that entire type tree uses same context type consistently. Unfortunately, at this step I've realised that currently the only way to do so is to specify it on every field: #[derive(DekuRead)]
#[deku(ctx = "ctx: MyCtx")]
struct BBB {
#[deku(ctx = "ctx")]
a: u8,
#[deku(ctx = "ctx")]
b: u16,
#[deku(ctx = "ctx")]
y: u32,
} So, while this helps with boilerplate for types, we're back to square one in terms of specifying something for all fields and probably need a custom macro like suggested in #217... It's not very complicated to write one, but I wonder if it would make sense to add |
@RReverser @v1gnesh these are interesting suggestions, I never really thought about an override for built-in types. I like the solution above but it is verbose and kinda a hack way around it. I think we can do better. I'll give it some thought and experimentation this week. |
Sort-of related to #174, but more general.
I'm looking into converting RustPython's bytecode format to use deku over bincode (RustPython/RustPython#2362 (comment)), and I'm running into a few issues cause I want to keep the
DekuRead/Write
structs roughly as they are, i.e. ergonomic to use from Rust. For example, not adding separate length fields for deserializing vectors, and also I'd like to use LEB128 to represent most integers as our existing bincode impl does. I understand why those aren't defaults indeku
, but it'd be a lot easier to customizedeku
to meet our needs if there was something like serde'swith
attribute, or evenserde_with
'sas
attribute. Would you accept a PR that addsDeku{Read/Write}As
traits and anas
attribute to the derive macros so that individual field [de]serializing can be customized? I might just base it offserde_with
's design, since I think that seems to work well.The text was updated successfully, but these errors were encountered: