Skip to content

Commit

Permalink
Fix FromBytes::read_from_suffix return type; remove panic paths (#1716
Browse files Browse the repository at this point in the history
)

`FromBytes::read_from_suffix` should return a `SizeError`, like
`read_from_bytes` and `read_from_prefix`. This commit corrects this
error, and also removes unreachable panic paths in all three
functions.

Makes progress towards #1692
  • Loading branch information
jswrenn authored Sep 21, 2024
1 parent 553996e commit c57e3b9
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3567,7 +3567,12 @@ pub unsafe trait FromBytes: FromZeros {
match Ref::<_, Unalign<Self>>::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<Self>` 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 {},
}
}
Expand Down Expand Up @@ -3613,7 +3618,12 @@ pub unsafe trait FromBytes: FromZeros {
match Ref::<_, Unalign<Self>>::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<Self>` 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 {},
}
}
Expand Down Expand Up @@ -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<Self>>::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<Self>` 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 {},
}
}
Expand Down

0 comments on commit c57e3b9

Please sign in to comment.