Skip to main content

rusty_cl/
result.rs

1use std::fmt::{Display, Formatter};
2
3/// Type representing an error code returned by OpenCL functions.
4#[derive(Eq, PartialEq, Copy, Clone, Debug)]
5#[repr(i32)]
6pub enum Error {
7    /// An unsupported error code was received.
8    Unknown = 1,
9
10    /// The operation succeeded.
11    Success = 0,
12
13    /// An invalid value was passed as parameter.
14    InvalidValue = -30,
15
16    /// An invalid platform ID was passed as parameter.
17    InvalidPlatform = -32,
18}
19
20impl Default for Error {
21    /// The default error code is [Error::Unknown].
22    fn default() -> Self {
23        Error::Unknown
24    }
25}
26
27impl From<i32> for Error {
28    fn from(code: i32) -> Self {
29        match code {
30            0 => Error::Success,
31            -30 => Error::InvalidValue,
32            -32 => Error::InvalidPlatform,
33            _ => Error::Unknown,
34        }
35    }
36}
37
38impl Display for Error {
39    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40        match *self {
41            Error::Unknown => write!(f, "An unknown error occurred."),
42            Error::Success => write!(f, "The operation completed successfully."),
43            Error::InvalidValue => write!(f, "An invalid value was passed as parameter."),
44            Error::InvalidPlatform => write!(f, "An invalid platform ID was passed as parameter."),
45        }
46    }
47}
48
49/// The result type for OpenCL operations.
50pub type Result<T> = std::result::Result<T, Error>;
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn default_error_code() {
58        assert_eq!(Error::default(), Error::Unknown);
59    }
60}