Skip to main content

oxicuda_backend/
error.rs

1//! Error and result types for backend operations.
2
3use std::fmt;
4
5/// Error type for backend operations.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum BackendError {
8    /// The requested operation is not supported by this backend.
9    Unsupported(String),
10    /// A GPU/device error occurred.
11    DeviceError(String),
12    /// Invalid argument to an operation.
13    InvalidArgument(String),
14    /// Out of device memory.
15    OutOfMemory,
16    /// Backend not initialized — call [`ComputeBackend::init`](crate::ComputeBackend::init) first.
17    NotInitialized,
18}
19
20impl fmt::Display for BackendError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Self::Unsupported(msg) => write!(f, "unsupported operation: {msg}"),
24            Self::DeviceError(msg) => write!(f, "device error: {msg}"),
25            Self::InvalidArgument(msg) => write!(f, "invalid argument: {msg}"),
26            Self::OutOfMemory => write!(f, "out of device memory"),
27            Self::NotInitialized => write!(f, "backend not initialized"),
28        }
29    }
30}
31
32impl std::error::Error for BackendError {}
33
34/// Result type for backend operations.
35pub type BackendResult<T> = Result<T, BackendError>;
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn backend_error_display() {
43        assert_eq!(
44            BackendError::Unsupported("foo".into()).to_string(),
45            "unsupported operation: foo"
46        );
47        assert_eq!(
48            BackendError::DeviceError("bar".into()).to_string(),
49            "device error: bar"
50        );
51        assert_eq!(
52            BackendError::InvalidArgument("baz".into()).to_string(),
53            "invalid argument: baz"
54        );
55        assert_eq!(
56            BackendError::OutOfMemory.to_string(),
57            "out of device memory"
58        );
59        assert_eq!(
60            BackendError::NotInitialized.to_string(),
61            "backend not initialized"
62        );
63    }
64
65    #[test]
66    fn backend_error_is_std_error() {
67        let err: Box<dyn std::error::Error> = Box::new(BackendError::DeviceError("test".into()));
68        assert!(err.to_string().contains("test"));
69    }
70}