Skip to content

Commit 5f1e78f

Browse files
committed
move Termination trait to std::process
1 parent e446f70 commit 5f1e78f

File tree

7 files changed

+71
-89
lines changed

7 files changed

+71
-89
lines changed

src/libstd/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,6 @@ mod memchr;
502502
// compiler
503503
pub mod rt;
504504

505-
// The trait to support returning arbitrary types in the main function
506-
#[unstable(feature = "termination_trait", issue = "43301")]
507-
pub mod termination;
508-
509505
// Include a number of private modules that exist solely to provide
510506
// the rustdoc documentation for primitive types. Using `include!`
511507
// because rustdoc only looks for these modules at the crate level.

src/libstd/process.rs

+67
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,73 @@ pub fn id() -> u32 {
13921392
::sys::os::getpid()
13931393
}
13941394

1395+
#[cfg(target_arch = "wasm32")]
1396+
mod exit {
1397+
pub const SUCCESS: i32 = 0;
1398+
pub const FAILURE: i32 = 1;
1399+
}
1400+
#[cfg(not(target_arch = "wasm32"))]
1401+
mod exit {
1402+
use libc;
1403+
pub const SUCCESS: i32 = libc::EXIT_SUCCESS;
1404+
pub const FAILURE: i32 = libc::EXIT_FAILURE;
1405+
}
1406+
1407+
/// A trait for implementing arbitrary return types in the `main` function.
1408+
///
1409+
/// The c-main function only supports to return integers as return type.
1410+
/// So, every type implementing the `Termination` trait has to be converted
1411+
/// to an integer.
1412+
///
1413+
/// The default implementations are returning `libc::EXIT_SUCCESS` to indicate
1414+
/// a successful execution. In case of a failure, `libc::EXIT_FAILURE` is returned.
1415+
#[cfg_attr(not(test), lang = "termination")]
1416+
#[unstable(feature = "termination_trait_lib", issue = "43301")]
1417+
#[rustc_on_unimplemented =
1418+
"`main` can only return types that implement {Termination}, not `{Self}`"]
1419+
pub trait Termination {
1420+
/// Is called to get the representation of the value as status code.
1421+
/// This status code is returned to the operating system.
1422+
fn report(self) -> i32;
1423+
}
1424+
1425+
#[unstable(feature = "termination_trait_lib", issue = "43301")]
1426+
impl Termination for () {
1427+
fn report(self) -> i32 { exit::SUCCESS }
1428+
}
1429+
1430+
#[unstable(feature = "termination_trait_lib", issue = "43301")]
1431+
impl<T: Termination, E: fmt::Debug> Termination for Result<T, E> {
1432+
fn report(self) -> i32 {
1433+
match self {
1434+
Ok(val) => val.report(),
1435+
Err(err) => {
1436+
eprintln!("Error: {:?}", err);
1437+
exit::FAILURE
1438+
}
1439+
}
1440+
}
1441+
}
1442+
1443+
#[unstable(feature = "termination_trait_lib", issue = "43301")]
1444+
impl Termination for ! {
1445+
fn report(self) -> i32 { unreachable!(); }
1446+
}
1447+
1448+
#[unstable(feature = "termination_trait_lib", issue = "43301")]
1449+
impl Termination for bool {
1450+
fn report(self) -> i32 {
1451+
if self { exit::SUCCESS } else { exit::FAILURE }
1452+
}
1453+
}
1454+
1455+
#[unstable(feature = "termination_trait_lib", issue = "43301")]
1456+
impl Termination for i32 {
1457+
fn report(self) -> i32 {
1458+
self
1459+
}
1460+
}
1461+
13951462
#[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten"))))]
13961463
mod tests {
13971464
use io::prelude::*;

src/libstd/rt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn lang_start_internal(main: &(Fn() -> i32 + Sync + ::panic::RefUnwindSafe),
6868

6969
#[cfg(not(test))]
7070
#[lang = "start"]
71-
fn lang_start<T: ::termination::Termination + 'static>
71+
fn lang_start<T: ::process::Termination + 'static>
7272
(main: fn() -> T, argc: isize, argv: *const *const u8) -> isize
7373
{
7474
lang_start_internal(&move || main().report(), argc, argv)

src/libstd/termination.rs

-81
This file was deleted.

src/libtest/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ use std::io::prelude::*;
6868
use std::io;
6969
use std::iter::repeat;
7070
use std::path::PathBuf;
71+
use std::process::Termination;
7172
use std::sync::mpsc::{channel, Sender};
7273
use std::sync::{Arc, Mutex};
73-
use std::termination::Termination;
7474
use std::thread;
7575
use std::time::{Instant, Duration};
7676
use std::borrow::Cow;

src/test/compile-fail/rfc-1937-termination-trait/termination-trait-main-wrong-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
#![feature(termination_trait)]
1111

1212
fn main() -> char {
13-
//~^ ERROR: the trait bound `char: std::termination::Termination` is not satisfied
13+
//~^ ERROR: the trait bound `char: std::process::Termination` is not satisfied
1414
' '
1515
}

src/test/compile-fail/rfc-1937-termination-trait/termination-trait-not-satisfied.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
struct ReturnType {}
1414

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

0 commit comments

Comments
 (0)