-
-
Notifications
You must be signed in to change notification settings - Fork 82
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
Zero-sized type (ZST) handling inconsistency between borrowed and owned slice casts. #253
Comments
Additionally, the error returned for an invalid let x: &[i32] = &[1, 2, 3];
let y: Result<&[[i32; 2]], PodCastError> = bytemuck::try_cast_slice(x);
assert_eq!(y.unwrap_err(), PodCastError::OutputSliceWouldHaveSlop);
let x: Box<[i32]> = Box::new([1, 2, 3]);
let y: Result<Box<[[i32; 2]]>, (PodCastError, Box<[i32]>)> = bytemuck::try_cast_slice_box(x);
assert_eq!(y.unwrap_err().0, PodCastError::SizeMismatch); (The documentation for |
Long ago when starting the crate, I had the idea that a That's neat and all, but consistency among all of this might be better, so just having ZST slices turn into 0-length non-ZST slices does seem like a compelling path.
I wanted to respond to this separately from the rest of the option selection: This particular break seems too small to worry about. I'd say that, in general, our policy as a crate should be that any casts which do work will continue to work in future versions, but anything which doesn't cast currently might be able to cast in some future version. Basically, it's non-breaking to enable more casts to be possible, when appropriate. |
(For the purposes of this issue, assume
Src
/Dst
/Zst
/NonZst
have the same alignment. Borrowed and owned casts intentionally have different alignment requirements, which are not the issue here.)bytemuck::try_cast_slice<Src, Dst>
1 is documented as returning an error (specificallyErr(SizeMismatch)
) whenSrc
andDst
are a ZST and a non-ZST (in either order).However,
bytemuck::allocation::try_cast_slice_box<Src, Dst>
2 says nothing in its documentation regarding ZSTs, currently always returnsOk(empty)
whenSrc
is a ZST andDst
is not, and currently panics with a modulo-by-zero error whenSrc
is a non-ZST andDst
is a ZST. (playground link)I see three possible overall resolutions to the inconsistency:
try_cast_slice_box
allows[Zst] -> [NonZst]
, returning an empty slice.try_cast_slice
disallows[Zst] -> [NonZst]
and[NonZst] -> [Zst]
, returningErr(SizeMismatch)
cast_slice
behave likecast_slice_box
(but fix the panic).cast_slice
andcast_slice_box
allow[Zst] -> [NonZst]
casts, returning an empty slice.cast_slice::<Zst, NonZst>
not succeeding.cast_slice_box
behave likecast_slice
.Err(SizeMismatch)
.cast_vec::<Zst, NonZst>
succeeding.and two possible resolutions for the
[NonZst] -> [Zst]
panic:[NonZst] -> [Zst]
, return an error3 on non-empty source slices.[NonZst]
to[Zst]
, always return an error3.Footnotes
Everything mentioned about
cast_slice
also applies tocast_slice_mut
) ↩Everything mentioned about
cast_slice_box
also applies tocast_vec
,cast_slice_arc
, andcast_slice_rc
. ↩On non-empty source slices, could return
Err(SizeMismatch)
, or could returnErr(OutputSliceWouldHaveSlop)
("If the output slice wouldn’t be a whole number of elements then the conversion fails.") since there is no whole number of ZSTs that would have any non-zero size, and on empty source slices since any whole number of ZSTs would have zero size. ↩ ↩2The text was updated successfully, but these errors were encountered: