Skip to main content

ironaccelerator_core/
error.rs

1//! Unified error type. Backend errors are erased to a small enum + opaque code
2//! so the hot path never allocates.
3
4use core::fmt;
5
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum Error {
9    /// Backend not compiled in or runtime library not found.
10    BackendUnavailable(&'static str),
11    /// Operation is not supported by the selected device / driver.
12    Unsupported(&'static str),
13    /// Out of device or pinned host memory.
14    OutOfMemory,
15    /// Kernel launch failed; opaque driver code stored verbatim.
16    LaunchFailure { code: i32 },
17    /// Misuse: violated an `_unchecked` precondition.
18    InvalidArgument(&'static str),
19    /// Backend-specific error wrapped as a code so we don't allocate on hot
20    /// paths. Backends keep their own decode tables.
21    Backend {
22        backend: crate::BackendKind,
23        code: i64,
24    },
25    /// Anything else.
26    Other(&'static str),
27}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Error::BackendUnavailable(b) => write!(f, "backend `{b}` unavailable"),
33            Error::Unsupported(s) => write!(f, "unsupported: {s}"),
34            Error::OutOfMemory => f.write_str("out of memory"),
35            Error::LaunchFailure { code } => write!(f, "kernel launch failed (code {code})"),
36            Error::InvalidArgument(s) => write!(f, "invalid argument: {s}"),
37            Error::Backend { backend, code } => write!(f, "{backend:?} error {code}"),
38            Error::Other(s) => f.write_str(s),
39        }
40    }
41}
42
43#[cfg(feature = "std")]
44impl std::error::Error for Error {}
45
46pub type Result<T, E = Error> = core::result::Result<T, E>;