ndarray/
error.rs

1// Copyright 2014-2016 bluss and ndarray developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8use super::Dimension;
9#[cfg(feature = "std")]
10use std::error::Error;
11use std::fmt;
12
13/// An error related to array shape or layout.
14#[derive(Clone)]
15pub struct ShapeError
16{
17    // we want to be able to change this representation later
18    repr: ErrorKind,
19}
20
21impl ShapeError
22{
23    /// Return the `ErrorKind` of this error.
24    #[inline]
25    pub fn kind(&self) -> ErrorKind
26    {
27        self.repr
28    }
29
30    /// Create a new `ShapeError`
31    pub fn from_kind(error: ErrorKind) -> Self
32    {
33        from_kind(error)
34    }
35}
36
37/// Error code for an error related to array shape or layout.
38///
39/// This enumeration is not exhaustive. The representation of the enum
40/// is not guaranteed.
41#[non_exhaustive]
42#[derive(Copy, Clone, Debug)]
43pub enum ErrorKind
44{
45    /// incompatible shape
46    IncompatibleShape = 1,
47    /// incompatible memory layout
48    IncompatibleLayout,
49    /// the shape does not fit inside type limits
50    RangeLimited,
51    /// out of bounds indexing
52    OutOfBounds,
53    /// aliasing array elements
54    Unsupported,
55    /// overflow when computing offset, length, etc.
56    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}