move Termination trait to std::process

This commit is contained in:
Niko Matsakis 2018-02-22 17:36:55 -05:00
parent e446f706a8
commit 5f1e78f19a
7 changed files with 71 additions and 89 deletions

View File

@ -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.

View File

@ -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::*;

View File

@ -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)

View File

@ -1,81 +0,0 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Defines the meaning of the return value from `main`, and hence
//! controls what happens in a Rust program after `main` returns.
use fmt::Debug;
#[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: 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
}
}

View File

@ -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;

View File

@ -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
' '
}

View File

@ -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 {}
}