From 195148e51b470eb17c1c0f729de81fb23002c114 Mon Sep 17 00:00:00 2001 From: Dabo Ross Date: Fri, 19 Jun 2015 23:39:42 -0700 Subject: [PATCH] Remove ThrowBehavior and usage, as it is broken on 1.0-stable/1.1-beta.3. Tracking issue in rust for this issue: https://github.com/rust-lang/rust/issues/26448 --- src/lib.rs | 65 ++++------------------------------------------------ tests/lib.rs | 12 ++++------ 2 files changed, 10 insertions(+), 67 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e040003..4014136 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,9 +103,9 @@ //! at 6:8 in main (src/main.rs) //! ``` //! -//! `throw!()` can also be used in place of `throw_new!()` *if the argument provided is &str or -//! String. `throw!()` is special cased so that if you provide it a string literal rather than a -//! Result, it will throw the string literal directly. +//! `throw_new!()` differs from `throw!()` in that it takes a parameter directly to pass to a +//! `throw::Error`, rather than a `Result<>` to match on. `throw_new!()` will always return +//! directly from the function. use std::fmt; @@ -178,67 +178,12 @@ impl From for Error { } } -/// Represents a behavior of what to do when throw!() is used with a type. This is implemented to -/// start with on Result<>, &str and String. When throw!() is used, ThrowBehavior::handle_throw() -/// is called on the expr passed. E represents the type of error stored in the Error<> portion of -/// the Result. -pub trait ThrowBehavior { - /// Represents the type stored in the Ok portion of the Result. - type OkType; - - /// Turns this type into a Result in a manner appropriate with throw!(). - fn handle_throw(self) -> Result>; -} - -impl ThrowBehavior for Result where OE: Into> { - type OkType = T; - - fn handle_throw(self) -> Result> { - match self { - Ok(x) => Ok(x), - Err(e) => { - Err(e.into()) - } - } - } -} - -impl<'a, E> ThrowBehavior for &'a str where &'a str: Into { - type OkType = (); - - fn handle_throw(self) -> Result<(), Error> { - Err(Error { - points: Vec::new(), - original_error: self.into(), - }) - } -} - -impl ThrowBehavior for String where String: Into { - type OkType = (); - - fn handle_throw(self) -> Result<(), Error> { - Err(Error { - points: Vec::new(), - original_error: self.into(), - }) - } -} - #[macro_export] macro_rules! throw { ($e:expr) => ( - match $crate::ThrowBehavior::handle_throw($e) { + match $e { Ok(v) => v, - Err(mut e) => { - e.__push_point($crate::ErrorPoint { - line: line!(), - column: column!(), - module: module_path!(), - file: file!(), - }); - return Err(e); - }, + Err(e) => throw_new!(e), } ) } diff --git a/tests/lib.rs b/tests/lib.rs index 218df82..795d66b 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -3,9 +3,7 @@ extern crate throw; fn throw_static_message() -> Result<(), throw::Error<&'static str>> { - throw!("hi"); - - Ok(()) + throw_new!("hi"); } fn throw1() -> Result<(), throw::Error<()>> { @@ -51,9 +49,9 @@ fn test_multiple_throws() { let error = throw3().unwrap_err(); assert_eq!(error.original_error(), &()); assert_eq!(format!("{:?}", error), "Error: ()\ - \n\tat 21:4 in lib (tests/lib.rs)\ - \n\tat 16:4 in lib (tests/lib.rs)\ - \n\tat 12:4 in lib (tests/lib.rs)"); + \n\tat 19:4 in lib (tests/lib.rs)\ + \n\tat 14:4 in lib (tests/lib.rs)\ + \n\tat 10:4 in lib (tests/lib.rs)"); } #[test] @@ -66,5 +64,5 @@ fn test_returns_ok() { fn test_mod_throw() { let error = mod_test::throws().unwrap_err(); assert_eq!(format!("{}", error), "Error: ahhhh\ - \n\tat 38:8 in lib::mod_test (tests/lib.rs)"); + \n\tat 36:8 in lib::mod_test (tests/lib.rs)"); }