where
Self: Sized,
{
- if bytes.len() < size_of::<Self>() {
- None
- } else {
- // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
- // panic.
- // TODO: replace with `split_at_checked` once the MSRV is >= 1.80.
- let (prefix, remainder) = bytes.split_at(size_of::<Self>());
+ let (prefix, remainder) = bytes.split_at_checked(size_of::<Self>())?;
- Self::from_bytes(prefix).map(|s| (s, remainder))
- }
+ Self::from_bytes(prefix).map(|s| (s, remainder))
}
/// Converts a mutable slice of bytes to a reference to `Self`.
where
Self: AsBytes + Sized,
{
- if bytes.len() < size_of::<Self>() {
- None
- } else {
- // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at_mut` cannot
- // panic.
- // TODO: replace with `split_at_mut_checked` once the MSRV is >= 1.80.
- let (prefix, remainder) = bytes.split_at_mut(size_of::<Self>());
+ let (prefix, remainder) = bytes.split_at_mut_checked(size_of::<Self>())?;
- Self::from_bytes_mut(prefix).map(|s| (s, remainder))
- }
+ Self::from_bytes_mut(prefix).map(|s| (s, remainder))
}
/// Creates an owned instance of `Self` by copying `bytes`.
where
Self: Sized,
{
- if bytes.len() < size_of::<Self>() {
- None
- } else {
- // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
- // panic.
- // TODO: replace with `split_at_checked` once the MSRV is >= 1.80.
- let (prefix, remainder) = bytes.split_at(size_of::<Self>());
+ let (prefix, remainder) = bytes.split_at_checked(size_of::<Self>())?;
- Self::from_bytes_copy(prefix).map(|s| (s, remainder))
- }
+ Self::from_bytes_copy(prefix).map(|s| (s, remainder))
}
}