about summary refs log tree commit diff
path: root/src/libstd/process.rs
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2018-02-22 17:36:55 -0500
committerNiko Matsakis <niko@alum.mit.edu>2018-02-22 17:57:08 -0500
commit5f1e78f19ad40c6265a200b41c772c321b8b08cd (patch)
treecfa88d292f71b6252f9864b4ff731bcb71424e23 /src/libstd/process.rs
parente446f706a89e3d5c26c01318bd70904d492ab8b2 (diff)
downloadrust-5f1e78f19ad40c6265a200b41c772c321b8b08cd.tar.gz
rust-5f1e78f19ad40c6265a200b41c772c321b8b08cd.zip
move Termination trait to std::process
Diffstat (limited to 'src/libstd/process.rs')
-rw-r--r--src/libstd/process.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/libstd/process.rs b/src/libstd/process.rs
index 9b2f815b713..e25599b8bd8 100644
--- a/src/libstd/process.rs
+++ b/src/libstd/process.rs
@@ -1392,6 +1392,73 @@ pub fn id() -> u32 {
     ::sys::os::getpid()
 }
 
+#[cfg(target_arch = "wasm32")]
+mod exit {
+    pub const SUCCESS: i32 = 0;
+    pub const FAILURE: i32 = 1;
+}
+#[cfg(not(target_arch = "wasm32"))]
+mod exit {
+    use libc;
+    pub const SUCCESS: i32 = libc::EXIT_SUCCESS;
+    pub const FAILURE: i32 = libc::EXIT_FAILURE;
+}
+
+/// A trait for implementing arbitrary return types in the `main` function.
+///
+/// The c-main function only supports to return integers as return type.
+/// So, every type implementing the `Termination` trait has to be converted
+/// to an integer.
+///
+/// The default implementations are returning `libc::EXIT_SUCCESS` to indicate
+/// a successful execution. In case of a failure, `libc::EXIT_FAILURE` is returned.
+#[cfg_attr(not(test), lang = "termination")]
+#[unstable(feature = "termination_trait_lib", issue = "43301")]
+#[rustc_on_unimplemented =
+  "`main` can only return types that implement {Termination}, not `{Self}`"]
+pub trait Termination {
+    /// Is called to get the representation of the value as status code.
+    /// This status code is returned to the operating system.
+    fn report(self) -> i32;
+}
+
+#[unstable(feature = "termination_trait_lib", issue = "43301")]
+impl Termination for () {
+    fn report(self) -> i32 { exit::SUCCESS }
+}
+
+#[unstable(feature = "termination_trait_lib", issue = "43301")]
+impl<T: Termination, E: fmt::Debug> Termination for Result<T, E> {
+    fn report(self) -> i32 {
+        match self {
+            Ok(val) => val.report(),
+            Err(err) => {
+                eprintln!("Error: {:?}", err);
+                exit::FAILURE
+            }
+        }
+    }
+}
+
+#[unstable(feature = "termination_trait_lib", issue = "43301")]
+impl Termination for ! {
+    fn report(self) -> i32 { unreachable!(); }
+}
+
+#[unstable(feature = "termination_trait_lib", issue = "43301")]
+impl Termination for bool {
+    fn report(self) -> i32 {
+        if self { exit::SUCCESS } else { exit::FAILURE }
+    }
+}
+
+#[unstable(feature = "termination_trait_lib", issue = "43301")]
+impl Termination for i32 {
+    fn report(self) -> i32 {
+        self
+    }
+}
+
 #[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten"))))]
 mod tests {
     use io::prelude::*;