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")]
84#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
85impl Error for ShapeError {}
86
87impl fmt::Display for ShapeError
88{
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
90 {
91 let description = match self.kind() {
92 ErrorKind::IncompatibleShape => "incompatible shapes",
93 ErrorKind::IncompatibleLayout => "incompatible memory layout",
94 ErrorKind::RangeLimited => "the shape does not fit in type limits",
95 ErrorKind::OutOfBounds => "out of bounds indexing",
96 ErrorKind::Unsupported => "unsupported operation",
97 ErrorKind::Overflow => "arithmetic overflow",
98 };
99 write!(f, "ShapeError/{:?}: {}", self.kind(), description)
100 }
101}
102
103impl fmt::Debug for ShapeError
104{
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
106 {
107 write!(f, "{}", self)
108 }
109}
110
111pub fn incompatible_shapes<D, E>(_a: &D, _b: &E) -> ShapeError
112where
113 D: Dimension,
114 E: Dimension,
115{
116 from_kind(ErrorKind::IncompatibleShape)
117}