Correct the error variant returned in some situations.

This commit is contained in:
Lokathor 2020-07-24 20:41:57 -06:00
parent 8b43015301
commit 4f2a822cb6

View File

@ -164,7 +164,7 @@ pub fn try_from_bytes<T: Pod>(s: &[u8]) -> Result<&T, PodCastError> {
if s.len() != size_of::<T>() { if s.len() != size_of::<T>() {
Err(PodCastError::SizeMismatch) Err(PodCastError::SizeMismatch)
} else if (s.as_ptr() as usize) % align_of::<T>() != 0 { } else if (s.as_ptr() as usize) % align_of::<T>() != 0 {
Err(PodCastError::AlignmentMismatch) Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
} else { } else {
Ok(unsafe { &*(s.as_ptr() as *const T) }) Ok(unsafe { &*(s.as_ptr() as *const T) })
} }
@ -183,7 +183,7 @@ pub fn try_from_bytes_mut<T: Pod>(
if s.len() != size_of::<T>() { if s.len() != size_of::<T>() {
Err(PodCastError::SizeMismatch) Err(PodCastError::SizeMismatch)
} else if (s.as_ptr() as usize) % align_of::<T>() != 0 { } else if (s.as_ptr() as usize) % align_of::<T>() != 0 {
Err(PodCastError::AlignmentMismatch) Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
} else { } else {
Ok(unsafe { &mut *(s.as_mut_ptr() as *mut T) }) Ok(unsafe { &mut *(s.as_mut_ptr() as *mut T) })
} }
@ -205,6 +205,10 @@ pub enum PodCastError {
SizeMismatch, SizeMismatch,
/// For this type of cast the alignments must be exactly the same and they /// For this type of cast the alignments must be exactly the same and they
/// were not so now you're sad. /// were not so now you're sad.
///
/// This error is generated **only** by operations that cast allocated types
/// (such as `Box` and `Vec`), because in that case the alignment must stay
/// exact.
AlignmentMismatch, AlignmentMismatch,
} }
impl core::fmt::Display for PodCastError { impl core::fmt::Display for PodCastError {