-
Notifications
You must be signed in to change notification settings - Fork 104
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
Statically prevent ZST slices rather than panicking at runtime? #325
Comments
I'd quite like to see this code work: fn main() {
use zerocopy::*;
use zerocopy_derive::*;
#[derive(FromBytes, Immutable, KnownLayout)]
#[repr(C)]
struct Zesty {
leading_sized: u8,
trailing_dst: [()],
}
let bytes = [0u8; 100];
for count in 0..256 {
let (zesty, _) = Zesty::from_prefix_with_trailing_elements(bytes.as_slice(), count).unwrap();
assert_eq!(zesty.trailing_dst.len(), count);
}
} ...but I don't think that will be achievable until we have |
Presently, we deny ZSTy DSTs in our APIs via panicking at runtime. However, the ZSTiness of a DST is statically detectable and can be denied instead at compile time. This PR replaces our ZSTy DST panics with compile-time assertions. Doing gives us the freedom later provide meaningful runtime semantics in such cases. Partially addresses #325 Closes #1149
Presently, we deny ZSTy DSTs in our APIs via panicking at runtime. However, the ZSTiness of a DST is statically detectable and can be denied instead at compile time. This PR replaces our ZSTy DST panics with compile-time assertions. Doing gives us the freedom later provide meaningful runtime semantics in such cases. Partially addresses #325 Closes #1149
Presently, we deny ZSTy DSTs in our APIs via panicking at runtime. However, the ZSTiness of a DST is statically detectable and can be denied instead at compile time. This PR replaces our ZSTy DST panics with compile-time assertions. Doing gives us the freedom later provide meaningful runtime semantics in such cases. Partially addresses #325 Closes #1149
Presently, we deny ZSTy DSTs in our APIs via panicking at runtime. However, the ZSTiness of a DST is statically detectable and can be denied instead at compile time. This PR replaces our ZSTy DST panics with compile-time assertions. Doing gives us the freedom later provide meaningful runtime semantics in such cases. Partially addresses #325 Closes #1149
@jswrenn and I discussed this offline. Currently, supporting ZSTy types even when an explicit count is given is not possible. In particular, all of our APIs that take a slice element count bottom out in one of Eventually, if we can use |
Presently, we deny ZSTy DSTs in our APIs via panicking at runtime. However, the ZSTiness of a DST is statically detectable and can be denied instead at compile time. This PR replaces our ZSTy DST panics with compile-time assertions. Doing gives us the freedom later provide meaningful runtime semantics in such cases. Partially addresses #325 Closes #1149
@jswrenn Do we think that this should be closed given our PME strategy? |
Yes, we've thoroughly addressed this. |
zerocopy/src/lib.rs
Lines 1731 to 1735 in 07854bc
Alternatively, maybe we could just give these types well-defined semantics (e.g., always synthesize a pointer with 0 slice elements or
isize::MAX
slice elements). Need to be very careful that none of our operations can be round-tripped losslessly; this might break that.There are also reasons to be skeptical of wanting to prevent this at all:
As of this writing, per #1149, we intend to use post-monomorphization errors for ZST slice conversions. We can always make our restrictions looser in the future. If we do decide to loosen restrictions, note the following caveats:
See also #202, #284, #349 (comment), #1125, and #1149
The text was updated successfully, but these errors were encountered: