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

Feature request: Casting owned slices or vec, I.e. Box<[u64]> to Box<[u8]> or Vec<u64> to Vec<u8> #227

Closed forrestli74 closed 6 months ago

zachs18 commented 6 months ago

You can't go directly from Box<[u64]> to Box<[u8]> due to allocation alognment requirements, but you can use cast_slice and then slice::to_vec to get an owned vec in a new allocation (and then Vec::into_boxed_slice if needed)

forrestli74 commented 6 months ago

I was really looking for a solution with no copy or new allocation. I didn't know this was impossible. Any suggestions for future reading? Thanks

zachs18 commented 6 months ago

bytemuck::allocation::BoxBytes might fit your use-case, however it currently doesn't support casting to/from boxed slices, only boxed Sized types.

forrestli74 commented 6 months ago

Thanks!