-
Notifications
You must be signed in to change notification settings - Fork 105
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
Support casting to a KnownLayout
type with a user-provided length computed dynamically
#1289
Comments
(also mentioned in #5 (comment)) This feature could be achieved rather naturally by extending the validation routine to also extract length information. What if the result of enum BitValidity {
/// This &T contains invalid bits for the type.
Invalid,
/// This `&T` contains valid bits for the type.
Valid,
/// This `&T` would contain valid bits if the tail slice were truncated to this many elements.
///
/// If the tail slice already contains exactly this many elements, this is semantically identical to returning `Valid`.
ValidIfTruncatedTo(usize),
} That way |
I was toying around with this and got something to work that might be the seed of a good API: // Method on `FromBytes`
fn ref_from_bytes_with_length_field(
source: &[u8],
elems: impl FnOnce(&Self) -> usize,
) -> Result<&Self, CastError<&[u8], Self>>
where
Self: KnownLayout<PointerMetadata = usize> + Immutable,
{
let slf = Self::ref_from_bytes_with_elems(source, 0)?;
let count = elems(slf);
Self::ref_from_bytes_with_elems(source, count)
} |
Consolidating #1328 into this issue. Authored by @jswrenn. See also: #5 (comment) Addresses #1289. Discussion of alternative solutions should occur there, or in their own issues. Many formats include a #[derive(FromBytes, KnownLayout, Immutable)]
#[repr(C)]
struct Packet {
src_port: [u8; 2],
dst_port: [u8; 2],
length: [u8; 2],
checksum: [u8; 2],
body: [u8],
} Such types are inconvenient to correctly parse in zerocopy; the We could potentially simplify this with a #[derive(FromBytes, KnownLayout, Immutable)]
#[repr(C)]
struct Packet {
src_port: [u8; 2],
dst_port: [u8; 2],
#[zerocopy::length]
length: [u8; 2],
checksum: [u8; 2],
body: [u8],
} ...such that Some considerations:
@kupiakos on May 20 2024
@kupiakos on May 20 2024
|
See also: #1290, #1328, #5 (comment)
Requirements
packet::BufferView::take_front
and friendsDetails
Some formats have an explicit length field, and some use cases with these formats require parsing a subset of the available bytes based on that length field. We'd like to write something like:
Unfortunately, all of the conversions we permit today require the number of bytes to be parsed to be known ahead of time - either it's simply the entire source byte slice, or it's computed from a fixed number of trailing slice elements.
Ideally, we could support an API that permits the caller to specify how to extract the length and then use that to determine the number of bytes to parse.
One idea: Do one parsing pass, then allow the user to provide a callback which extracts the length field. Finally, re-parse using the extracted length. There may be multiple axes we need to consider:
usize
, or do we need to support other numeric types?The text was updated successfully, but these errors were encountered: