1#[cfg(not(no_zerocopy_core_error_1_81_0))]
121use core::error::Error;
122use core::{
123 convert::Infallible,
124 fmt::{self, Debug, Write},
125 ops::Deref,
126};
127#[cfg(all(no_zerocopy_core_error_1_81_0, any(feature = "std", test)))]
128use std::error::Error;
129
130use crate::{util::SendSyncPhantomData, KnownLayout, TryFromBytes, Unaligned};
131#[cfg(doc)]
132use crate::{FromBytes, Ref};
133
134#[derive(PartialEq, Eq, Clone)]
155pub enum ConvertError<A, S, V> {
156 Alignment(A),
158 Size(S),
160 Validity(V),
162}
163
164impl<Src, Dst: ?Sized + Unaligned, S, V> From<ConvertError<AlignmentError<Src, Dst>, S, V>>
165 for ConvertError<Infallible, S, V>
166{
167 #[inline]
200 fn from(err: ConvertError<AlignmentError<Src, Dst>, S, V>) -> ConvertError<Infallible, S, V> {
201 match err {
202 ConvertError::Alignment(e) => {
203 #[allow(unreachable_code)]
204 return ConvertError::Alignment(Infallible::from(e));
205 }
206 ConvertError::Size(e) => ConvertError::Size(e),
207 ConvertError::Validity(e) => ConvertError::Validity(e),
208 }
209 }
210}
211
212impl<A: fmt::Debug, S: fmt::Debug, V: fmt::Debug> fmt::Debug for ConvertError<A, S, V> {
213 #[inline]
214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215 match self {
216 Self::Alignment(e) => f.debug_tuple("Alignment").field(e).finish(),
217 Self::Size(e) => f.debug_tuple("Size").field(e).finish(),
218 Self::Validity(e) => f.debug_tuple("Validity").field(e).finish(),
219 }
220 }
221}
222
223impl<A: fmt::Display, S: fmt::Display, V: fmt::Display> fmt::Display for ConvertError<A, S, V> {
229 #[inline]
230 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
231 match self {
232 Self::Alignment(e) => e.fmt(f),
233 Self::Size(e) => e.fmt(f),
234 Self::Validity(e) => e.fmt(f),
235 }
236 }
237}
238
239#[cfg(any(not(no_zerocopy_core_error_1_81_0), feature = "std", test))]
240#[cfg_attr(doc_cfg, doc(cfg(all(rust = "1.81.0", feature = "std"))))]
241impl<A, S, V> Error for ConvertError<A, S, V>
242where
243 A: fmt::Display + fmt::Debug,
244 S: fmt::Display + fmt::Debug,
245 V: fmt::Display + fmt::Debug,
246{
247}
248
249pub struct AlignmentError<Src, Dst: ?Sized> {
251 src: Src,
253 _dst: SendSyncPhantomData<Dst>,
258}
259
260impl<Src, Dst: ?Sized> AlignmentError<Src, Dst> {
261 pub(crate) unsafe fn new_unchecked(src: Src) -> Self {
266 Self { src, _dst: SendSyncPhantomData::default() }
269 }
270
271 #[inline]
273 pub fn into_src(self) -> Src {
274 self.src
275 }
276
277 pub(crate) fn with_src<NewSrc>(self, new_src: NewSrc) -> AlignmentError<NewSrc, Dst> {
278 AlignmentError { src: new_src, _dst: SendSyncPhantomData::default() }
282 }
283
284 #[inline]
305 pub fn map_src<NewSrc>(self, f: impl FnOnce(Src) -> NewSrc) -> AlignmentError<NewSrc, Dst> {
306 AlignmentError { src: f(self.src), _dst: SendSyncPhantomData::default() }
307 }
308
309 pub(crate) fn into<S, V>(self) -> ConvertError<Self, S, V> {
310 ConvertError::Alignment(self)
311 }
312
313 fn display_verbose_extras(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
317 where
318 Src: Deref,
319 Dst: KnownLayout,
320 {
321 #[allow(clippy::as_conversions)]
322 let addr = self.src.deref() as *const _ as *const ();
323 let addr_align = 2usize.pow((crate::util::AsAddress::addr(addr)).trailing_zeros());
324
325 f.write_str("\n\nSource type: ")?;
326 f.write_str(core::any::type_name::<Src>())?;
327
328 f.write_str("\nSource address: ")?;
329 addr.fmt(f)?;
330 f.write_str(" (a multiple of ")?;
331 addr_align.fmt(f)?;
332 f.write_str(")")?;
333
334 f.write_str("\nDestination type: ")?;
335 f.write_str(core::any::type_name::<Dst>())?;
336
337 f.write_str("\nDestination alignment: ")?;
338 <Dst as KnownLayout>::LAYOUT.align.get().fmt(f)?;
339
340 Ok(())
341 }
342}
343
344impl<Src: Clone, Dst: ?Sized> Clone for AlignmentError<Src, Dst> {
345 #[inline]
346 fn clone(&self) -> Self {
347 Self { src: self.src.clone(), _dst: SendSyncPhantomData::default() }
348 }
349}
350
351impl<Src: PartialEq, Dst: ?Sized> PartialEq for AlignmentError<Src, Dst> {
352 #[inline]
353 fn eq(&self, other: &Self) -> bool {
354 self.src == other.src
355 }
356}
357
358impl<Src: Eq, Dst: ?Sized> Eq for AlignmentError<Src, Dst> {}
359
360impl<Src, Dst: ?Sized + Unaligned> From<AlignmentError<Src, Dst>> for Infallible {
361 #[inline(always)]
362 fn from(_: AlignmentError<Src, Dst>) -> Infallible {
363 unsafe { core::hint::unreachable_unchecked() }
368 }
369}
370
371#[cfg(test)]
372impl<Src, Dst> AlignmentError<Src, Dst> {
373 fn new_checked(src: Src) -> AlignmentError<Src, Dst> {
376 assert_ne!(core::mem::align_of::<Dst>(), 1);
377 unsafe { AlignmentError::new_unchecked(src) }
380 }
381}
382
383impl<Src, Dst: ?Sized> fmt::Debug for AlignmentError<Src, Dst> {
384 #[inline]
385 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
386 f.debug_struct("AlignmentError").finish()
387 }
388}
389
390impl<Src, Dst: ?Sized> fmt::Display for AlignmentError<Src, Dst>
396where
397 Src: Deref,
398 Dst: KnownLayout,
399{
400 #[inline]
401 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
402 f.write_str("The conversion failed because the address of the source is not a multiple of the alignment of the destination type.")?;
403
404 if cfg!(debug_assertions) {
405 self.display_verbose_extras(f)
406 } else {
407 Ok(())
408 }
409 }
410}
411
412#[cfg(any(not(no_zerocopy_core_error_1_81_0), feature = "std", test))]
413#[cfg_attr(doc_cfg, doc(cfg(all(rust = "1.81.0", feature = "std"))))]
414impl<Src, Dst: ?Sized> Error for AlignmentError<Src, Dst>
415where
416 Src: Deref,
417 Dst: KnownLayout,
418{
419}
420
421impl<Src, Dst: ?Sized, S, V> From<AlignmentError<Src, Dst>>
422 for ConvertError<AlignmentError<Src, Dst>, S, V>
423{
424 #[inline(always)]
425 fn from(err: AlignmentError<Src, Dst>) -> Self {
426 Self::Alignment(err)
427 }
428}
429
430pub struct SizeError<Src, Dst: ?Sized> {
432 src: Src,
434 _dst: SendSyncPhantomData<Dst>,
436}
437
438impl<Src, Dst: ?Sized> SizeError<Src, Dst> {
439 pub(crate) fn new(src: Src) -> Self {
440 Self { src, _dst: SendSyncPhantomData::default() }
441 }
442
443 #[inline]
445 pub fn into_src(self) -> Src {
446 self.src
447 }
448
449 pub(crate) fn with_src<NewSrc>(self, new_src: NewSrc) -> SizeError<NewSrc, Dst> {
451 SizeError { src: new_src, _dst: SendSyncPhantomData::default() }
452 }
453
454 #[inline]
476 pub fn map_src<NewSrc>(self, f: impl FnOnce(Src) -> NewSrc) -> SizeError<NewSrc, Dst> {
477 SizeError { src: f(self.src), _dst: SendSyncPhantomData::default() }
478 }
479
480 pub(crate) fn with_dst<NewDst: ?Sized>(self) -> SizeError<Src, NewDst> {
482 SizeError { src: self.src, _dst: SendSyncPhantomData::default() }
483 }
484
485 pub(crate) fn into<A, V>(self) -> ConvertError<A, Self, V> {
487 ConvertError::Size(self)
488 }
489
490 fn display_verbose_extras(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
494 where
495 Src: Deref,
496 Dst: KnownLayout,
497 {
498 f.write_str("\nSource type: ")?;
500 f.write_str(core::any::type_name::<Src>())?;
501
502 let src_size = core::mem::size_of_val(&*self.src);
504 f.write_str("\nSource size: ")?;
505 src_size.fmt(f)?;
506 f.write_str(" byte")?;
507 if src_size != 1 {
508 f.write_char('s')?;
509 }
510
511 if let crate::SizeInfo::Sized { size } = Dst::LAYOUT.size_info {
513 f.write_str("\nDestination size: ")?;
514 size.fmt(f)?;
515 f.write_str(" byte")?;
516 if size != 1 {
517 f.write_char('s')?;
518 }
519 }
520
521 f.write_str("\nDestination type: ")?;
523 f.write_str(core::any::type_name::<Dst>())?;
524
525 Ok(())
526 }
527}
528
529impl<Src: Clone, Dst: ?Sized> Clone for SizeError<Src, Dst> {
530 #[inline]
531 fn clone(&self) -> Self {
532 Self { src: self.src.clone(), _dst: SendSyncPhantomData::default() }
533 }
534}
535
536impl<Src: PartialEq, Dst: ?Sized> PartialEq for SizeError<Src, Dst> {
537 #[inline]
538 fn eq(&self, other: &Self) -> bool {
539 self.src == other.src
540 }
541}
542
543impl<Src: Eq, Dst: ?Sized> Eq for SizeError<Src, Dst> {}
544
545impl<Src, Dst: ?Sized> fmt::Debug for SizeError<Src, Dst> {
546 #[inline]
547 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
548 f.debug_struct("SizeError").finish()
549 }
550}
551
552impl<Src, Dst: ?Sized> fmt::Display for SizeError<Src, Dst>
558where
559 Src: Deref,
560 Dst: KnownLayout,
561{
562 #[inline]
563 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
564 f.write_str("The conversion failed because the source was incorrectly sized to complete the conversion into the destination type.")?;
565 if cfg!(debug_assertions) {
566 f.write_str("\n")?;
567 self.display_verbose_extras(f)?;
568 }
569 Ok(())
570 }
571}
572
573#[cfg(any(not(no_zerocopy_core_error_1_81_0), feature = "std", test))]
574#[cfg_attr(doc_cfg, doc(cfg(all(rust = "1.81.0", feature = "std"))))]
575impl<Src, Dst: ?Sized> Error for SizeError<Src, Dst>
576where
577 Src: Deref,
578 Dst: KnownLayout,
579{
580}
581
582impl<Src, Dst: ?Sized, A, V> From<SizeError<Src, Dst>> for ConvertError<A, SizeError<Src, Dst>, V> {
583 #[inline(always)]
584 fn from(err: SizeError<Src, Dst>) -> Self {
585 Self::Size(err)
586 }
587}
588
589pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
591 pub(crate) src: Src,
593 _dst: SendSyncPhantomData<Dst>,
595}
596
597impl<Src, Dst: ?Sized + TryFromBytes> ValidityError<Src, Dst> {
598 pub(crate) fn new(src: Src) -> Self {
599 Self { src, _dst: SendSyncPhantomData::default() }
600 }
601
602 #[inline]
604 pub fn into_src(self) -> Src {
605 self.src
606 }
607
608 #[inline]
629 pub fn map_src<NewSrc>(self, f: impl FnOnce(Src) -> NewSrc) -> ValidityError<NewSrc, Dst> {
630 ValidityError { src: f(self.src), _dst: SendSyncPhantomData::default() }
631 }
632
633 pub(crate) fn into<A, S>(self) -> ConvertError<A, S, Self> {
635 ConvertError::Validity(self)
636 }
637
638 fn display_verbose_extras(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
642 where
643 Dst: KnownLayout,
644 {
645 f.write_str("Destination type: ")?;
646 f.write_str(core::any::type_name::<Dst>())?;
647 Ok(())
648 }
649}
650
651impl<Src: Clone, Dst: ?Sized + TryFromBytes> Clone for ValidityError<Src, Dst> {
652 #[inline]
653 fn clone(&self) -> Self {
654 Self { src: self.src.clone(), _dst: SendSyncPhantomData::default() }
655 }
656}
657
658unsafe impl<Src, NewSrc, Dst> crate::pointer::TryWithError<NewSrc>
661 for crate::ValidityError<Src, Dst>
662where
663 Dst: TryFromBytes + ?Sized,
664{
665 type Inner = Src;
666 type Mapped = crate::ValidityError<NewSrc, Dst>;
667 #[inline]
668 fn map<F: FnOnce(Src) -> NewSrc>(self, f: F) -> Self::Mapped {
669 self.map_src(f)
670 }
671}
672
673impl<Src: PartialEq, Dst: ?Sized + TryFromBytes> PartialEq for ValidityError<Src, Dst> {
674 #[inline]
675 fn eq(&self, other: &Self) -> bool {
676 self.src == other.src
677 }
678}
679
680impl<Src: Eq, Dst: ?Sized + TryFromBytes> Eq for ValidityError<Src, Dst> {}
681
682impl<Src, Dst: ?Sized + TryFromBytes> fmt::Debug for ValidityError<Src, Dst> {
683 #[inline]
684 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
685 f.debug_struct("ValidityError").finish()
686 }
687}
688
689impl<Src, Dst: ?Sized> fmt::Display for ValidityError<Src, Dst>
695where
696 Dst: KnownLayout + TryFromBytes,
697{
698 #[inline]
699 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
700 f.write_str("The conversion failed because the source bytes are not a valid value of the destination type.")?;
701 if cfg!(debug_assertions) {
702 f.write_str("\n\n")?;
703 self.display_verbose_extras(f)?;
704 }
705 Ok(())
706 }
707}
708
709#[cfg(any(not(no_zerocopy_core_error_1_81_0), feature = "std", test))]
710#[cfg_attr(doc_cfg, doc(cfg(all(rust = "1.81.0", feature = "std"))))]
711impl<Src, Dst: ?Sized> Error for ValidityError<Src, Dst> where Dst: KnownLayout + TryFromBytes {}
712
713impl<Src, Dst: ?Sized + TryFromBytes, A, S> From<ValidityError<Src, Dst>>
714 for ConvertError<A, S, ValidityError<Src, Dst>>
715{
716 #[inline(always)]
717 fn from(err: ValidityError<Src, Dst>) -> Self {
718 Self::Validity(err)
719 }
720}
721
722#[allow(type_alias_bounds)]
729pub type CastError<Src, Dst: ?Sized> =
730 ConvertError<AlignmentError<Src, Dst>, SizeError<Src, Dst>, Infallible>;
731
732impl<Src, Dst: ?Sized> CastError<Src, Dst> {
733 #[inline]
735 pub fn into_src(self) -> Src {
736 match self {
737 Self::Alignment(e) => e.src,
738 Self::Size(e) => e.src,
739 Self::Validity(i) => match i {},
740 }
741 }
742
743 pub(crate) fn with_src<NewSrc>(self, new_src: NewSrc) -> CastError<NewSrc, Dst> {
745 match self {
746 Self::Alignment(e) => CastError::Alignment(e.with_src(new_src)),
747 Self::Size(e) => CastError::Size(e.with_src(new_src)),
748 Self::Validity(i) => match i {},
749 }
750 }
751
752 #[inline]
774 pub fn map_src<NewSrc>(self, f: impl FnOnce(Src) -> NewSrc) -> CastError<NewSrc, Dst> {
775 match self {
776 Self::Alignment(e) => CastError::Alignment(e.map_src(f)),
777 Self::Size(e) => CastError::Size(e.map_src(f)),
778 Self::Validity(i) => match i {},
779 }
780 }
781
782 pub(crate) fn into(self) -> TryCastError<Src, Dst>
784 where
785 Dst: TryFromBytes,
786 {
787 match self {
788 Self::Alignment(e) => TryCastError::Alignment(e),
789 Self::Size(e) => TryCastError::Size(e),
790 Self::Validity(i) => match i {},
791 }
792 }
793}
794
795unsafe impl<Src, NewSrc, Dst> crate::pointer::TryWithError<NewSrc> for crate::CastError<Src, Dst>
800where
801 Dst: ?Sized,
802{
803 type Inner = Src;
804 type Mapped = crate::CastError<NewSrc, Dst>;
805
806 #[inline]
807 fn map<F: FnOnce(Src) -> NewSrc>(self, f: F) -> Self::Mapped {
808 self.map_src(f)
809 }
810}
811
812impl<Src, Dst: ?Sized + Unaligned> From<CastError<Src, Dst>> for SizeError<Src, Dst> {
813 #[inline(always)]
853 fn from(err: CastError<Src, Dst>) -> SizeError<Src, Dst> {
854 match err {
855 #[allow(unreachable_code)]
856 CastError::Alignment(e) => match Infallible::from(e) {},
857 CastError::Size(e) => e,
858 CastError::Validity(i) => match i {},
859 }
860 }
861}
862
863#[allow(type_alias_bounds)]
871pub type TryCastError<Src, Dst: ?Sized + TryFromBytes> =
872 ConvertError<AlignmentError<Src, Dst>, SizeError<Src, Dst>, ValidityError<Src, Dst>>;
873
874impl<Src, Dst: ?Sized + TryFromBytes> TryCastError<Src, Dst> {
878 #[inline]
880 pub fn into_src(self) -> Src {
881 match self {
882 Self::Alignment(e) => e.src,
883 Self::Size(e) => e.src,
884 Self::Validity(e) => e.src,
885 }
886 }
887
888 #[inline]
912 pub fn map_src<NewSrc>(self, f: impl FnOnce(Src) -> NewSrc) -> TryCastError<NewSrc, Dst> {
913 match self {
914 Self::Alignment(e) => TryCastError::Alignment(e.map_src(f)),
915 Self::Size(e) => TryCastError::Size(e.map_src(f)),
916 Self::Validity(e) => TryCastError::Validity(e.map_src(f)),
917 }
918 }
919}
920
921impl<Src, Dst: ?Sized + TryFromBytes> From<CastError<Src, Dst>> for TryCastError<Src, Dst> {
922 #[inline]
923 fn from(value: CastError<Src, Dst>) -> Self {
924 match value {
925 CastError::Alignment(e) => Self::Alignment(e),
926 CastError::Size(e) => Self::Size(e),
927 CastError::Validity(i) => match i {},
928 }
929 }
930}
931
932#[allow(type_alias_bounds)]
940pub type TryReadError<Src, Dst: ?Sized + TryFromBytes> =
941 ConvertError<Infallible, SizeError<Src, Dst>, ValidityError<Src, Dst>>;
942
943impl<Src, Dst: ?Sized + TryFromBytes> TryReadError<Src, Dst> {
944 #[inline]
946 pub fn into_src(self) -> Src {
947 match self {
948 Self::Alignment(i) => match i {},
949 Self::Size(e) => e.src,
950 Self::Validity(e) => e.src,
951 }
952 }
953
954 #[inline]
978 pub fn map_src<NewSrc>(self, f: impl FnOnce(Src) -> NewSrc) -> TryReadError<NewSrc, Dst> {
979 match self {
980 Self::Alignment(i) => match i {},
981 Self::Size(e) => TryReadError::Size(e.map_src(f)),
982 Self::Validity(e) => TryReadError::Validity(e.map_src(f)),
983 }
984 }
985}
986
987#[allow(type_alias_bounds)]
1021pub type AlignedTryCastError<Src, Dst: ?Sized + TryFromBytes> =
1022 ConvertError<Infallible, SizeError<Src, Dst>, ValidityError<Src, Dst>>;
1023
1024#[derive(Copy, Clone, PartialEq, Eq, Debug)]
1036pub struct AllocError;
1037
1038#[cfg(test)]
1039mod tests {
1040 use core::convert::Infallible;
1041
1042 use super::*;
1043
1044 #[test]
1045 fn test_send_sync() {
1046 #[allow(dead_code)]
1050 fn is_send_sync<T: Send + Sync>(_t: T) {}
1051
1052 #[allow(dead_code)]
1053 fn alignment_err_is_send_sync<Src: Send + Sync, Dst>(err: AlignmentError<Src, Dst>) {
1054 is_send_sync(err)
1055 }
1056
1057 #[allow(dead_code)]
1058 fn size_err_is_send_sync<Src: Send + Sync, Dst>(err: SizeError<Src, Dst>) {
1059 is_send_sync(err)
1060 }
1061
1062 #[allow(dead_code)]
1063 fn validity_err_is_send_sync<Src: Send + Sync, Dst: TryFromBytes>(
1064 err: ValidityError<Src, Dst>,
1065 ) {
1066 is_send_sync(err)
1067 }
1068
1069 #[allow(dead_code)]
1070 fn convert_error_is_send_sync<Src: Send + Sync, Dst: TryFromBytes>(
1071 err: ConvertError<
1072 AlignmentError<Src, Dst>,
1073 SizeError<Src, Dst>,
1074 ValidityError<Src, Dst>,
1075 >,
1076 ) {
1077 is_send_sync(err)
1078 }
1079 }
1080
1081 #[test]
1082 fn test_eq_partial_eq_clone() {
1083 #[allow(dead_code)]
1088 fn is_eq_partial_eq_clone<T: Eq + PartialEq + Clone>(_t: T) {}
1089
1090 #[allow(dead_code)]
1091 fn alignment_err_is_eq_partial_eq_clone<Src: Eq + PartialEq + Clone, Dst>(
1092 err: AlignmentError<Src, Dst>,
1093 ) {
1094 is_eq_partial_eq_clone(err)
1095 }
1096
1097 #[allow(dead_code)]
1098 fn size_err_is_eq_partial_eq_clone<Src: Eq + PartialEq + Clone, Dst>(
1099 err: SizeError<Src, Dst>,
1100 ) {
1101 is_eq_partial_eq_clone(err)
1102 }
1103
1104 #[allow(dead_code)]
1105 fn validity_err_is_eq_partial_eq_clone<Src: Eq + PartialEq + Clone, Dst: TryFromBytes>(
1106 err: ValidityError<Src, Dst>,
1107 ) {
1108 is_eq_partial_eq_clone(err)
1109 }
1110
1111 #[allow(dead_code)]
1112 fn convert_error_is_eq_partial_eq_clone<Src: Eq + PartialEq + Clone, Dst: TryFromBytes>(
1113 err: ConvertError<
1114 AlignmentError<Src, Dst>,
1115 SizeError<Src, Dst>,
1116 ValidityError<Src, Dst>,
1117 >,
1118 ) {
1119 is_eq_partial_eq_clone(err)
1120 }
1121 }
1122
1123 #[test]
1124 fn alignment_display() {
1125 #[repr(C, align(128))]
1126 struct Aligned {
1127 bytes: [u8; 128],
1128 }
1129
1130 impl_known_layout!(elain::Align::<8>);
1131
1132 let aligned = Aligned { bytes: [0; 128] };
1133
1134 let bytes = &aligned.bytes[1..];
1135 let addr = crate::util::AsAddress::addr(bytes);
1136 assert_eq!(
1137 AlignmentError::<_, elain::Align::<8>>::new_checked(bytes).to_string(),
1138 format!("The conversion failed because the address of the source is not a multiple of the alignment of the destination type.\n\
1139 \nSource type: &[u8]\
1140 \nSource address: 0x{:x} (a multiple of 1)\
1141 \nDestination type: elain::Align<8>\
1142 \nDestination alignment: 8", addr)
1143 );
1144
1145 let bytes = &aligned.bytes[2..];
1146 let addr = crate::util::AsAddress::addr(bytes);
1147 assert_eq!(
1148 AlignmentError::<_, elain::Align::<8>>::new_checked(bytes).to_string(),
1149 format!("The conversion failed because the address of the source is not a multiple of the alignment of the destination type.\n\
1150 \nSource type: &[u8]\
1151 \nSource address: 0x{:x} (a multiple of 2)\
1152 \nDestination type: elain::Align<8>\
1153 \nDestination alignment: 8", addr)
1154 );
1155
1156 let bytes = &aligned.bytes[3..];
1157 let addr = crate::util::AsAddress::addr(bytes);
1158 assert_eq!(
1159 AlignmentError::<_, elain::Align::<8>>::new_checked(bytes).to_string(),
1160 format!("The conversion failed because the address of the source is not a multiple of the alignment of the destination type.\n\
1161 \nSource type: &[u8]\
1162 \nSource address: 0x{:x} (a multiple of 1)\
1163 \nDestination type: elain::Align<8>\
1164 \nDestination alignment: 8", addr)
1165 );
1166
1167 let bytes = &aligned.bytes[4..];
1168 let addr = crate::util::AsAddress::addr(bytes);
1169 assert_eq!(
1170 AlignmentError::<_, elain::Align::<8>>::new_checked(bytes).to_string(),
1171 format!("The conversion failed because the address of the source is not a multiple of the alignment of the destination type.\n\
1172 \nSource type: &[u8]\
1173 \nSource address: 0x{:x} (a multiple of 4)\
1174 \nDestination type: elain::Align<8>\
1175 \nDestination alignment: 8", addr)
1176 );
1177 }
1178
1179 #[test]
1180 fn size_display() {
1181 assert_eq!(
1182 SizeError::<_, [u8]>::new(&[0u8; 2][..]).to_string(),
1183 "The conversion failed because the source was incorrectly sized to complete the conversion into the destination type.\n\
1184 \nSource type: &[u8]\
1185 \nSource size: 2 bytes\
1186 \nDestination type: [u8]"
1187 );
1188
1189 assert_eq!(
1190 SizeError::<_, [u8; 2]>::new(&[0u8; 1][..]).to_string(),
1191 "The conversion failed because the source was incorrectly sized to complete the conversion into the destination type.\n\
1192 \nSource type: &[u8]\
1193 \nSource size: 1 byte\
1194 \nDestination size: 2 bytes\
1195 \nDestination type: [u8; 2]"
1196 );
1197 }
1198
1199 #[test]
1200 fn validity_display() {
1201 assert_eq!(
1202 ValidityError::<_, bool>::new(&[2u8; 1][..]).to_string(),
1203 "The conversion failed because the source bytes are not a valid value of the destination type.\n\
1204 \n\
1205 Destination type: bool"
1206 );
1207 }
1208
1209 #[test]
1210 fn test_convert_error_debug() {
1211 let err: ConvertError<
1212 AlignmentError<&[u8], u16>,
1213 SizeError<&[u8], u16>,
1214 ValidityError<&[u8], bool>,
1215 > = ConvertError::Alignment(AlignmentError::new_checked(&[0u8]));
1216 assert_eq!(format!("{:?}", err), "Alignment(AlignmentError)");
1217
1218 let err: ConvertError<
1219 AlignmentError<&[u8], u16>,
1220 SizeError<&[u8], u16>,
1221 ValidityError<&[u8], bool>,
1222 > = ConvertError::Size(SizeError::new(&[0u8]));
1223 assert_eq!(format!("{:?}", err), "Size(SizeError)");
1224
1225 let err: ConvertError<
1226 AlignmentError<&[u8], u16>,
1227 SizeError<&[u8], u16>,
1228 ValidityError<&[u8], bool>,
1229 > = ConvertError::Validity(ValidityError::new(&[0u8]));
1230 assert_eq!(format!("{:?}", err), "Validity(ValidityError)");
1231 }
1232
1233 #[test]
1234 fn test_convert_error_from_unaligned() {
1235 let err: ConvertError<
1237 AlignmentError<&[u8], u8>,
1238 SizeError<&[u8], u8>,
1239 ValidityError<&[u8], bool>,
1240 > = ConvertError::Size(SizeError::new(&[0u8]));
1241 let converted: ConvertError<Infallible, SizeError<&[u8], u8>, ValidityError<&[u8], bool>> =
1242 ConvertError::from(err);
1243 match converted {
1244 ConvertError::Size(_) => {}
1245 _ => panic!("Expected Size error"),
1246 }
1247 }
1248
1249 #[test]
1250 fn test_alignment_error_display_debug() {
1251 let err: AlignmentError<&[u8], u16> = AlignmentError::new_checked(&[0u8]);
1252 assert!(format!("{:?}", err).contains("AlignmentError"));
1253 assert!(format!("{}", err).contains("address of the source is not a multiple"));
1254 }
1255
1256 #[test]
1257 fn test_size_error_display_debug() {
1258 let err: SizeError<&[u8], u16> = SizeError::new(&[0u8]);
1259 assert!(format!("{:?}", err).contains("SizeError"));
1260 assert!(format!("{}", err).contains("source was incorrectly sized"));
1261 }
1262
1263 #[test]
1264 fn test_validity_error_display_debug() {
1265 let err: ValidityError<&[u8], bool> = ValidityError::new(&[0u8]);
1266 assert!(format!("{:?}", err).contains("ValidityError"));
1267 assert!(format!("{}", err).contains("source bytes are not a valid value"));
1268 }
1269
1270 #[test]
1271 fn test_convert_error_display_debug_more() {
1272 let err: ConvertError<
1273 AlignmentError<&[u8], u16>,
1274 SizeError<&[u8], u16>,
1275 ValidityError<&[u8], bool>,
1276 > = ConvertError::Alignment(AlignmentError::new_checked(&[0u8]));
1277 assert!(format!("{}", err).contains("address of the source is not a multiple"));
1278
1279 let err: ConvertError<
1280 AlignmentError<&[u8], u16>,
1281 SizeError<&[u8], u16>,
1282 ValidityError<&[u8], bool>,
1283 > = ConvertError::Size(SizeError::new(&[0u8]));
1284 assert!(format!("{}", err).contains("source was incorrectly sized"));
1285
1286 let err: ConvertError<
1287 AlignmentError<&[u8], u16>,
1288 SizeError<&[u8], u16>,
1289 ValidityError<&[u8], bool>,
1290 > = ConvertError::Validity(ValidityError::new(&[0u8]));
1291 assert!(format!("{}", err).contains("source bytes are not a valid value"));
1292 }
1293
1294 #[test]
1295 fn test_alignment_error_methods() {
1296 let err: AlignmentError<&[u8], u16> = AlignmentError::new_checked(&[0u8]);
1297
1298 let src = err.clone().into_src();
1300 assert_eq!(src, &[0u8]);
1301
1302 let converted: ConvertError<
1304 AlignmentError<&[u8], u16>,
1305 SizeError<&[u8], u16>,
1306 ValidityError<&[u8], bool>,
1307 > = err.clone().into();
1308 match converted {
1309 ConvertError::Alignment(_) => {}
1310 _ => panic!("Expected Alignment error"),
1311 }
1312
1313 let cloned = err.clone();
1315 assert_eq!(err, cloned);
1316
1317 assert_eq!(err, cloned);
1319 let err2: AlignmentError<&[u8], u16> = AlignmentError::new_checked(&[1u8]);
1320 assert_ne!(err, err2);
1321 }
1322
1323 #[test]
1324 fn test_convert_error_from_unaligned_variants() {
1325 let err: ConvertError<
1327 AlignmentError<&[u8], u8>,
1328 SizeError<&[u8], u8>,
1329 ValidityError<&[u8], bool>,
1330 > = ConvertError::Validity(ValidityError::new(&[0u8]));
1331 let converted: ConvertError<Infallible, SizeError<&[u8], u8>, ValidityError<&[u8], bool>> =
1332 ConvertError::from(err);
1333 match converted {
1334 ConvertError::Validity(_) => {}
1335 _ => panic!("Expected Validity error"),
1336 }
1337
1338 let err: ConvertError<
1339 AlignmentError<&[u8], u8>,
1340 SizeError<&[u8], u8>,
1341 ValidityError<&[u8], bool>,
1342 > = ConvertError::Size(SizeError::new(&[0u8]));
1343 let converted: ConvertError<Infallible, SizeError<&[u8], u8>, ValidityError<&[u8], bool>> =
1344 ConvertError::from(err);
1345 match converted {
1346 ConvertError::Size(_) => {}
1347 _ => panic!("Expected Size error"),
1348 }
1349 }
1350}