-
Notifications
You must be signed in to change notification settings - Fork 24
Add optional zerocopy support #62
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,8 @@ | |
| //! | ||
| //! - [`serde`](https://serde.rs/) implements `Serialize` and `Deserialize` | ||
| //! for `BitFlags<T>`. | ||
| //! - [`zerocopy`](https://github.com/google/zerocopy/) implements `Immutable`, `IntoBytes`, | ||
| //! `FromZeros`, `TryFromBytes`, and `KnownLayout` for all `BitFlags<T>` and `Unaligned` if the value type is unaligned. | ||
| //! - `std` implements `std::error::Error` for `FromBitsError`. | ||
| //! | ||
| //! ## `const fn`-compatible APIs | ||
|
|
@@ -523,6 +525,14 @@ pub use crate::const_api::ConstToken; | |
| /// `BitFlags` value where that isn't the case is only possible with | ||
| /// incorrect unsafe code. | ||
| #[derive(Copy, Clone)] | ||
| #[cfg_attr( | ||
| feature = "zerocopy", | ||
| derive( | ||
| zerocopy_derive::Immutable, | ||
| zerocopy_derive::KnownLayout, | ||
| zerocopy_derive::IntoBytes, | ||
| ) | ||
| )] | ||
| #[repr(transparent)] | ||
| pub struct BitFlags<T, N = <T as _internal::RawBitFlags>::Numeric> { | ||
| val: N, | ||
|
|
@@ -1032,3 +1042,69 @@ mod impl_serde { | |
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "zerocopy")] | ||
| mod impl_zerocopy { | ||
| use super::{BitFlag, BitFlags}; | ||
| use zerocopy::{FromZeros, Immutable, TryFromBytes, Unaligned}; | ||
|
|
||
| // All zeros is always valid | ||
| unsafe impl<T> FromZeros for BitFlags<T> | ||
| where | ||
| T: BitFlag, | ||
| T::Numeric: Immutable, | ||
| T::Numeric: FromZeros, | ||
| { | ||
| fn only_derive_is_allowed_to_implement_this_trait() {} | ||
| } | ||
|
|
||
| // Mark all BitFlags as Unaligned if the underlying number type is unaligned | ||
| unsafe impl<T> Unaligned for BitFlags<T> | ||
| where | ||
| T: BitFlag, | ||
| T::Numeric: Unaligned, | ||
| { | ||
| fn only_derive_is_allowed_to_implement_this_trait() {} | ||
| } | ||
|
|
||
| // Assert that there are no invalid bytes set | ||
| unsafe impl<T> TryFromBytes for BitFlags<T> | ||
| where | ||
| T: BitFlag, | ||
| T::Numeric: Immutable, | ||
| T::Numeric: TryFromBytes, | ||
| { | ||
| fn only_derive_is_allowed_to_implement_this_trait() | ||
| where | ||
| Self: Sized, | ||
| { | ||
| } | ||
|
|
||
| #[inline] | ||
| fn is_bit_valid< | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this impl relies on semver-unstable |
||
| ZerocopyAliasing: zerocopy::pointer::invariant::Aliasing | ||
| + zerocopy::pointer::invariant::AtLeast<zerocopy::pointer::invariant::Shared>, | ||
| >( | ||
| candidate: zerocopy::Maybe<'_, Self, ZerocopyAliasing>, | ||
| ) -> bool { | ||
| // SAFETY: | ||
| // - The cast preserves address. The caller has promised that the | ||
| // cast results in an object of equal or lesser size, and so the | ||
| // cast returns a pointer which references a subset of the bytes | ||
| // of `p`. | ||
| // - The cast preserves provenance. | ||
| // - The caller has promised that the destination type has | ||
| // `UnsafeCell`s at the same byte ranges as the source type. | ||
| let candidate = unsafe { candidate.cast_unsized::<T::Numeric, _>(|p| p as *mut _) }; | ||
|
|
||
| // SAFETY: The caller has promised that the referenced memory region | ||
| // will contain a valid `$repr`. | ||
| let my_candidate = | ||
| unsafe { candidate.assume_validity::<zerocopy::pointer::invariant::Valid>() }; | ||
| { | ||
| (my_candidate.read_unaligned::<zerocopy::pointer::BecauseImmutable>() ^ T::ALL_BITS) | ||
| == T::EMPTY | ||
| } | ||
|
Comment on lines
+1104
to
+1149
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bit manipulation looks suspicious. Please add tests that exercise this. Or, maybe something like |
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| use enumflags2::{bitflags, BitFlags}; | ||
| use zerocopy::{Immutable, IntoBytes, KnownLayout, TryFromBytes}; | ||
|
|
||
| #[test] | ||
| fn zerocopy_compile() { | ||
| #[bitflags] | ||
| #[derive(Copy, Clone, Debug, KnownLayout)] | ||
| #[repr(u8)] | ||
| enum TestU8 { | ||
| A, | ||
| B, | ||
| C, | ||
| D, | ||
| } | ||
|
|
||
| #[bitflags] | ||
| #[derive(Copy, Clone, Debug, KnownLayout)] | ||
| #[repr(u16)] | ||
| enum TestU16 { | ||
| A, | ||
| B, | ||
| C, | ||
| D, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Immutable, TryFromBytes, IntoBytes, KnownLayout)] | ||
| #[repr(packed)] | ||
| struct Other { | ||
| flags2: BitFlags<TestU8>, | ||
| flags: BitFlags<TestU16>, | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could use a comment linking to google/zerocopy#287, so that this doesn't scream "crimes! damn crimes!" at anyone who looks at this later.