Skip to main content

nuvec_gpu/
error.rs

1//! Errors of the Metal runtime.
2
3use std::fmt;
4
5/// Why a GPU operation failed.
6#[derive(Debug, Clone, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum GpuError {
9    /// The kernel library is unavailable in this build (non-macOS target).
10    NoKernels,
11    /// The kernel library could not be loaded or compiled.
12    LibraryLoad(String),
13    /// No kernel with this name exists in the library.
14    MissingKernel(String),
15    /// A compute pipeline could not be created.
16    Pipeline {
17        /// The kernel name.
18        name: String,
19        /// Metal's description.
20        message: String,
21    },
22    /// A dispatch argument is invalid (offset out of range, too many arguments, …).
23    InvalidArgument(String),
24    /// The buffer is used by submitted GPU work that has not been waited on.
25    BufferInUse,
26    /// Metal could not allocate a buffer or command object of this many bytes.
27    OutOfMemory(usize),
28    /// The command buffer failed on the GPU.
29    CommandBuffer(String),
30    /// A file could not be opened or mapped.
31    Io(String),
32    /// The buffer is a read-only mapped file.
33    ReadOnly,
34}
35
36/// `Result` with [`GpuError`].
37pub type Result<T> = std::result::Result<T, GpuError>;
38
39impl fmt::Display for GpuError {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Self::NoKernels => f.write_str("the Metal kernel library is unavailable in this build"),
43            Self::LibraryLoad(msg) => write!(f, "cannot load the Metal kernel library: {msg}"),
44            Self::MissingKernel(name) => write!(f, "no Metal kernel named `{name}`"),
45            Self::Pipeline { name, message } => {
46                write!(f, "cannot create pipeline `{name}`: {message}")
47            }
48            Self::InvalidArgument(msg) => write!(f, "invalid dispatch argument: {msg}"),
49            Self::BufferInUse => {
50                f.write_str("buffer is in use by submitted GPU work; wait for it first")
51            }
52            Self::OutOfMemory(n) => write!(f, "Metal could not allocate {n} bytes"),
53            Self::CommandBuffer(msg) => write!(f, "GPU command buffer failed: {msg}"),
54            Self::Io(msg) => write!(f, "I/O error: {msg}"),
55            Self::ReadOnly => f.write_str("the buffer is a read-only mapped file"),
56        }
57    }
58}
59
60impl std::error::Error for GpuError {}