Skip to content
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

Implement AnyBitPattern for MaybeUninit<T> where T: AnyBitPattern. #160

Merged

Conversation

zachs18
Copy link
Contributor

@zachs18 zachs18 commented Jan 5, 2023

(Partially) resolves #108 .

A feature flag is required because MaybeUninit was unstable in bytemuck's MSRV. I just used the #[cfg(feature = "zeroable_maybe_uninit")] flag since that's required anyway, and it didn't seem reasonable to add another feature flag for this.


As discussed in #152 , it is probably unsound to have MaybeUninit<T> implement AnyBitPattern when T contains interior mutability, so a blanket impl<T: Copy + 'static> AnyBitPattern for MaybeUninit<T> would be unsound under that reasoning. However, if T implements AnyBitPattern, then it is known to not have any interior mutability, so T: AnyBitPattern is a sufficient (but not necessary) condition for MaybeUninit<T>: AnyBitPattern to be sound.

Since T: AnyBitPattern sufficient-but-not-necessary requirement, it could be relaxed later, e.g. if APIs around Freeze become available.

@Lokathor Lokathor merged commit 117222d into Lokathor:main Jan 11, 2023
@Lokathor Lokathor added the semver-patch semver patch change label Jan 20, 2023
@Lokathor
Copy link
Owner

Follow up question: Can't you cast from AnyBitPattern to AnyBitPattern? Like that's the idea right? Then, wouldn't this impl allow you to cast from MaybeUninit to something else? Because that could put uninit bytes into the something else where they're not supposed to be.

@Lokathor Lokathor added the bug label Jan 20, 2023
@zachs18
Copy link
Contributor Author

zachs18 commented Jan 20, 2023

No, I think you can only cast from NoUninit to AnyBitPattern. AnyBitPattern does not alone imply NoUninit.

AnyBitPattern
The requirements for this is very similar to Pod, except that the type can allow uninit (or padding) bytes.

(from docs)

@Lokathor
Copy link
Owner

#150 hm, but couldn't you use pod_read_unaligned?

@zachs18
Copy link
Contributor Author

zachs18 commented Jan 20, 2023

No, pod_read_unaligned (and the try_/checked:: versions) take in a &[u8], which cannot be uninitialized.

@Lokathor
Copy link
Owner

uhhh, yeah you're right, you're right. Sorry for the extra questions, MaybeUninit just makes me extra paranoid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-patch semver patch change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Could MaybeUninit<T: Copy> implement AnyBitPattern?
2 participants