1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! Standard error type for ocl futures.
//!

use std;
// use std::sync::mpsc::{SendError as StdMpscSendError, RecvError as StdMpscRecvError};
use failure::{Context, Fail, Backtrace};
use futures::sync::oneshot::Canceled as OneshotCanceled;
use futures::sync::mpsc::SendError;
use crate::core::error::{Error as OclCoreError};
use crate::core::Status;
use crate::standard::{DeviceError, PlatformError, KernelError};

use crate::BufferCmdError;

pub type Result<T> = std::result::Result<T, Error>;


/// An enum containing either a `String` or one of several other error types.
///
/// Implements the usual error traits.
#[derive(Debug, Fail)]
pub enum ErrorKind {
    #[fail(display = "{}", _0)]
    OclCore(OclCoreError),
    #[fail(display = "{}", _0)]
    FuturesMpscSend(String),
    // #[fail(display = "{}", _0)]
    // StdMpscSend(String),
    // #[fail(display = "{}", _0)]
    // StdMpscRecv(StdMpscRecvError),
    #[fail(display = "{}", _0)]
    OneshotCanceled(#[cause] OneshotCanceled),
    #[fail(display = "{}", _0)]
    BufferCmd(BufferCmdError),
    #[fail(display = "{}", _0)]
    Device(DeviceError),
    #[fail(display = "{}", _0)]
    Platform(PlatformError),
    #[fail(display = "{}", _0)]
    Kernel(KernelError),
}


/// An Error.
pub struct Error {
    inner: Context<ErrorKind>,
}

impl Error {
    /// Returns the error status code for `OclCore` variants.
    pub fn api_status(&self) -> Option<Status> {
        match *self.kind() {
            ErrorKind::OclCore(ref err) => err.api_status(),
            _ => None,
        }
    }

    /// Returns the error variant and contents.
    pub fn kind(&self) -> &ErrorKind {
        self.inner.get_context()
    }

    /// Returns the immediate cause of this error (e.g. the next error in the
    /// chain).
    pub fn cause(&self) -> Option<&dyn Fail> {
        self.inner.cause()
    }
}

impl Fail for Error {
    fn cause(&self) -> Option<&dyn Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        std::fmt::Display::fmt(&self.inner, f)
    }
}

impl std::fmt::Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        std::fmt::Display::fmt(&self.inner, f)
    }
}

impl From<OclCoreError> for Error {
    fn from(err: OclCoreError) -> Error {
        Error { inner: Context::new(ErrorKind::OclCore(err)) }
    }
}

impl<T> From<SendError<T>> for Error {
    fn from(err: SendError<T>) -> Error {
        let debug = format!("{:?}", err);
        let display = format!("{}", err);
        Error { inner: Context::new(ErrorKind::FuturesMpscSend(
            format!("{}: '{}'", debug, display))) }
    }
}

// impl<T> From<StdMpscSendError<T>> for Error {
//     fn from(err: StdMpscSendError<T>) -> Error {
//         let debug = format!("{:?}", err);
//         let display = format!("{}", err);
//         Error { inner: Context::new(ErrorKind::StdMpscSend(
//             format!("{}: '{}'", debug, display))) }
//     }
// }

// impl From<StdMpscRecvError> for Error {
//     fn from(err: StdMpscRecvError) -> Error {
//         Error { inner: Context::new(ErrorKind::StdMpscRecv(err)) }
//     }
// }

impl From<OneshotCanceled> for Error {
    fn from(err: OneshotCanceled) -> Error {
        Error { inner: Context::new(ErrorKind::OneshotCanceled(err)) }
    }
}

// TODO: Remove eventually
impl From<String> for Error {
    fn from(desc: String) -> Error {
        Error { inner: Context::new(ErrorKind::OclCore(desc.into())) }
    }
}

// TODO: Remove eventually
impl<'a> From<&'a str> for Error {
    fn from(desc: &'a str) -> Error {
        Error { inner: Context::new(ErrorKind::OclCore(desc.into())) }
    }
}

impl From<std::ffi::NulError> for Error {
    fn from(err: std::ffi::NulError) -> Error {
        Error { inner: Context::new(ErrorKind::OclCore(err.into())) }
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Error {
        Error { inner: Context::new(ErrorKind::OclCore(err.into())) }
    }
}

impl From<BufferCmdError> for Error {
    fn from(err: BufferCmdError) -> Error {
        Error { inner: Context::new(ErrorKind::BufferCmd(err)) }
    }
}

impl From<DeviceError> for Error {
    fn from(err: DeviceError) -> Error {
        Error { inner: Context::new(ErrorKind::Device(err)) }
    }
}

impl From<PlatformError> for Error {
    fn from(err: PlatformError) -> Error {
        Error { inner: Context::new(ErrorKind::Platform(err)) }
    }
}

impl From<KernelError> for Error {
    fn from(err: KernelError) -> Error {
        Error { inner: Context::new(ErrorKind::Kernel(err)) }
    }
}

impl From<Error> for String {
    fn from(err: Error) -> String {
        err.to_string()
    }
}


unsafe impl Send for Error {}
unsafe impl Sync for Error {}