From 8e0014d5079fc1b5d0036965b8fa452038a79e11 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 16 May 2016 23:51:13 +0200 Subject: [PATCH 1/3] Add IntoError trait to convert Error kinds into Error instances --- libimagerror/src/into.rs | 14 ++++++++++++++ libimagerror/src/lib.rs | 1 + 2 files changed, 15 insertions(+) create mode 100644 libimagerror/src/into.rs diff --git a/libimagerror/src/into.rs b/libimagerror/src/into.rs new file mode 100644 index 00000000..a959abd4 --- /dev/null +++ b/libimagerror/src/into.rs @@ -0,0 +1,14 @@ +use std::error::Error; + +/// Trait to help converting Error kinds into Error instances +pub trait IntoError { + type Target: Error; + + /// Convert the type into an error with no cause + fn into_error(self) -> Self::Target; + + /// Convert the type into an error with cause + fn into_error_with_cause(self, cause: Box) -> Self::Target; + +} + diff --git a/libimagerror/src/lib.rs b/libimagerror/src/lib.rs index d7f374a7..1d1f75e2 100644 --- a/libimagerror/src/lib.rs +++ b/libimagerror/src/lib.rs @@ -1,5 +1,6 @@ #[macro_use] extern crate log; extern crate ansi_term; +pub mod into; pub mod error_gen; pub mod trace; From 3845f28dad3e94166dbcc4658f14e6465c558d5a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 16 May 2016 23:51:26 +0200 Subject: [PATCH 2/3] Implement IntoError for all generated Errors automatically --- libimagerror/src/error_gen.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libimagerror/src/error_gen.rs b/libimagerror/src/error_gen.rs index 2f22078a..f54334e4 100644 --- a/libimagerror/src/error_gen.rs +++ b/libimagerror/src/error_gen.rs @@ -1,3 +1,5 @@ +use into::IntoError; + #[macro_export] macro_rules! generate_error_imports { () => { @@ -41,6 +43,19 @@ macro_rules! generate_error_types { } + impl IntoError for $kindname { + type Target = $name; + + fn into_error(self) -> Self::Target { + $name::new(self, None) + } + + fn into_error_with_cause(self, cause: Box) -> Self::Target { + $name::new(self, Some(cause)) + } + + } + #[derive(Debug)] pub struct $name { err_type: $kindname, From 972d313ed545df6ce1d8f8f2d949791b9da70903 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 20 May 2016 17:11:51 +0200 Subject: [PATCH 3/3] Import IntoError trait --- libimagerror/src/error_gen.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libimagerror/src/error_gen.rs b/libimagerror/src/error_gen.rs index f54334e4..e88da514 100644 --- a/libimagerror/src/error_gen.rs +++ b/libimagerror/src/error_gen.rs @@ -6,6 +6,8 @@ macro_rules! generate_error_imports { use std::error::Error; use std::fmt::Error as FmtError; use std::fmt::{Display, Formatter}; + + use $crate::into::IntoError; } }