From 4f2a822cb691ba18a748aea31f6bb6bc36eb7a09 Mon Sep 17 00:00:00 2001 From: Lokathor Date: Fri, 24 Jul 2020 20:41:57 -0600 Subject: [PATCH] Correct the error variant returned in some situations. --- src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f21c0b2..fe450db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -164,7 +164,7 @@ pub fn try_from_bytes(s: &[u8]) -> Result<&T, PodCastError> { if s.len() != size_of::() { Err(PodCastError::SizeMismatch) } else if (s.as_ptr() as usize) % align_of::() != 0 { - Err(PodCastError::AlignmentMismatch) + Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned) } else { Ok(unsafe { &*(s.as_ptr() as *const T) }) } @@ -183,7 +183,7 @@ pub fn try_from_bytes_mut( if s.len() != size_of::() { Err(PodCastError::SizeMismatch) } else if (s.as_ptr() as usize) % align_of::() != 0 { - Err(PodCastError::AlignmentMismatch) + Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned) } else { Ok(unsafe { &mut *(s.as_mut_ptr() as *mut T) }) } @@ -205,6 +205,10 @@ pub enum PodCastError { SizeMismatch, /// For this type of cast the alignments must be exactly the same and they /// 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, } impl core::fmt::Display for PodCastError {