diff --git a/src/lib.rs b/src/lib.rs index cc96a4413f..b1df9a54a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3567,7 +3567,12 @@ pub unsafe trait FromBytes: FromZeros { match Ref::<_, Unalign>::sized_from(source) { Ok(r) => Ok(Ref::read(&r).into_inner()), Err(CastError::Size(e)) => Err(e.with_dst()), - Err(CastError::Alignment(_)) => unreachable!(), + Err(CastError::Alignment(_)) => { + // SAFETY: `Unalign` is trivially aligned, so + // `Ref::sized_from` cannot fail due to unmet alignment + // requirements. + unsafe { core::hint::unreachable_unchecked() } + } Err(CastError::Validity(i)) => match i {}, } } @@ -3613,7 +3618,12 @@ pub unsafe trait FromBytes: FromZeros { match Ref::<_, Unalign>::sized_from_prefix(source) { Ok((r, suffix)) => Ok((Ref::read(&r).into_inner(), suffix)), Err(CastError::Size(e)) => Err(e.with_dst()), - Err(CastError::Alignment(_)) => unreachable!(), + Err(CastError::Alignment(_)) => { + // SAFETY: `Unalign` is trivially aligned, so + // `Ref::sized_from_prefix` cannot fail due to unmet alignment + // requirements. + unsafe { core::hint::unreachable_unchecked() } + } Err(CastError::Validity(i)) => match i {}, } } @@ -3646,14 +3656,19 @@ pub unsafe trait FromBytes: FromZeros { /// ``` #[must_use = "has no side effects"] #[inline] - fn read_from_suffix(source: &[u8]) -> Result<(&[u8], Self), CastError<&[u8], Self>> + fn read_from_suffix(source: &[u8]) -> Result<(&[u8], Self), SizeError<&[u8], Self>> where Self: Sized, { match Ref::<_, Unalign>::sized_from_suffix(source) { Ok((prefix, r)) => Ok((prefix, Ref::read(&r).into_inner())), - Err(CastError::Size(e)) => Err(CastError::Size(e.with_dst())), - Err(CastError::Alignment(_)) => unreachable!(), + Err(CastError::Size(e)) => Err(e.with_dst()), + Err(CastError::Alignment(_)) => { + // SAFETY: `Unalign` is trivially aligned, so + // `Ref::sized_from_suffix` cannot fail due to unmet alignment + // requirements. + unsafe { core::hint::unreachable_unchecked() } + } Err(CastError::Validity(i)) => match i {}, } }