Skip to content

Commit

Permalink
move Termination trait to std::process
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Feb 22, 2018
1 parent e446f70 commit 5f1e78f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 89 deletions.
4 changes: 0 additions & 4 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,6 @@ mod memchr;
// compiler
pub mod rt;

// The trait to support returning arbitrary types in the main function
#[unstable(feature = "termination_trait", issue = "43301")]
pub mod termination;

// Include a number of private modules that exist solely to provide
// the rustdoc documentation for primitive types. Using `include!`
// because rustdoc only looks for these modules at the crate level.
Expand Down
67 changes: 67 additions & 0 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn lang_start_internal(main: &(Fn() -> i32 + Sync + ::panic::RefUnwindSafe),

#[cfg(not(test))]
#[lang = "start"]
fn lang_start<T: ::termination::Termination + 'static>
fn lang_start<T: ::process::Termination + 'static>
(main: fn() -> T, argc: isize, argv: *const *const u8) -> isize
{
lang_start_internal(&move || main().report(), argc, argv)
Expand Down
81 changes: 0 additions & 81 deletions src/libstd/termination.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/libtest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ use std::io::prelude::*;
use std::io;
use std::iter::repeat;
use std::path::PathBuf;
use std::process::Termination;
use std::sync::mpsc::{channel, Sender};
use std::sync::{Arc, Mutex};
use std::termination::Termination;
use std::thread;
use std::time::{Instant, Duration};
use std::borrow::Cow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
#![feature(termination_trait)]

fn main() -> char {
//~^ ERROR: the trait bound `char: std::termination::Termination` is not satisfied
//~^ ERROR: the trait bound `char: std::process::Termination` is not satisfied
' '
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

struct ReturnType {}

fn main() -> ReturnType { //~ ERROR `ReturnType: std::termination::Termination` is not satisfied
fn main() -> ReturnType { //~ ERROR `ReturnType: std::process::Termination` is not satisfied
ReturnType {}
}

0 comments on commit 5f1e78f

Please sign in to comment.