Skip to main content

nuvec_core/
error.rs

1//! The error type returned by fallible operations.
2
3use std::fmt;
4
5use crate::{DType, Device};
6
7/// Why an operation failed.
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum Error {
11    /// Operand shapes are incompatible.
12    ShapeMismatch {
13        /// The operation that rejected the shapes.
14        op: &'static str,
15        /// Shape of the left operand.
16        lhs: Vec<usize>,
17        /// Shape of the right operand.
18        rhs: Vec<usize>,
19    },
20    /// Operand element types are incompatible.
21    DTypeMismatch {
22        /// The operation that rejected the types.
23        op: &'static str,
24        /// Element type of the left operand (or of the array).
25        lhs: DType,
26        /// Element type of the right operand (or the requested type).
27        rhs: DType,
28    },
29    /// The number of host values does not match the shape.
30    SizeMismatch {
31        /// Element count implied by the shape.
32        expected: usize,
33        /// Number of values provided.
34        actual: usize,
35    },
36    /// The operation does not support this element type.
37    UnsupportedDType {
38        /// The operation that rejected the type.
39        op: &'static str,
40        /// The unsupported element type.
41        dtype: DType,
42    },
43    /// Memory for this many bytes could not be allocated (or exceeds the memory limit).
44    OutOfMemory(usize),
45    /// The GPU backend failed (for example, a command buffer error).
46    Backend(String),
47    /// The device has no backend yet.
48    UnsupportedDevice(Device),
49    /// An argument is invalid for another reason.
50    InvalidArgument(String),
51    /// Reading or writing a file failed.
52    Io(String),
53}
54
55/// `Result` with [`Error`] as the error type.
56pub type Result<T> = std::result::Result<T, Error>;
57
58impl fmt::Display for Error {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        match self {
61            Self::ShapeMismatch { op, lhs, rhs } => {
62                write!(f, "{op}: shape mismatch, {lhs:?} vs {rhs:?}")
63            }
64            Self::DTypeMismatch { op, lhs, rhs } => {
65                write!(f, "{op}: dtype mismatch, {lhs} vs {rhs}")
66            }
67            Self::SizeMismatch { expected, actual } => {
68                write!(
69                    f,
70                    "size mismatch: shape needs {expected} values, got {actual}"
71                )
72            }
73            Self::UnsupportedDType { op, dtype } => write!(f, "{op}: not supported for {dtype}"),
74            Self::OutOfMemory(n) => write!(f, "out of memory: cannot allocate {n} bytes"),
75            Self::Backend(msg) => write!(f, "GPU backend error: {msg}"),
76            Self::UnsupportedDevice(device) => write!(f, "no backend for device {device} yet"),
77            Self::InvalidArgument(msg) => write!(f, "invalid argument: {msg}"),
78            Self::Io(msg) => write!(f, "I/O error: {msg}"),
79        }
80    }
81}
82
83impl std::error::Error for Error {}