Skip to content

Commit

Permalink
feat/plugin: impl From<tensor::Error> for plugin::Error
Browse files Browse the repository at this point in the history
Since plugin operations rely on `SharedTensor`'s memory access and allocation
API, they need to proxy errors. This commit adds new error enum entry to
plugin::Error, and removes deprecated plugin::Error::MissingMemoryForDevice.
It also adds autoconversion from plugin::Error::SharedTensor for convenient
use of `try!`.
  • Loading branch information
alexandermorozov committed Apr 23, 2016
1 parent 926f1e3 commit 753a773
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
//! [collenchyma-nn]: https://github.com/autumnai/collenchyma-nn

pub use self::numeric_helpers::Float;
use tensor;

/// Describes numeric types and traits for a Plugin.
pub mod numeric_helpers {
Expand All @@ -39,8 +40,9 @@ pub mod numeric_helpers {
#[derive(Debug, Copy, Clone)]
/// Defines a high-level Plugin Error.
pub enum Error {
/// Failure at receiving the correct device memory from the SharedTensor.
MissingMemoryForDevice(&'static str),
/// Failure related to `SharedTensor`: use of uninitialized memory,
/// synchronization error or memory allocation failure.
SharedTensor(tensor::Error),
/// Failure at the execution of the Operation.
Operation(&'static str),
/// Failure at the Plugin.
Expand All @@ -50,7 +52,7 @@ pub enum Error {
impl ::std::fmt::Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
Error::MissingMemoryForDevice(ref err) => write!(f, "MissingMemoryForDevice error: {}", err),
Error::SharedTensor(ref err) => write!(f, "SharedTensor error: {}", err),
Error::Operation(ref err) => write!(f, "Operation error: {}", err),
Error::Plugin(ref err) => write!(f, "Plugin error: {}", err),
}
Expand All @@ -60,15 +62,15 @@ impl ::std::fmt::Display for Error {
impl ::std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::MissingMemoryForDevice(ref err) => err,
Error::SharedTensor(ref err) => err.description(),
Error::Operation(ref err) => err,
Error::Plugin(ref err) => err,
}
}

fn cause(&self) -> Option<&::std::error::Error> {
match *self {
Error::MissingMemoryForDevice(_) => None,
Error::SharedTensor(ref err) => err.cause(),
Error::Operation(_) => None,
Error::Plugin(_) => None,
}
Expand All @@ -80,3 +82,9 @@ impl From<Error> for ::error::Error {
::error::Error::Plugin(err)
}
}

impl From<tensor::Error> for Error {
fn from(err: tensor::Error) -> Error {
Error::SharedTensor(err)
}
}

0 comments on commit 753a773

Please sign in to comment.