-
Notifications
You must be signed in to change notification settings - Fork 138
[WIP] #2864
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: Gb15e30e716a041285c1bf7434ce3163f594526a7
Are you sure you want to change the base?
[WIP] #2864
Changes from all commits
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 |
|---|---|---|
|
|
@@ -135,15 +135,15 @@ macro_rules! unsafe_impl { | |
| fn only_derive_is_allowed_to_implement_this_trait() {} | ||
|
|
||
| #[inline] | ||
| fn is_bit_valid<AA: crate::pointer::invariant::Reference>($candidate: Maybe<'_, Self, AA>) -> bool { | ||
| fn is_bit_valid($candidate: Maybe<'_, Self>) -> bool { | ||
| $is_bit_valid | ||
| } | ||
| }; | ||
| (@method TryFromBytes) => { | ||
| #[allow(clippy::missing_inline_in_public_items)] | ||
| #[cfg_attr(all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS), coverage(off))] | ||
| fn only_derive_is_allowed_to_implement_this_trait() {} | ||
| #[inline(always)] fn is_bit_valid<AA: crate::pointer::invariant::Reference>(_: Maybe<'_, Self, AA>) -> bool { true } | ||
| #[inline(always)] fn is_bit_valid(_: Maybe<'_, Self>) -> bool { true } | ||
| }; | ||
| (@method $trait:ident) => { | ||
| #[allow(clippy::missing_inline_in_public_items, dead_code)] | ||
|
|
@@ -218,12 +218,15 @@ macro_rules! impl_for_transmute_from { | |
| TryFromBytes for $ty:ty [UnsafeCell<$repr:ty>] | ||
| ) => { | ||
| #[inline] | ||
| fn is_bit_valid<A: crate::pointer::invariant::Reference>(candidate: Maybe<'_, Self, A>) -> bool { | ||
| let c: Maybe<'_, Self, crate::pointer::invariant::Exclusive> = candidate.into_exclusive_or_pme(); | ||
| let c: Maybe<'_, $repr, _> = c.transmute::<_, _, (_, (_, (BecauseExclusive, BecauseExclusive)))>(); | ||
| // SAFETY: This macro ensures that `$repr` and `Self` have the same | ||
| // size and bit validity. Thus, a bit-valid instance of `$repr` is | ||
| // also a bit-valid instance of `Self`. | ||
| #[inline] | ||
|
Contributor
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. |
||
| fn is_bit_valid(candidate: Maybe<'_, Self>) -> bool { | ||
| let c = unsafe { | ||
| candidate.transmute_unchecked::< | ||
| crate::wrappers::ReadOnly<$repr>, | ||
| _, | ||
| crate::pointer::cast::CastUnsized | ||
| >() | ||
| }; | ||
| <$repr as TryFromBytes>::is_bit_valid(c) | ||
| } | ||
| }; | ||
|
|
@@ -233,11 +236,14 @@ macro_rules! impl_for_transmute_from { | |
| TryFromBytes for $ty:ty [<$repr:ty>] | ||
| ) => { | ||
| #[inline] | ||
| fn is_bit_valid<A: crate::pointer::invariant::Reference>(candidate: $crate::Maybe<'_, Self, A>) -> bool { | ||
| // SAFETY: This macro ensures that `$repr` and `Self` have the same | ||
| // size and bit validity. Thus, a bit-valid instance of `$repr` is | ||
| // also a bit-valid instance of `Self`. | ||
| <$repr as TryFromBytes>::is_bit_valid(candidate.transmute()) | ||
| fn is_bit_valid(candidate: $crate::Maybe<'_, Self>) -> bool { | ||
| <$repr as TryFromBytes>::is_bit_valid(unsafe { | ||
| candidate.transmute_unchecked::< | ||
| crate::wrappers::ReadOnly<$repr>, | ||
| _, | ||
| crate::pointer::cast::CastUnsized | ||
| >() | ||
| }) | ||
| } | ||
| }; | ||
| ( | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -879,21 +879,36 @@ fn derive_try_from_bytes_struct( | |||||||||||||||||||||
| // validity of a struct is just the composition of the bit | ||||||||||||||||||||||
| // validities of its fields, so this is a sound implementation | ||||||||||||||||||||||
| // of `is_bit_valid`. | ||||||||||||||||||||||
| fn is_bit_valid<___ZerocopyAliasing>( | ||||||||||||||||||||||
| mut candidate: #zerocopy_crate::Maybe<Self, ___ZerocopyAliasing>, | ||||||||||||||||||||||
| ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool | ||||||||||||||||||||||
| where | ||||||||||||||||||||||
| ___ZerocopyAliasing: #zerocopy_crate::pointer::invariant::Reference, | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| fn is_bit_valid( | ||||||||||||||||||||||
| mut candidate: #zerocopy_crate::Maybe<Self>, | ||||||||||||||||||||||
| ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool { | ||||||||||||||||||||||
| use #zerocopy_crate::util::macro_util::core_reexport; | ||||||||||||||||||||||
| use #zerocopy_crate::pointer::PtrInner; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // SAFETY: `ReadOnly<Self>` has the same memory layout as `Self`. | ||||||||||||||||||||||
| let mut candidate = unsafe { | ||||||||||||||||||||||
| candidate.transmute_unchecked::< | ||||||||||||||||||||||
| Self, | ||||||||||||||||||||||
| _, | ||||||||||||||||||||||
| #zerocopy_crate::pointer::cast::CastUnsized | ||||||||||||||||||||||
| >() | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| true #(&& { | ||||||||||||||||||||||
| let field_candidate = candidate.reborrow().project::< | ||||||||||||||||||||||
| _, | ||||||||||||||||||||||
| { #zerocopy_crate::ident_id!(#field_names) } | ||||||||||||||||||||||
| >(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // SAFETY: `ReadOnly<T>` has the same memory layout as `T`. | ||||||||||||||||||||||
| let field_candidate = unsafe { | ||||||||||||||||||||||
| field_candidate.transmute_unchecked::< | ||||||||||||||||||||||
| #zerocopy_crate::wrappers::ReadOnly<#field_tys>, | ||||||||||||||||||||||
| _, | ||||||||||||||||||||||
| #zerocopy_crate::pointer::cast::CastUnsized | ||||||||||||||||||||||
| >() | ||||||||||||||||||||||
|
Comment on lines
+905
to
+909
Contributor
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. There's an extra space at the beginning of this line, causing inconsistent indentation within the
Suggested change
|
||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| <#field_tys as #zerocopy_crate::TryFromBytes>::is_bit_valid(field_candidate) | ||||||||||||||||||||||
| })* | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -933,15 +948,21 @@ fn derive_try_from_bytes_union( | |||||||||||||||||||||
| // The bit validity of a union is not yet well defined in Rust, | ||||||||||||||||||||||
| // but it is guaranteed to be no more strict than this | ||||||||||||||||||||||
| // definition. See #696 for a more in-depth discussion. | ||||||||||||||||||||||
| fn is_bit_valid<___ZerocopyAliasing>( | ||||||||||||||||||||||
| mut candidate: #zerocopy_crate::Maybe<'_, Self,___ZerocopyAliasing> | ||||||||||||||||||||||
| ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool | ||||||||||||||||||||||
| where | ||||||||||||||||||||||
| ___ZerocopyAliasing: #zerocopy_crate::pointer::invariant::Reference, | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| fn is_bit_valid( | ||||||||||||||||||||||
| mut candidate: #zerocopy_crate::Maybe<'_, Self> | ||||||||||||||||||||||
| ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool { | ||||||||||||||||||||||
| use #zerocopy_crate::util::macro_util::core_reexport; | ||||||||||||||||||||||
| use #zerocopy_crate::pointer::PtrInner; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // SAFETY: `ReadOnly<Self>` has the same memory layout as `Self`. | ||||||||||||||||||||||
| let mut candidate = unsafe { | ||||||||||||||||||||||
| candidate.transmute_unchecked::< | ||||||||||||||||||||||
| Self, | ||||||||||||||||||||||
| _, | ||||||||||||||||||||||
| #zerocopy_crate::pointer::cast::CastUnsized | ||||||||||||||||||||||
| >() | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| false #(|| { | ||||||||||||||||||||||
| // SAFETY: | ||||||||||||||||||||||
| // - Since `Self: Immutable` is enforced by | ||||||||||||||||||||||
|
|
@@ -959,6 +980,15 @@ fn derive_try_from_bytes_union( | |||||||||||||||||||||
| >() | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // SAFETY: `ReadOnly<T>` has the same memory layout as `T`. | ||||||||||||||||||||||
| let field_candidate = unsafe { | ||||||||||||||||||||||
| field_candidate.transmute_unchecked::< | ||||||||||||||||||||||
| #zerocopy_crate::wrappers::ReadOnly<#field_tys>, | ||||||||||||||||||||||
| _, | ||||||||||||||||||||||
| #zerocopy_crate::pointer::cast::CastUnsized | ||||||||||||||||||||||
| >() | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| <#field_tys as #zerocopy_crate::TryFromBytes>::is_bit_valid(field_candidate) | ||||||||||||||||||||||
| })* | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -1040,12 +1070,9 @@ fn try_gen_trivial_is_bit_valid( | |||||||||||||||||||||
| if matches!(top_level, Trait::FromBytes) && ast.generics.params.is_empty() { | ||||||||||||||||||||||
| Some(quote!( | ||||||||||||||||||||||
| // SAFETY: See inline. | ||||||||||||||||||||||
| fn is_bit_valid<___ZerocopyAliasing>( | ||||||||||||||||||||||
| _candidate: #zerocopy_crate::Maybe<Self, ___ZerocopyAliasing>, | ||||||||||||||||||||||
| ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool | ||||||||||||||||||||||
| where | ||||||||||||||||||||||
| ___ZerocopyAliasing: #zerocopy_crate::pointer::invariant::Reference, | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| fn is_bit_valid( | ||||||||||||||||||||||
| _candidate: #zerocopy_crate::Maybe<Self>, | ||||||||||||||||||||||
| ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool { | ||||||||||||||||||||||
| if false { | ||||||||||||||||||||||
| fn assert_is_from_bytes<T>() | ||||||||||||||||||||||
| where | ||||||||||||||||||||||
|
|
@@ -1083,12 +1110,9 @@ unsafe fn gen_trivial_is_bit_valid_unchecked(zerocopy_crate: &Path) -> proc_macr | |||||||||||||||||||||
| quote!( | ||||||||||||||||||||||
| // SAFETY: The caller of `gen_trivial_is_bit_valid_unchecked` has | ||||||||||||||||||||||
| // promised that all initialized bit patterns are valid for `Self`. | ||||||||||||||||||||||
| fn is_bit_valid<___ZerocopyAliasing>( | ||||||||||||||||||||||
| _candidate: #zerocopy_crate::Maybe<Self, ___ZerocopyAliasing>, | ||||||||||||||||||||||
| ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool | ||||||||||||||||||||||
| where | ||||||||||||||||||||||
| ___ZerocopyAliasing: #zerocopy_crate::pointer::invariant::Reference, | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| fn is_bit_valid( | ||||||||||||||||||||||
| _candidate: #zerocopy_crate::Maybe<Self>, | ||||||||||||||||||||||
| ) -> #zerocopy_crate::util::macro_util::core_reexport::primitive::bool { | ||||||||||||||||||||||
| true | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
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.
The
#[inline]attribute is duplicated. Please remove one of them.