use thiserror::Error;
pub type AccelResult<T> = Result<T, AccelError>;
pub const CONTEXT_POISONED_TAG: &str = "ContextPoisoned";
pub const OUT_OF_MEMORY_TAG: &str = "OutOfMemory";
pub const UNRECOVERABLE_TAG: &str = "Unrecoverable";
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AccelError {
#[error("ContextPoisoned: {0}")]
ContextPoisoned(String),
#[error("OutOfMemory: {0}")]
OutOfMemory(String),
#[error("Unrecoverable: {0}")]
Unrecoverable(String),
#[error("AccelRef stale: {0}")]
AccelRefStale(&'static str),
#[error("driver error: {0}")]
Driver(String),
#[error("{lib} error: {msg}")]
LibraryError { lib: &'static str, msg: String },
#[error("ask timed out before completion")]
Timeout,
}
impl AccelError {
pub fn lib(lib: &'static str, msg: impl Into<String>) -> Self {
Self::LibraryError {
lib,
msg: msg.into(),
}
}
pub fn panic_message(&self) -> String {
self.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn library_error_constructor() {
let e = AccelError::lib("cudnn", "create_handle failed");
match e {
AccelError::LibraryError { lib, msg } => {
assert_eq!(lib, "cudnn");
assert!(msg.contains("create_handle"));
}
_ => panic!("expected LibraryError"),
}
}
#[test]
fn panic_message_carries_tag() {
let e = AccelError::ContextPoisoned("cuInit failed".into());
let m = e.panic_message();
assert!(m.contains(CONTEXT_POISONED_TAG));
}
}