about summary refs log tree commit diff
path: root/src/libstd/sys.rs
diff options
context:
space:
mode:
authorMarvin Löbel <loebel.marvin@gmail.com>2013-10-11 23:20:34 +0200
committerMarvin Löbel <loebel.marvin@gmail.com>2013-10-28 08:50:32 +0100
commitfa8e71a8257f4226ab532d4bf268d3ecbfa98eb4 (patch)
tree0b8051814dd8a5ef08e663c172e2b456065d625d /src/libstd/sys.rs
parentcb5b21eba713ff3888b2741db4c9e7d841cfde02 (diff)
downloadrust-fa8e71a8257f4226ab532d4bf268d3ecbfa98eb4.tar.gz
rust-fa8e71a8257f4226ab532d4bf268d3ecbfa98eb4.zip
Allow fail messages to be caught, and introduce the Any trait
Some code cleanup, sorting of import blocks

Removed std::unstable::UnsafeArc's use of Either

Added run-fail tests for the new FailWithCause impls

Changed future_result and try to return Result<(), ~Any>.

- Internally, there is an enum of possible fail messages passend around.
- In case of linked failure or a string message, the ~Any gets
  lazyly allocated in future_results recv method.
- For that, future result now returns a wrapper around a Port.
- Moved and renamed task::TaskResult into rt::task::UnwindResult
  and made it an internal enum.
- Introduced a replacement typedef `type TaskResult = Result<(), ~Any>`.
Diffstat (limited to 'src/libstd/sys.rs')
-rw-r--r--src/libstd/sys.rs62
1 files changed, 44 insertions, 18 deletions
diff --git a/src/libstd/sys.rs b/src/libstd/sys.rs
index d20a6696e27..b35b25aeb6f 100644
--- a/src/libstd/sys.rs
+++ b/src/libstd/sys.rs
@@ -12,41 +12,55 @@
 
 #[allow(missing_doc)];
 
-use c_str::ToCStr;
-use libc::size_t;
-use libc;
+use any::Any;
+use kinds::Send;
+use rt::task::{UnwindReasonStr, UnwindReasonAny};
 use rt::task;
+use send_str::{SendStr, IntoSendStr};
 
-/// Trait for initiating task failure.
+/// Trait for initiating task failure with a sendable cause.
 pub trait FailWithCause {
-    /// Fail the current task, taking ownership of `cause`
+    /// Fail the current task with `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 cause.with_c_str |msg_buf| {
-            do file.with_c_str |file_buf| {
-                task::begin_unwind(msg_buf, file_buf, line as libc::size_t)
-            }
-        }
+        task::begin_unwind_reason(UnwindReasonStr(cause.into_send_str()), file, line)
     }
 }
 
 impl FailWithCause for &'static str {
     fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! {
-        do cause.with_c_str |msg_buf| {
-            do file.with_c_str |file_buf| {
-                task::begin_unwind(msg_buf, file_buf, line as libc::size_t)
-            }
-        }
+        task::begin_unwind_reason(UnwindReasonStr(cause.into_send_str()), file, line)
+    }
+}
+
+impl FailWithCause for SendStr {
+    fn fail_with(cause: SendStr, file: &'static str, line: uint) -> ! {
+        task::begin_unwind_reason(UnwindReasonStr(cause), file, line)
+    }
+}
+
+impl FailWithCause for ~Any {
+    fn fail_with(cause: ~Any, file: &'static str, line: uint) -> ! {
+        task::begin_unwind_reason(UnwindReasonAny(cause), file, line)
+    }
+}
+
+impl<T: Any + Send + 'static> FailWithCause for ~T {
+    fn fail_with(cause: ~T, file: &'static str, line: uint) -> ! {
+        task::begin_unwind_reason(UnwindReasonAny(cause as ~Any), file, line)
     }
 }
 
 #[cfg(test)]
 mod tests {
+    use super::*;
+
+    use any::Any;
     use cast;
-    use sys::*;
+    use send_str::IntoSendStr;
 
     #[test]
     fn synthesize_closure() {
@@ -74,9 +88,21 @@ mod tests {
 
     #[test]
     #[should_fail]
-    fn fail_static() { FailWithCause::fail_with("cause", file!(), line!())  }
+    fn fail_static() { FailWithCause::fail_with("cause", file!(), line!()) }
+
+    #[test]
+    #[should_fail]
+    fn fail_owned() { FailWithCause::fail_with(~"cause", file!(), line!()) }
+
+    #[test]
+    #[should_fail]
+    fn fail_send() { FailWithCause::fail_with("cause".into_send_str(), file!(), line!()) }
+
+    #[test]
+    #[should_fail]
+    fn fail_any() { FailWithCause::fail_with(~612_u16 as ~Any, file!(), line!()) }
 
     #[test]
     #[should_fail]
-    fn fail_owned() { FailWithCause::fail_with(~"cause", file!(), line!())  }
+    fn fail_any_wrap() { FailWithCause::fail_with(~413_u16, file!(), line!()) }
 }