Repository navigation
Replies: 1 comment
|
Switch the outer struct from repr(C, align(1)) to repr(C, packed). The IntoBytes derive requires a non-generic repr(C) struct to be sized with no padding, which is the error you're hitting, but a packed struct only needs its fields to be IntoBytes, and slice DSTs are fine there. All your fields are align 1 already so the layout doesn't change. #[derive(TryFromBytes, IntoBytes, KnownLayout, Immutable)]
#[repr(C, packed)]
pub struct ObjectAttributes {
header: ObjectAttrsHeader,
pub policy_set: AttributesSuffix,
}
let attrs = ObjectAttributes::try_ref_from_bytes(&raw)?;
assert_eq!(attrs.as_bytes(), &raw[..]);Compiles and round-trips on 0.8 for me. You'll have to drop the Debug/PartialEq derives (rustc won't derive them through an unsized field in a packed struct), but implementing them over as_bytes() is trivial. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I am attempting to parse some real-world dynamically sized data. Due to the variable-length suffix of this data, I split it into two parts: a fixed size header (
ObjectAttrsHeader) and a dynamically sized suffix (AttributesSuffix).I'd really like to derive
IntoBytesfor this data, but attempting to add that toObjectAttributes, but I get an error:Here is (roughly) the code:
Is there any way to get the full contiguous original slice without needing
unsafe? Even with unsafe, I wasn't sure if its sound due to pointer provenanceAll reactions