Skip to content

Commit 155e82d

Browse files
committed
Simplify MutationCompatible impls
Previously, we allowed for `Src: Read<A, R>` and `Dst: Read<A, S>` for different `R` and `S` "reason" parameters. This is an unnecessary degree of freedom (two types are only ever `Read<A>` for a particular `A` for the same reason – either because `A = Exclusive` or because both types implement `Immutable`). This degree of freedom required more verbose type annotations by callers. gherrit-pr-id: Gbec926233303ce55c894f8ba65c939dd42690530
1 parent 13498ff commit 155e82d

7 files changed

Lines changed: 17 additions & 16 deletions

File tree

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4386,9 +4386,7 @@ pub unsafe trait FromBytes: FromZeros {
43864386
let source = Ptr::from_mut(source);
43874387
let maybe_slf = source.try_cast_into_no_leftover::<_, BecauseImmutable>(Some(count));
43884388
match maybe_slf {
4389-
Ok(slf) => Ok(slf
4390-
.recall_validity::<_, (_, (_, (BecauseExclusive, BecauseExclusive)))>()
4391-
.as_mut()),
4389+
Ok(slf) => Ok(slf.recall_validity::<_, (_, (_, BecauseExclusive))>().as_mut()),
43924390
Err(err) => Err(err.map_src(|s| s.as_mut())),
43934391
}
43944392
}

