diff options
| author | Marvin Löbel <loebel.marvin@gmail.com> | 2013-04-23 22:30:58 +0200 |
|---|---|---|
| committer | Marvin Löbel <loebel.marvin@gmail.com> | 2013-04-25 17:32:25 +0200 |
| commit | e1be9ae22468e19d66daaebbceeeeaea2e75f903 (patch) | |
| tree | dc442111f15ccd656e6ea9598307f57a3bd78b73 /src/libcore/sys.rs | |
| parent | 1d53babd2f23439975518fda94d9122b15e779c9 (diff) | |
| download | rust-e1be9ae22468e19d66daaebbceeeeaea2e75f903.tar.gz rust-e1be9ae22468e19d66daaebbceeeeaea2e75f903.zip | |
Made fail! and assert! accept both &'static str and ~str, as well as a fmt! like format list.
Unwinding through macros now happens as a call to the trait function `FailWithCause::fail_with()`, which consumes self, allowing to use a more generic failure object in the future.
Diffstat (limited to 'src/libcore/sys.rs')
| -rw-r--r-- | src/libcore/sys.rs | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/src/libcore/sys.rs b/src/libcore/sys.rs index c4ec83aa176..bdc3a17308d 100644 --- a/src/libcore/sys.rs +++ b/src/libcore/sys.rs @@ -165,7 +165,42 @@ pub fn log_str<T>(t: &T) -> ~str { } } -/** Initiate task failure */ +/// Trait for initiating task failure. +pub trait FailWithCause { + /// Fail the current task, taking ownership of `cause` + fn fail_with(cause: Self, file: &'static str, line: uint) -> !; +} + +impl FailWithCause for ~str { + fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! { + do str::as_buf(cause) |msg_buf, _msg_len| { + do str::as_buf(file) |file_buf, _file_len| { + unsafe { + let msg_buf = cast::transmute(msg_buf); + let file_buf = cast::transmute(file_buf); + begin_unwind_(msg_buf, file_buf, line as libc::size_t) + } + } + } + } +} + +impl FailWithCause for &'static str { + fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! { + do str::as_buf(cause) |msg_buf, _msg_len| { + do str::as_buf(file) |file_buf, _file_len| { + unsafe { + let msg_buf = cast::transmute(msg_buf); + let file_buf = cast::transmute(file_buf); + begin_unwind_(msg_buf, file_buf, line as libc::size_t) + } + } + } + } +} + +// NOTE: remove function after snapshot +#[cfg(stage0)] pub fn begin_unwind(msg: ~str, file: ~str, line: uint) -> ! { do str::as_buf(msg) |msg_buf, _msg_len| { do str::as_buf(file) |file_buf, _file_len| { @@ -187,6 +222,8 @@ pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! { } } +// NOTE: remove function after snapshot +#[cfg(stage0)] pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! { let (msg, file) = (msg.to_owned(), file.to_owned()); begin_unwind(~"assertion failed: " + msg, file, line) @@ -297,6 +334,14 @@ mod tests { assert!(new_f(20) == 30); } } + + #[test] + #[should_fail] + fn fail_static() { FailWithCause::fail_with("cause", file!(), line!()) } + + #[test] + #[should_fail] + fn fail_owned() { FailWithCause::fail_with(~"cause", file!(), line!()) } } // Local Variables: |
