You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 IntoBytes for this data, but attempting to add that to ObjectAttributes, but I get an error:
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> se050/src/attributes.rs:81:24
|
81 | ...ytes, IntoBytes, KnownLayout, Immutable, Debug, PartialEq, Eq)]
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `ObjectAttributes`, the trait `Sized` is not implemented for `[u8]`
note: required because it appears within the type `ObjectAttributes`
--> se050/src/attributes.rs:83:12
|
83 | pub struct ObjectAttributes {
| ^^^^^^^^^^^^^^^^
= note: required for `ObjectAttributes` to implement `macro_util::__size_of::Sized`
note: required by a bound in `macro_util::__size_of::size_of`
--> /home/ryan.butler/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerocopy-0.8.27/src/util/macro_util.rs:349:29
|
349 | ...size_of<T: Sized + ?core::marker::Sized>() -> usize {
| ^^^^^ required by this bound in `size_of`
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> se050/src/attributes.rs:85:21
|
85 | pub policy_set: AttributesSuffix,
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `AttributesSuffix`, the trait `Sized` is not implemented for `[u8]`
note: required because it appears within the type `AttributesSuffix`
--> se050/src/attributes.rs:163:12
|
163 | pub struct AttributesSuffix([u8]);
| ^^^^^^^^^^^^^^^^
= note: required for `AttributesSuffix` to implement `macro_util::__size_of::Sized`
note: required by a bound in `macro_util::__size_of::size_of`
--> /home/ryan.butler/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerocopy-0.8.27/src/util/macro_util.rs:349:29
|
349 | ...size_of<T: Sized + ?core::marker::Sized>() -> usize {
| ^^^^^ required by this bound in `size_of`
For more information about this error, try `rustc --explain E0277`.
Here is (roughly) the code:
#[derive(TryFromBytes,KnownLayout,Immutable,Debug,PartialEq,Eq)]#[repr(C, align(1))]pubstructObjectAttributes{header:ObjectAttrsHeader,pubpolicy_set:AttributesSuffix,}#[derive(Debug, thiserror::Error)]#[error("size mismatch")]pubstructSizeErr;implObjectAttributes{// This exists because we cannot derive(IntoBytes) due to `policy_set` being// a DST.pubfnas_bytes(&self) -> (&[u8],&[u8]){(self.header.as_bytes(),self.policy_set.as_bytes())}pubfnlen(&self) -> usize{let header_len = self.header.as_bytes().len();let suffix_len = self.policy_set.as_bytes().len();
header_len + suffix_len
}pubfnwrite_to(&self,dst:&mut[u8]) -> Result<(),SizeErr>{let header_len = self.header.as_bytes().len();let suffix_len = self.policy_set.as_bytes().len();if dst.len() != header_len + suffix_len {returnErr(SizeErr);}self.header.write_to(&mut dst[0..header_len]).expect("infallible");self.policy_set.write_to(&mut dst[header_len..]).expect("infallible");Ok(())}}#[derive(TryFromBytes,IntoBytes,KnownLayout,Immutable,Debug,PartialEq,Eq,Clone,Copy,)]#[repr(C, align(1))]pubstructObjectAttrsHeader{// ...}#[derive(TryFromBytes,IntoBytes,KnownLayout,Immutable,Eq,PartialEq)]#[repr(C)]pubstructAttributesSuffix([u8]);
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 provenance
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
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 provenanceBeta Was this translation helpful? Give feedback.
All reactions