Skip to content

Commit

Permalink
simplify custos Result type, minimum Rust version is now 1.81
Browse files Browse the repository at this point in the history
  • Loading branch information
elftausend committed Sep 8, 2024
1 parent 3c4a421 commit 153b9b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repository = "https://github.com/elftausend/custos"
keywords = ["gpu", "autodiff", "arrays", "deep-learning", "fixed-size"]
categories = ["science", "mathematics", "no-std", "external-ffi-bindings"]
readme = "README.md"
rust-version = "1.79"
rust-version = "1.81"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down Expand Up @@ -53,6 +53,7 @@ min-cl = { git = "https://github.com/elftausend/min-cl", optional = true }
[features]
# default = ["cpu", "opencl", "cuda", "blas", "static-api", "stack", "macro", "nnapi", "untyped", "autograd", "autograd", "cached", "lazy", "fork", "graph", "serde"]
default = ["cpu", "untyped", "lazy", "graph", "cached", "autograd", "cuda", "vulkan", "opencl"]
# default = ["no-std"]
# default = ["opencl"]
# default = ["untyped", "cpu", "lazy", "graph", "autograd", "fork", "serde", "json", "half", "cached", "static-api", "stack", "opencl", "nnapi"]

Expand Down
48 changes: 20 additions & 28 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
/// A type alias for Box<dyn core::error::Error + Send + Sync>
#[cfg(feature = "std")]
mod std_err {
/// A type alias for Box<dyn std::error::Error + Send + Sync>
pub type Error = Box<dyn std::error::Error + Send + Sync>;

/// A trait for downcasting errors.
pub trait ErrorKind {
/// Downcasts the error to the specified type.
fn kind<E: std::error::Error + PartialEq + 'static>(&self) -> Option<&E>;
}

impl ErrorKind for Error {
fn kind<E: std::error::Error + PartialEq + 'static>(&self) -> Option<&E> {
self.downcast_ref::<E>()
}
}
pub type Error = Box<dyn core::error::Error + Send + Sync>;
#[cfg(not(feature = "std"))]
pub type Error = DeviceError;

impl std::error::Error for crate::DeviceError {}
/// A trait for downcasting errors.
pub trait ErrorKind {
/// Downcasts the error to the specified type.
fn kind<E: core::error::Error + PartialEq + 'static>(&self) -> Option<&E>;
}

#[cfg(feature = "std")]
pub use std_err::*;
impl ErrorKind for Error {
fn kind<E: core::error::Error + PartialEq + 'static>(&self) -> Option<&E> {
#[cfg(feature = "std")]
let err = self;

/// A type alias for `Result<T, Error>`.
#[cfg(feature = "std")]
pub type Result<T> = core::result::Result<T, self::std_err::Error>;

/// An error for no-std.
#[cfg(not(feature = "std"))]
#[derive(Debug)]
pub struct Error {}
#[cfg(not(feature = "std"))]
let err: &dyn core::error::Error = self;
err.downcast_ref::<E>()
}
}

/// A type alias for `Result<T, Error>`.
#[cfg(not(feature = "std"))]
pub type Result<T> = core::result::Result<T, DeviceError>;
pub type Result<T> = core::result::Result<T, Error>;

/// 'generic' device errors that can occur on any device.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down Expand Up @@ -65,6 +55,8 @@ pub enum DeviceError {
ShapeLengthMismatch,
}

impl core::error::Error for crate::DeviceError {}

impl DeviceError {
/// Returns a string slice containing the error message.
pub fn as_str(&self) -> &'static str {
Expand Down

0 comments on commit 153b9b2

Please sign in to comment.