Lokathor / bytemuck

A crate for mucking around with piles of bytes
https://docs.rs/bytemuck
Apache License 2.0
697 stars 77 forks source link

Allow casting between slices of ZSTs and slices of non-ZSTs in all cases. #256

Closed zachs18 closed 1 month ago

zachs18 commented 1 month ago

Closes #253.

having ZST slices turn into 0-length non-ZST slices does seem like a compelling path.

This PR implements that. Additionally, casting non-ZST slices to ZST slices will work iff the input slice has length 0, and results in a slice length of 0; if the input slice is not of length 0, PodCastError::OutputSliceWouldHaveSlop is returned.

Updates the docs of the PodCastError variants to reflect when they can occur. Updates the docs of try_cast_slice (and checked::) to remove note about ZST <-> non-ZST not being allowed. Update bytes_of(_mut) to remove ZST-may-not-have-same-address exception, since casting [ZST] -> [u8] is now allowed directly using cast_slice(_mut) (but does not remove the documentation of the exception just in case). Update must_cast_slice checks and doctests to allow [ZST] -> [non-ZST], but disallow [non-ZST] -> [ZST].