-
Notifications
You must be signed in to change notification settings - Fork 108
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 slicing a KnownLayout
type
#1290
Comments
If we also encode the trailing slice element type in |
I wonder if there's some way that we could provide an associated element type only when |
We can probably solve this problem alongside #2149 in 0.9. In the meantime, I've been experimenting on a branch with having The interaction of dynamic padding and splitting is weird. Not insurmountably weird, but the semantics are going to be tricky to document. In particular, use zerocopy::*; // 0.8.16
#[derive(FromBytes, KnownLayout, Immutable)]
#[repr(C)]
struct Foo(u16, u8, [u8]);
fn main() {
let bytes = [0, 0, 1, 2];
let foo = Foo::ref_from_bytes(&bytes).unwrap();
let (bar, trailing) = foo.split_at(0);
assert_eq!(trailing, &[2]);
}
Rather, for any DST, there's a minimum element index at which the split could occur, and the parameter passed to |
Another gnarly type for our bestiary: #[repr(C)]
struct Foo(u32, u8, [[u8; 3]]); In this case, there needs to be at least three elements in the trailing slice. |
Maybe the thing to do is just to require |
Maaaaybe. For the sake of minimizing API bloat, I'd really like to see us approach this as a generalization of |
Keep in mind another edge case: there may be certain indexes which are valid and certain ones which are invalid, and there may be arbitrarily many invalid indexes. For example, with alignment 2 and trailing I struggle to think of cases in which people would want to use this API with such a type, as they'd have to either accept runtime validation or have some very odd requirements such that they know for certain that they'll only ever slice using valid slice lengths. |
Given a
&[T]
, Rust supports creating a new&[T]
referring to a subset of the original referent, and also supports splitting into two non-overlapping&[T]
s that, together, cover the original referent. To my knowledge, this is not supported for slice DSTs.We could add APIs for these slicing operations to
KnownLayout<PointerMetadata = usize>
.One use case for this design would be to support packet formats with explicit length fields - it would be a possibly lighter-weight way of supporting #1289 without requiring solving the general problem described in that issue.
The text was updated successfully, but these errors were encountered: