Skip to content

Commit

Permalink
Remove ThrowBehavior and usage, as it is broken on 1.0-stable/1.1-bet…
Browse files Browse the repository at this point in the history
…a.3. Tracking issue in rust for this issue: rust-lang/rust#26448
  • Loading branch information
daboross committed Jun 20, 2015
1 parent 8178993 commit 195148e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 67 deletions.
65 changes: 5 additions & 60 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -178,67 +178,12 @@ impl <E> From<E> for Error<E> {
}
}

/// 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<E> {
/// 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<Self::OkType, Error<E>>;
}

impl<T, OE, NE> ThrowBehavior<NE> for Result<T, OE> where OE: Into<Error<NE>> {
type OkType = T;

fn handle_throw(self) -> Result<T, Error<NE>> {
match self {
Ok(x) => Ok(x),
Err(e) => {
Err(e.into())
}
}
}
}

impl<'a, E> ThrowBehavior<E> for &'a str where &'a str: Into<E> {
type OkType = ();

fn handle_throw(self) -> Result<(), Error<E>> {
Err(Error {
points: Vec::new(),
original_error: self.into(),
})
}
}

impl<E> ThrowBehavior<E> for String where String: Into<E> {
type OkType = ();

fn handle_throw(self) -> Result<(), Error<E>> {
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),
}
)
}
Expand Down
12 changes: 5 additions & 7 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>> {
Expand Down Expand Up @@ -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]
Expand All @@ -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)");
}

0 comments on commit 195148e

Please sign in to comment.