-
Notifications
You must be signed in to change notification settings - Fork 37
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
Tutorial: how to add paddings #48
Comments
Hmm, why doesn’t this work ? I’m also slightly confused by their spec; they say: call x the amount allocated to hold the string. But is the value in the struct field “Length” x or m? Anyway, your code looks good; for padding I don’t think there’s a robust approach. Do be aware that scroll basically reads and writes packed structs (which I think you’re aware) though. For your particular case I’d say you’re doing it just right except probably some off by one issue or other similar things when dealing with bits at that level, unless I’ve misunderstood your issue. Only other thing, and not sure precisely how it would work with your second struct with a str, but if you don’t want to allocate and are ok with unsafe grabbing a zero copy slice of your stream headers should be possible with |
Alternatively, you could play around with doing something like:
And then implement all the padding logic as a TryFromCtx impl for it, then you could If you had a similar wrapper for other non standard types, and as long as you update the offset appropriately in each, you should be then able to just derive the base Pread impl. Just exploring though, maybe not a great idea, some other issue later on with lifetimes or whatever comes up, etc :) |
No-no-no, I edited this issue several times. This code works seamless, I just wonder if there is some more "derivish" way to specify offset via attribute or something. I noticed that attribute works pretty well unless there are
I'm almost sure that
Hmm, interesting idea. However, in this case either I should write wrappers to extract underlying I just think that cases like |
Yes &str is zero copy, but I was referring to your Vec of stream headers. For the padded string yes would force client to use newtype or you can just extract the contents and place it in your struct in your TryFrom impl. But probably only worthwhile if you have multiple padding cases for strs. Dunno :) |
Ideally, it should be something like: #[repr(C)]
#[derive(Debug, Pread)]
struct MetadataRoot<'a> {
pub signature: u32,
pub major_version: u16,
pub minor_version: u16,
_reserved: u32,
pub length: u32,
#[padding(4)]
pub version: &'a str,
pub flags: u16,
pub streams: u16,
#[padding_each(4)]
pub stream_headers: Vec<StreamHeader<'a>>
}
#[repr(C)]
#[derive(Debug, Pread)]
struct StreamHeader<'a> {
pub offset: u32,
pub size: u32,
pub name: &'a str
}
Ah, ok. I don't seem much troubles in copying 2 x u32+reference :) The main reason for |
Ah yea, that’s very similar to #33. There is definitely some desire, and I’d be interested in a backwards compatible proposal, but I don’t think anyone’s found the time to play around with what it would look like, both at implementation level and api level. I will note it’s starting to turn scroll into a more complicated deserialization and serialization library, which was never it’s explicit goal; on the other hand it’s probably very reasonable to have padding like attributes or other such details for advanced use cases like yours ? |
Hey! It's me again.
I'm continuing implementing CLR runtime in rust, and I wonder if there is some ideomatic way to express paddings between fields. Let's take an example:
I'm writing following code:
I'd like to ask if there is more ideomatic way to express such a structure, and if there isn't, maybe it may give you some thoughts about implementing it? It may be really useful.
P.S. Great lib, thank you for your work :)
The text was updated successfully, but these errors were encountered: