-
-
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
Should pod_collect_to_vec()
input type require AnyBitPattern
?
#177
Comments
The reason for the pub fn pod_collect_to_vec<
A: NoUninit + AnyBitPattern,
B: NoUninit + AnyBitPattern,
>(
src: &[A],
) -> Vec<B> {
let src_size = size_of_val(src);
// Note(Lokathor): dst_count is rounded up so that the dest will always be at
// least as many bytes as the src.
let dst_count = src_size / size_of::<B>()
+ if src_size % size_of::<B>() != 0 { 1 } else { 0 };
let mut dst = vec![B::zeroed(); dst_count];
let src_bytes: &[u8] = cast_slice(src);
let dst_bytes: &mut [u8] = cast_slice_mut(&mut dst[..]);
dst_bytes[..src_size].copy_from_slice(src_bytes);
dst
} The actual copy from the If we really had to, we could use slice pointers instead of slice references, but then the function itself would have to do one or two unsafe calls. Even within So, it's not a copy-paste error, but it's also not an absolute requirement of performing this operation. It's possible, in an absolute sense, to relax the restriction, but without a strong and clear motivating case i wouldn't want to relax the restriction "just to do it". |
I should have been more clear and repeated the title once more: my main concern is with the input/source type. After all,
I don't want to suddenly derive
Still fine (and it makes sense) to keep EDIT: Here I wonder what this additional complexity is for, over just calling
Fwiw looks like everything compiles just fine without any changes (no |
Ahhhh, I understand the concern now (I think). Opened #178, which I think fixes what you're after. |
The way it's written casting both vecs to be bytes and then copying as bytes lets you go up in alignment (eg, u16 to u32) without fail. |
Yes, that's exactly it! Lots of unrelated changes though, but I found and approved what is relevant :)
Right of course, that is what the |
Clippy is always coming after me with new warnings :(
If you statically know that the alignment won't go up then yeah just using I seem to recall that this fn was long ago suggested by someone that wanted to go up in alignment (u8 to u32, or maybe it was f32 to f32x4?) |
Stricter clippy lints are nice at times (keeps getting better and more complete at pointing out code-smells in a codebase) but sometimes overly pedantic and unnecessarily blocking unrelated PRs. Fwiw Yeah we statically know alignment always goes to
That's what I understand from reading the original issue #49 (PR #50). At first I just thought this was a neat helper but now understand its applicability more clearly. I'll use it in places where alignment requirements go up (and where we need an allocation). |
Oh and @Lokathor, while I have you here there are a few questions that I don't feel like spawning new issues/discussions for (but can definitely do if desired!).
|
Disclaimer: I'm still fuzzy on the various traits and their requirements.
pod_collect_to_vec()
requiresNoUninit + AnyBitPattern
on both the source slice and outputVec
type. However, casts likecast_vec
andcast_slice
only requireNoUninit
on the source type (we can safely read all bytes, but don't need to zero-initialize the source type) and require the fullAnyBitPattern
exclusively on the output type (forpod_collect_to_vec()
: to first zero-initialize the outputVec
before copying into it?).bytemuck/src/allocation.rs
Lines 285 to 288 in 1039388
Is this constraint desired (and if so, can it be more clearly documented?) or accidental (and can it be relaxed)?
Thanks for this crate by the way! It's solving a bunch of problems for us that have always been looming around the corner 😬
The text was updated successfully, but these errors were encountered: