1use super::Dimension;
9#[cfg(feature = "std")]
10use std::error::Error;
11use std::fmt;
12
13#[derive(Clone)]
15pub struct ShapeError
16{
17 repr: ErrorKind,
19}
20
21impl ShapeError
22{
23 #[inline]
25 pub fn kind(&self) -> ErrorKind
26 {
27 self.repr
28 }
29
30 pub fn from_kind(error: ErrorKind) -> Self
32 {
33 from_kind(error)
34 }
35}
36
37#[non_exhaustive]
42#[derive(Copy, Clone, Debug)]
43pub enum ErrorKind
44{
45 IncompatibleShape = 1,
47 IncompatibleLayout,
49 RangeLimited,
51 OutOfBounds,
53 Unsupported,
55 Overflow,
57}
58
59#[inline(always)]
60pub fn from_kind(k: ErrorKind) -> ShapeError
61{
62 ShapeError { repr: k }
63}
64
65impl PartialEq for ErrorKind
66{
67 #[inline(always)]
68 fn eq(&self, rhs: &Self) -> bool
69 {
70 *self as u8 == *rhs as u8
71 }
72}
73
74impl PartialEq for ShapeError
75{
76 #[inline(always)]
77 fn eq(&self, rhs: &Self) -> bool
78 {
79 self.repr == rhs.repr
80 }
81}
82
83#[cfg(feature = "std")]
84impl Error for ShapeError {}
85
86impl fmt::Display for ShapeError
87{
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
89 {
90 let description = match self.kind() {
91 ErrorKind::IncompatibleShape => "incompatible shapes",
92 ErrorKind::IncompatibleLayout => "incompatible memory layout",
93 ErrorKind::RangeLimited => "the shape does not fit in type limits",
94 ErrorKind::OutOfBounds => "out of bounds indexing",
95 ErrorKind::Unsupported => "unsupported operation",
96 ErrorKind::Overflow => "arithmetic overflow",
97 };
98 write!(f, "ShapeError/{:?}: {}", self.kind(), description)
99 }
100}
101
102impl fmt::Debug for ShapeError
103{
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
105 {
106 write!(f, "{}", self)
107 }
108}
109
110pub fn incompatible_shapes<D, E>(_a: &D, _b: &E) -> ShapeError
111where
112 D: Dimension,
113 E: Dimension,
114{
115 from_kind(ErrorKind::IncompatibleShape)
116}