src/pointer/ptr.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,13 +966,17 @@ mod _casts {
966966
T: 'a + KnownLayout + ?Sized,
967967
I: Invariants<Validity = Initialized>,
968968
{
969+
// TODO: Is there any way to teach Rust that, for all `T, A, R`, `T:
970+
// Read<A, R>` implies `[u8]: Read<A, R>`?
971+
969972
/// Casts this pointer-to-initialized into a pointer-to-bytes.
970973
#[allow(clippy::wrong_self_convention)]
971974
#[must_use]
972975
#[inline]
973976
pub fn as_bytes<R>(self) -> Ptr<'a, [u8], (I::Aliasing, Aligned, Valid)>
974977
where
975978
T: Read<I::Aliasing, R>,
979+
[u8]: Read<I::Aliasing, R>,
976980
I::Aliasing: Reference,
977981
{
978982
let ptr = self.cast::<_, AsBytesCast, _>();
@@ -1125,6 +1129,7 @@ mod _casts {
11251129
where
11261130
I::Aliasing: Reference,
11271131
U: 'a + ?Sized + KnownLayout + Read<I::Aliasing, R>,
1132+
[u8]: Read<I::Aliasing, R>,
11281133
{
11291134
// FIXME(#67): Remove this allow. See NonNulSlicelExt for more
11301135
// details.

src/pointer/transmute.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ pub unsafe trait MutationCompatible<Src: ?Sized, A: Aliasing, SV, DV, R> {}
188188
pub enum BecauseRead {}
189189

190190
// SAFETY: `Src: Read<A, _>` and `Dst: Read<A, _>`.
191-
unsafe impl<Src: ?Sized, Dst: ?Sized, A: Aliasing, SV: Validity, DV: Validity, R, S>
192-
MutationCompatible<Src, A, SV, DV, (BecauseRead, (R, S))> for Dst
191+
unsafe impl<Src: ?Sized, Dst: ?Sized, A: Aliasing, SV: Validity, DV: Validity, R>
192+
MutationCompatible<Src, A, SV, DV, (BecauseRead, R)> for Dst
193193
where
194194
Src: Read<A, R>,
195-
Dst: Read<A, S>,
195+
Dst: Read<A, R>,
196196
{
197197
}
198198

src/ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ where
799799
let ptr = Ptr::from_mut(b.deref_mut())
800800
.try_cast_into_no_leftover::<T, BecauseExclusive>(None)
801801
.expect("zerocopy internal error: DerefMut::deref_mut should be infallible");
802-
let ptr = ptr.recall_validity::<_, (_, (_, (BecauseExclusive, BecauseExclusive)))>();
802+
let ptr = ptr.recall_validity::<_, (_, (_, BecauseExclusive))>();
803803
ptr.as_mut()
804804
}
805805
}

src/util/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ macro_rules! impl_for_transmute_from {
220220
#[inline]
221221
fn is_bit_valid<A: crate::pointer::invariant::Reference>(candidate: Maybe<'_, Self, A>) -> bool {
222222
let c: Maybe<'_, Self, crate::pointer::invariant::Exclusive> = candidate.into_exclusive_or_pme();
223-
let c: Maybe<'_, $repr, _> = c.transmute::<_, _, (_, (_, (BecauseExclusive, BecauseExclusive)))>();
223+
let c: Maybe<'_, $repr, _> = c.transmute::<_, _, (_, (_, BecauseExclusive))>();
224224
// SAFETY: This macro ensures that `$repr` and `Self` have the same
225225
// size and bit validity. Thus, a bit-valid instance of `$repr` is
226226
// also a bit-valid instance of `Self`.

zerocopy-derive/tests/struct_try_from_bytes.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn two_bad() {
7676

7777
let candidate = {
7878
use imp::pointer::{cast::CastSized, BecauseExclusive};
79-
candidate.cast::<_, CastSized, (_, (BecauseExclusive, BecauseExclusive))>()
79+
candidate.cast::<_, CastSized, (_, BecauseExclusive)>()
8080
};
8181

8282
// SAFETY: `candidate`'s referent is as-initialized as `Two`.
@@ -105,7 +105,7 @@ fn un_sized() {
105105

106106
let candidate = {
107107
use imp::pointer::{cast::CastUnsized, BecauseExclusive};
108-
candidate.cast::<_, CastUnsized, (_, (BecauseExclusive, BecauseExclusive))>()
108+
candidate.cast::<_, CastUnsized, (_, BecauseExclusive)>()
109109
};
110110

111111
// SAFETY: `candidate`'s referent is as-initialized as `Two`.
@@ -161,8 +161,7 @@ fn test_maybe_from_bytes() {
161161

162162
let candidate = {
163163
use imp::pointer::{cast::CastSized, BecauseExclusive};
164-
candidate
165-
.cast::<MaybeFromBytes<bool>, CastSized, (_, (BecauseExclusive, BecauseExclusive))>()
164+
candidate.cast::<MaybeFromBytes<bool>, CastSized, (_, BecauseExclusive)>()
166165
};
167166

168167
// SAFETY: `[u8]` consists entirely of initialized bytes.

zerocopy-derive/tests/union_try_from_bytes.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn two_bad() {
7171

7272
let candidate = {
7373
use imp::pointer::{cast::CastSized, BecauseExclusive};
74-
candidate.cast::<Two, CastSized, (_, (BecauseExclusive, BecauseExclusive))>()
74+
candidate.cast::<Two, CastSized, (_, BecauseExclusive)>()
7575
};
7676

7777
// SAFETY: `candidate`'s referent is as-initialized as `Two`.
@@ -99,7 +99,7 @@ fn bool_and_zst() {
9999

100100
let candidate = {
101101
use imp::pointer::{cast::CastSized, BecauseExclusive};
102-
candidate.cast::<BoolAndZst, CastSized, (_, (BecauseExclusive, BecauseExclusive))>()
102+
candidate.cast::<BoolAndZst, CastSized, (_, BecauseExclusive)>()
103103
};
104104

105105
// SAFETY: `candidate`'s referent is fully initialized.
@@ -127,8 +127,7 @@ fn test_maybe_from_bytes() {
127127

128128
let candidate = {
129129
use imp::pointer::{cast::CastSized, BecauseExclusive};
130-
candidate
131-
.cast::<MaybeFromBytes<bool>, CastSized, (_, (BecauseExclusive, BecauseExclusive))>()
130+
candidate.cast::<MaybeFromBytes<bool>, CastSized, (_, BecauseExclusive)>()
132131
};
133132

134133
// SAFETY: `[u8]` consists entirely of initialized bytes.

0 commit comments

Comments
 (0)