Skip to content

Commit 7dedd50

Browse files
authored
[util] use more items to make code more concise (#2942)
gherrit-pr-id: Gf52a0089dd6f24906cd4488f99c158bacc746f06
1 parent 84eaf29 commit 7dedd50

File tree

5 files changed

+38
-38
lines changed

5 files changed

+38
-38
lines changed

src/util/macro_util.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ use core::{
3030
use crate::{
3131
pointer::{
3232
cast::{CastSized, IdCast},
33-
invariant::{self, BecauseExclusive, BecauseImmutable, Invariants},
34-
BecauseInvariantsEq, Read, TryTransmuteFromPtr,
33+
invariant::{
34+
Aligned, BecauseExclusive, BecauseImmutable, Initialized, Invariants, Read, Reference,
35+
Unaligned, Valid,
36+
},
37+
BecauseInvariantsEq, TryTransmuteFromPtr,
3538
},
3639
FromBytes, Immutable, IntoBytes, KnownLayout, Ptr, ReadOnly, TryFromBytes, ValidityError,
3740
};
@@ -626,20 +629,17 @@ pub const fn hash_name(name: &str) -> i128 {
626629
#[inline]
627630
fn try_cast_or_pme<Src, Dst, I, R, S>(
628631
src: Ptr<'_, Src, I>,
629-
) -> Result<
630-
Ptr<'_, Dst, (I::Aliasing, invariant::Unaligned, invariant::Valid)>,
631-
ValidityError<Ptr<'_, Src, I>, Dst>,
632-
>
632+
) -> Result<Ptr<'_, Dst, (I::Aliasing, Unaligned, Valid)>, ValidityError<Ptr<'_, Src, I>, Dst>>
633633
where
634634
// FIXME(#2226): There should be a `Src: FromBytes` bound here, but doing so
635635
// requires deeper surgery.
636-
Src: invariant::Read<I::Aliasing, R>,
636+
Src: Read<I::Aliasing, R>,
637637
Dst: TryFromBytes
638-
+ invariant::Read<I::Aliasing, R>
639-
+ TryTransmuteFromPtr<Dst, I::Aliasing, invariant::Initialized, invariant::Valid, IdCast, S>,
638+
+ Read<I::Aliasing, R>
639+
+ TryTransmuteFromPtr<Dst, I::Aliasing, Initialized, Valid, IdCast, S>,
640640
ReadOnly<Dst>: Read<I::Aliasing, R>,
641-
I: Invariants<Validity = invariant::Initialized>,
642-
I::Aliasing: invariant::Reference,
641+
I: Invariants<Validity = Initialized>,
642+
I::Aliasing: Reference,
643643
{
644644
let c_ptr = src.cast::<_, CastSized, _>();
645645
match c_ptr.try_into_valid() {
@@ -691,7 +691,7 @@ where
691691
// SAFETY: Since `Src: IntoBytes`, and since `size_of::<Src>() ==
692692
// size_of::<Dst>()` by the preceding assertion, all of `mu_dst`'s bytes are
693693
// initialized.
694-
let ptr = unsafe { ptr.assume_validity::<invariant::Initialized>() };
694+
let ptr = unsafe { ptr.assume_validity::<Initialized>() };
695695

696696
let ptr: Ptr<'_, ReadOnly<Dst>, _> = ptr.cast::<_, crate::pointer::cast::CastSized, _>();
697697

@@ -726,13 +726,13 @@ where
726726
Dst: TryFromBytes + Immutable,
727727
{
728728
let ptr = Ptr::from_ref(src);
729-
let ptr = ptr.recall_validity::<invariant::Initialized, _>();
729+
let ptr = ptr.recall_validity::<Initialized, _>();
730730
match try_cast_or_pme::<Src, Dst, _, BecauseImmutable, _>(ptr) {
731731
Ok(ptr) => {
732732
static_assert!(Src, Dst => mem::align_of::<Dst>() <= mem::align_of::<Src>());
733733
// SAFETY: We have checked that `Dst` does not have a stricter
734734
// alignment requirement than `Src`.
735-
let ptr = unsafe { ptr.assume_alignment::<invariant::Aligned>() };
735+
let ptr = unsafe { ptr.assume_alignment::<Aligned>() };
736736
Ok(ptr.as_ref())
737737
}
738738
Err(err) => Err(err.map_src(|ptr| {
@@ -770,13 +770,13 @@ where
770770
Dst: TryFromBytes + IntoBytes,
771771
{
772772
let ptr = Ptr::from_mut(src);
773-
let ptr = ptr.recall_validity::<invariant::Initialized, (_, (_, _))>();
773+
let ptr = ptr.recall_validity::<Initialized, (_, (_, _))>();
774774
match try_cast_or_pme::<Src, Dst, _, BecauseExclusive, _>(ptr) {
775775
Ok(ptr) => {
776776
static_assert!(Src, Dst => mem::align_of::<Dst>() <= mem::align_of::<Src>());
777777
// SAFETY: We have checked that `Dst` does not have a stricter
778778
// alignment requirement than `Src`.
779-
let ptr = unsafe { ptr.assume_alignment::<invariant::Aligned>() };
779+
let ptr = unsafe { ptr.assume_alignment::<Aligned>() };
780780
Ok(ptr.as_mut())
781781
}
782782
Err(err) => {
@@ -900,9 +900,9 @@ where
900900
}, "cannot transmute reference when destination type has higher alignment than source type");
901901

902902
let ptr = Ptr::from_ref(self.0)
903-
.recall_validity::<invariant::Initialized, _>()
904-
.transmute_with::<Dst, invariant::Initialized, crate::layout::CastFrom<Dst>, (crate::pointer::BecauseMutationCompatible, _)>()
905-
.recall_validity::<invariant::Valid, _>();
903+
.recall_validity::<Initialized, _>()
904+
.transmute_with::<Dst, Initialized, crate::layout::CastFrom<Dst>, (crate::pointer::BecauseMutationCompatible, _)>()
905+
.recall_validity::<Valid, _>();
906906

907907
// SAFETY: The preceding `static_assert!` ensures that
908908
// `Src::LAYOUT.align >= Dst::LAYOUT.align`. Since `self` is
@@ -933,9 +933,9 @@ where
933933
}, "cannot transmute reference when destination type has higher alignment than source type");
934934

935935
let ptr = Ptr::from_mut(self.0)
936-
.recall_validity::<invariant::Initialized, (_, (_, _))>()
937-
.transmute_with::<Dst, invariant::Initialized, crate::layout::CastFrom<Dst>, _>()
938-
.recall_validity::<invariant::Valid, (_, (_, _))>();
936+
.recall_validity::<Initialized, (_, (_, _))>()
937+
.transmute_with::<Dst, Initialized, crate::layout::CastFrom<Dst>, _>()
938+
.recall_validity::<Valid, (_, (_, _))>();
939939

940940
#[allow(unused_unsafe)]
941941
// SAFETY: The preceding `static_assert!` ensures that

zerocopy-derive/tests/ui-nightly/enum.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ help: the trait `PaddingFree<IntoBytes1, 1>` is not implemented for `()`
482482
but trait `PaddingFree<IntoBytes1, 0>` is implemented for it
483483
--> $WORKSPACE/src/util/macro_util.rs
484484
|
485-
65 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
485+
68 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
486486
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
487487
= help: see issue #48214
488488
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -504,7 +504,7 @@ help: the trait `PaddingFree<IntoBytes2, 3>` is not implemented for `()`
504504
but trait `PaddingFree<IntoBytes2, 0>` is implemented for it
505505
--> $WORKSPACE/src/util/macro_util.rs
506506
|
507-
65 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
507+
68 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
508508
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
509509
= help: see issue #48214
510510
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -526,7 +526,7 @@ help: the trait `PaddingFree<IntoBytes3, 2>` is not implemented for `()`
526526
but trait `PaddingFree<IntoBytes3, 0>` is implemented for it
527527
--> $WORKSPACE/src/util/macro_util.rs
528528
|
529-
65 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
529+
68 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
530530
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
531531
= help: see issue #48214
532532
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

zerocopy-derive/tests/ui-nightly/struct.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ help: the trait `PaddingFree<IntoBytes2, 1>` is not implemented for `()`
325325
but trait `PaddingFree<IntoBytes2, 0>` is implemented for it
326326
--> $WORKSPACE/src/util/macro_util.rs
327327
|
328-
65 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
328+
68 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
329329
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
330330
= help: see issue #48214
331331
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -347,7 +347,7 @@ help: the trait `PaddingFree<IntoBytes3, 1>` is not implemented for `()`
347347
but trait `PaddingFree<IntoBytes3, 0>` is implemented for it
348348
--> $WORKSPACE/src/util/macro_util.rs
349349
|
350-
65 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
350+
68 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
351351
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
352352
= help: see issue #48214
353353
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -405,7 +405,7 @@ help: the trait `DynamicPaddingFree<IntoBytes5, true>` is not implemented for `(
405405
but trait `DynamicPaddingFree<IntoBytes5, false>` is implemented for it
406406
--> $WORKSPACE/src/util/macro_util.rs
407407
|
408-
83 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
408+
86 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
409409
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
410410
= help: see issue #48214
411411
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -427,7 +427,7 @@ help: the trait `DynamicPaddingFree<IntoBytes6, true>` is not implemented for `(
427427
but trait `DynamicPaddingFree<IntoBytes6, false>` is implemented for it
428428
--> $WORKSPACE/src/util/macro_util.rs
429429
|
430-
83 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
430+
86 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
431431
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
432432
= help: see issue #48214
433433
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -449,7 +449,7 @@ help: the trait `DynamicPaddingFree<IntoBytes7, true>` is not implemented for `(
449449
but trait `DynamicPaddingFree<IntoBytes7, false>` is implemented for it
450450
--> $WORKSPACE/src/util/macro_util.rs
451451
|
452-
83 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
452+
86 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
453453
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
454454
= help: see issue #48214
455455
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

zerocopy-derive/tests/ui-stable/enum.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ help: the trait `PaddingFree<IntoBytes1, 1>` is not implemented for `()`
439439
but trait `PaddingFree<IntoBytes1, 0>` is implemented for it
440440
--> $WORKSPACE/src/util/macro_util.rs
441441
|
442-
65 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
442+
68 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
443443
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
444444
= help: see issue #48214
445445
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -457,7 +457,7 @@ help: the trait `PaddingFree<IntoBytes2, 3>` is not implemented for `()`
457457
but trait `PaddingFree<IntoBytes2, 0>` is implemented for it
458458
--> $WORKSPACE/src/util/macro_util.rs
459459
|
460-
65 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
460+
68 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
461461
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
462462
= help: see issue #48214
463463
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -475,7 +475,7 @@ help: the trait `PaddingFree<IntoBytes3, 2>` is not implemented for `()`
475475
but trait `PaddingFree<IntoBytes3, 0>` is implemented for it
476476
--> $WORKSPACE/src/util/macro_util.rs
477477
|
478-
65 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
478+
68 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
479479
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
480480
= help: see issue #48214
481481
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

zerocopy-derive/tests/ui-stable/struct.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ help: the trait `PaddingFree<IntoBytes2, 1>` is not implemented for `()`
293293
but trait `PaddingFree<IntoBytes2, 0>` is implemented for it
294294
--> $WORKSPACE/src/util/macro_util.rs
295295
|
296-
65 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
296+
68 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
297297
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
298298
= help: see issue #48214
299299
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -311,7 +311,7 @@ help: the trait `PaddingFree<IntoBytes3, 1>` is not implemented for `()`
311311
but trait `PaddingFree<IntoBytes3, 0>` is implemented for it
312312
--> $WORKSPACE/src/util/macro_util.rs
313313
|
314-
65 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
314+
68 | impl<T: ?Sized> PaddingFree<T, 0> for () {}
315315
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
316316
= help: see issue #48214
317317
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -365,7 +365,7 @@ help: the trait `DynamicPaddingFree<IntoBytes5, true>` is not implemented for `(
365365
but trait `DynamicPaddingFree<IntoBytes5, false>` is implemented for it
366366
--> $WORKSPACE/src/util/macro_util.rs
367367
|
368-
83 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
368+
86 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
369369
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
370370
= help: see issue #48214
371371
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -383,7 +383,7 @@ help: the trait `DynamicPaddingFree<IntoBytes6, true>` is not implemented for `(
383383
but trait `DynamicPaddingFree<IntoBytes6, false>` is implemented for it
384384
--> $WORKSPACE/src/util/macro_util.rs
385385
|
386-
83 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
386+
86 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
387387
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
388388
= help: see issue #48214
389389
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -401,7 +401,7 @@ help: the trait `DynamicPaddingFree<IntoBytes7, true>` is not implemented for `(
401401
but trait `DynamicPaddingFree<IntoBytes7, false>` is implemented for it
402402
--> $WORKSPACE/src/util/macro_util.rs
403403
|
404-
83 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
404+
86 | impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
405405
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
406406
= help: see issue #48214
407407
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)