-
-
Notifications
You must be signed in to change notification settings - Fork 80
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
Introduce a function to align a slice to the right alignment #49
Comments
The capacity and alignment of the vec's allocation can't change during casting, it causes UB in the allocator. I'm not sure I'm fully clear on the requirements here. Can't you just cast the slice, then gather the slice into a new vec once you've cast it? let new_slice: &[u16] = cast_slice(old_slice);
let new_vec: Vec<_> = new_slice.iter().copied().collect(); |
What I would like to be able to do is to cast a slice of bytes into an aligned |
Ah, interesting. I follow now. I'm sure we could make something like that. The term "cast" doesn't quite fit, that's why i was confused, since you're doing a new allocation and a copy and all that, but you could turn a alice of pod into a vec of some other pod type and ignore the alignment of the source slice in the process. This seems like a small enough request to handle it the next time i sit down to do Rust. Might have time tonight even. |
Hello @Lokathor,
I wanted to switch from zerocopy to bytemuck for multiple reasons, and one of the missing features for both crates is the ability to realign a slice of type
A
into aVec
of typeB
, which is allowed to allocate. What do you think about introducing this to this crate (under theallocation
feature) and introducing that forBox
?This function would be preferable not to restrict the size (where the length of the input must be the same as the output
Vec
). It would make this function useless for when you want to transpose differently sized types (e.g.u8
intou16
) like for thetry_cast_vec
function.Another solution would be to make the
try_cast_vec
function change the length and capacity of theVec
. This seems a lot better but does seem to be a breaking change in the sense that more results could return anOk(Vec)
instead of an error and that theVec
is no more of the same length than the input one.Could maybe be implemented this way:
The text was updated successfully, but these errors were encountered: