Skip to content

Commit 7af6a22

Browse files
djkoloskijoshlf
authored andcommitted
Rename generic param to avoid name collision
Also makes the signatures of is_bit_valid generated for structs, unions, and enums the same.
1 parent 38e64d2 commit 7af6a22

File tree

6 files changed

+70
-33
lines changed

6 files changed

+70
-33
lines changed

zerocopy-derive/src/enum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,11 @@ pub(crate) fn derive_is_bit_valid(
294294
// enum's tag corresponds to one of the enum's discriminants. Then, we
295295
// check the bit validity of each field of the corresponding variant.
296296
// Thus, this is a sound implementation of `is_bit_valid`.
297-
fn is_bit_valid<A>(
298-
mut candidate: ::zerocopy::Maybe<'_, Self, A>,
297+
fn is_bit_valid<___ZerocopyAliasing>(
298+
mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
299299
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
300300
where
301-
A: ::zerocopy::pointer::invariant::Aliasing
301+
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
302302
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
303303
{
304304
use ::zerocopy::util::macro_util::core_reexport;

zerocopy-derive/src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,13 @@ fn derive_try_from_bytes_struct(ast: &DeriveInput, strct: &DataStruct) -> proc_m
420420
// validity of a struct is just the composition of the bit
421421
// validities of its fields, so this is a sound implementation of
422422
// `is_bit_valid`.
423-
fn is_bit_valid<A: ::zerocopy::pointer::invariant::Aliasing + ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>>(
424-
mut candidate: ::zerocopy::Maybe<Self, A>,
425-
) -> bool {
423+
fn is_bit_valid<___ZerocopyAliasing>(
424+
mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
425+
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
426+
where
427+
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
428+
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
429+
{
426430
true #(&& {
427431
// SAFETY:
428432
// - `project` is a field projection, and so it addresses a
@@ -470,9 +474,13 @@ fn derive_try_from_bytes_union(ast: &DeriveInput, unn: &DataUnion) -> proc_macro
470474
// bit validity of a union is not yet well defined in Rust, but it
471475
// is guaranteed to be no more strict than this definition. See #696
472476
// for a more in-depth discussion.
473-
fn is_bit_valid<A: ::zerocopy::pointer::invariant::Aliasing + ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>>(
474-
mut candidate: ::zerocopy::Maybe<Self, A>
475-
) -> bool {
477+
fn is_bit_valid<___ZerocopyAliasing>(
478+
mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>
479+
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
480+
where
481+
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
482+
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
483+
{
476484
false #(|| {
477485
// SAFETY:
478486
// - `project` is a field projection, and so it addresses a

zerocopy-derive/src/output_tests.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,13 @@ fn test_try_from_bytes() {
147147
unsafe impl ::zerocopy::TryFromBytes for Foo {
148148
fn only_derive_is_allowed_to_implement_this_trait() {}
149149

150-
fn is_bit_valid<
151-
A: ::zerocopy::pointer::invariant::Aliasing
150+
fn is_bit_valid<___ZerocopyAliasing>(
151+
mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
152+
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
153+
where
154+
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
152155
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
153-
>(
154-
mut candidate: ::zerocopy::Maybe<Self, A>,
155-
) -> bool {
156+
{
156157
true
157158
}
158159
}
@@ -170,12 +171,13 @@ fn test_from_zeros() {
170171
unsafe impl ::zerocopy::TryFromBytes for Foo {
171172
fn only_derive_is_allowed_to_implement_this_trait() {}
172173

173-
fn is_bit_valid<
174-
A: ::zerocopy::pointer::invariant::Aliasing
174+
fn is_bit_valid<___ZerocopyAliasing>(
175+
mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
176+
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
177+
where
178+
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
175179
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
176-
>(
177-
mut candidate: ::zerocopy::Maybe<Self, A>,
178-
) -> bool {
180+
{
179181
true
180182
}
181183
}
@@ -198,12 +200,13 @@ fn test_from_bytes() {
198200
unsafe impl ::zerocopy::TryFromBytes for Foo {
199201
fn only_derive_is_allowed_to_implement_this_trait() {}
200202

201-
fn is_bit_valid<
202-
A: ::zerocopy::pointer::invariant::Aliasing
203+
fn is_bit_valid<___ZerocopyAliasing>(
204+
mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
205+
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
206+
where
207+
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
203208
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
204-
>(
205-
mut candidate: ::zerocopy::Maybe<Self, A>,
206-
) -> bool {
209+
{
207210
true
208211
}
209212
}
@@ -303,11 +306,11 @@ fn test_try_from_bytes_enum() {
303306
PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
304307
{
305308
fn only_derive_is_allowed_to_implement_this_trait() {}
306-
fn is_bit_valid<A>(
307-
mut candidate: ::zerocopy::Maybe<'_, Self, A>,
309+
fn is_bit_valid<___ZerocopyAliasing>(
310+
mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
308311
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
309312
where
310-
A: ::zerocopy::pointer::invariant::Aliasing
313+
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
311314
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
312315
{
313316
use ::zerocopy::util::macro_util::core_reexport;
@@ -445,11 +448,11 @@ fn test_try_from_bytes_enum() {
445448
PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
446449
{
447450
fn only_derive_is_allowed_to_implement_this_trait() {}
448-
fn is_bit_valid<A>(
449-
mut candidate: ::zerocopy::Maybe<'_, Self, A>,
451+
fn is_bit_valid<___ZerocopyAliasing>(
452+
mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
450453
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
451454
where
452-
A: ::zerocopy::pointer::invariant::Aliasing
455+
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
453456
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
454457
{
455458
use ::zerocopy::util::macro_util::core_reexport;
@@ -587,11 +590,11 @@ fn test_try_from_bytes_enum() {
587590
PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
588591
{
589592
fn only_derive_is_allowed_to_implement_this_trait() {}
590-
fn is_bit_valid<A>(
591-
mut candidate: ::zerocopy::Maybe<'_, Self, A>,
593+
fn is_bit_valid<___ZerocopyAliasing>(
594+
mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
592595
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
593596
where
594-
A: ::zerocopy::pointer::invariant::Aliasing
597+
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
595598
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
596599
{
597600
use ::zerocopy::util::macro_util::core_reexport;

zerocopy-derive/tests/enum_try_from_bytes.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,13 @@ enum GenericWithLifetimes<'a, 'b, X: 'a, Y: 'b> {
268268
Foo(::core::marker::PhantomData<&'a X>),
269269
Bar(::core::marker::PhantomData<&'b Y>),
270270
}
271+
272+
#[derive(Clone, Copy, imp::TryFromBytes)]
273+
struct A;
274+
275+
#[derive(imp::TryFromBytes)]
276+
#[repr(C)]
277+
enum B {
278+
A(A),
279+
A2 { a: A },
280+
}

zerocopy-derive/tests/struct_try_from_bytes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,11 @@ fn packed_unsized() {
216216
let converted = <CPackedUnsized as imp::TryFromBytes>::try_ref_from_bytes(candidate);
217217
imp::assert!(converted.is_ok());
218218
}
219+
220+
#[derive(imp::TryFromBytes)]
221+
struct A;
222+
223+
#[derive(imp::TryFromBytes)]
224+
struct B {
225+
a: A,
226+
}

zerocopy-derive/tests/union_try_from_bytes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,11 @@ where
144144
}
145145

146146
util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::TryFromBytes);
147+
148+
#[derive(Clone, Copy, imp::TryFromBytes, imp::Immutable)]
149+
struct A;
150+
151+
#[derive(imp::TryFromBytes)]
152+
union B {
153+
a: A,
154+
}

0 commit comments

Comments
 (0)