Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4390,9 +4390,7 @@ pub unsafe trait FromBytes: FromZeros {
let source = Ptr::from_mut(source);
let maybe_slf = source.try_cast_into_no_leftover::<_, BecauseImmutable>(Some(count));
match maybe_slf {
Ok(slf) => Ok(slf
.recall_validity::<_, (_, (_, (BecauseExclusive, BecauseExclusive)))>()
.as_mut()),
Ok(slf) => Ok(slf.recall_validity::<_, (_, (_, BecauseExclusive))>().as_mut()),
Err(err) => Err(err.map_src(|s| s.as_mut())),
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/pointer/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,13 +966,17 @@ mod _casts {
T: 'a + KnownLayout + ?Sized,
I: Invariants<Validity = Initialized>,
{
// FIXME: Is there any way to teach Rust that, for all `T, A, R`, `T:
// Read<A, R>` implies `[u8]: Read<A, R>`?

/// Casts this pointer-to-initialized into a pointer-to-bytes.
#[allow(clippy::wrong_self_convention)]
#[must_use]
#[inline]
pub fn as_bytes<R>(self) -> Ptr<'a, [u8], (I::Aliasing, Aligned, Valid)>
where
T: Read<I::Aliasing, R>,
[u8]: Read<I::Aliasing, R>,
I::Aliasing: Reference,
{
let ptr = self.cast::<_, AsBytesCast, _>();
Expand Down Expand Up @@ -1125,6 +1129,7 @@ mod _casts {
where
I::Aliasing: Reference,
U: 'a + ?Sized + KnownLayout + Read<I::Aliasing, R>,
[u8]: Read<I::Aliasing, R>,
{
// FIXME(#67): Remove this allow. See NonNulSlicelExt for more
// details.
Expand Down
6 changes: 3 additions & 3 deletions src/pointer/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ pub unsafe trait MutationCompatible<Src: ?Sized, A: Aliasing, SV, DV, R> {}
pub enum BecauseRead {}

// SAFETY: `Src: Read<A, _>` and `Dst: Read<A, _>`.
unsafe impl<Src: ?Sized, Dst: ?Sized, A: Aliasing, SV: Validity, DV: Validity, R, S>
MutationCompatible<Src, A, SV, DV, (BecauseRead, (R, S))> for Dst
unsafe impl<Src: ?Sized, Dst: ?Sized, A: Aliasing, SV: Validity, DV: Validity, R>
MutationCompatible<Src, A, SV, DV, (BecauseRead, R)> for Dst
where
Src: Read<A, R>,
Dst: Read<A, S>,
Dst: Read<A, R>,
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ where
cast_for_sized::<
T,
_,
(BecauseRead, (BecauseExclusive, BecauseExclusive)),
(BecauseRead, BecauseExclusive),
(BecauseMutationCompatible, BecauseInvariantsEq),
>(ptr)
};
Expand Down Expand Up @@ -857,7 +857,7 @@ where
cast_for_sized::<
T,
_,
(BecauseRead, (BecauseExclusive, BecauseExclusive)),
(BecauseRead, BecauseExclusive),
(BecauseMutationCompatible, BecauseInvariantsEq),
>(ptr)
};
Expand All @@ -876,7 +876,7 @@ where
let ptr = Ptr::from_mut(b)
.try_cast_into_no_leftover::<T, BecauseExclusive>(None)
.expect("zerocopy internal error: DerefMut::deref_mut should be infallible");
let ptr = ptr.recall_validity::<_, (_, (_, (BecauseExclusive, BecauseExclusive)))>();
let ptr = ptr.recall_validity::<_, (_, (_, BecauseExclusive))>();
ptr.as_mut()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ macro_rules! impl_for_transmute_from {
#[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)))>();
let c: Maybe<'_, $repr, _> = c.transmute::<_, _, (_, (_, 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`.
Expand Down
7 changes: 3 additions & 4 deletions zerocopy-derive/tests/struct_try_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn two_bad() {

let candidate = {
use imp::pointer::{cast::CastSized, BecauseExclusive};
candidate.cast::<_, CastSized, (_, (BecauseExclusive, BecauseExclusive))>()
candidate.cast::<_, CastSized, (_, BecauseExclusive)>()
};

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

let candidate = {
use imp::pointer::{cast::CastUnsized, BecauseExclusive};
candidate.cast::<_, CastUnsized, (_, (BecauseExclusive, BecauseExclusive))>()
candidate.cast::<_, CastUnsized, (_, BecauseExclusive)>()
};

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

let candidate = {
use imp::pointer::{cast::CastSized, BecauseExclusive};
candidate
.cast::<MaybeFromBytes<bool>, CastSized, (_, (BecauseExclusive, BecauseExclusive))>()
candidate.cast::<MaybeFromBytes<bool>, CastSized, (_, BecauseExclusive)>()
};

// SAFETY: `[u8]` consists entirely of initialized bytes.
Expand Down
7 changes: 3 additions & 4 deletions zerocopy-derive/tests/union_try_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn two_bad() {

let candidate = {
use imp::pointer::{cast::CastSized, BecauseExclusive};
candidate.cast::<Two, CastSized, (_, (BecauseExclusive, BecauseExclusive))>()
candidate.cast::<Two, CastSized, (_, BecauseExclusive)>()
};

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

let candidate = {
use imp::pointer::{cast::CastSized, BecauseExclusive};
candidate.cast::<BoolAndZst, CastSized, (_, (BecauseExclusive, BecauseExclusive))>()
candidate.cast::<BoolAndZst, CastSized, (_, BecauseExclusive)>()
};

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

let candidate = {
use imp::pointer::{cast::CastSized, BecauseExclusive};
candidate
.cast::<MaybeFromBytes<bool>, CastSized, (_, (BecauseExclusive, BecauseExclusive))>()
candidate.cast::<MaybeFromBytes<bool>, CastSized, (_, BecauseExclusive)>()
};

// SAFETY: `[u8]` consists entirely of initialized bytes.
Expand Down
Loading