@@ -1080,15 +1080,58 @@ impl fmt::Display for ExitStatus {
1080
1080
}
1081
1081
}
1082
1082
1083
- /// This is ridiculously unstable, as it's a completely-punted-upon part
1084
- /// of the `?`-in-`main` RFC. It's here only to allow experimenting with
1085
- /// returning a code directly from main. It will definitely change
1086
- /// drastically before being stabilized, if it doesn't just get deleted.
1087
- #[ doc( hidden) ]
1083
+ /// This type represents the status code a process can return to its
1084
+ /// parent under normal termination.
1085
+ ///
1086
+ /// Numeric values used in this type don't have portable meanings, and
1087
+ /// different platforms may mask different amounts of them.
1088
+ ///
1089
+ /// For the platform's canonical successful and unsuccessful codes, see
1090
+ /// the [`SUCCESS`] and [`FAILURE`] associated items.
1091
+ ///
1092
+ /// [`SUCCESS`]: #constant.SUCCESS
1093
+ /// [`FAILURE`]: #constant.FAILURE
1094
+ ///
1095
+ /// **Warning**: While various forms of this were discussed in [RFC #1937],
1096
+ /// it was ultimately cut from that RFC, and thus this type is more subject
1097
+ /// to change even than the usual unstable item churn.
1098
+ ///
1099
+ /// [RFC #1937]: https://github.com/rust-lang/rfcs/pull/1937
1088
1100
#[ derive( Clone , Copy , Debug ) ]
1089
1101
#[ unstable( feature = "process_exitcode_placeholder" , issue = "43301" ) ]
1090
1102
pub struct ExitCode ( pub i32 ) ;
1091
1103
1104
+ #[ cfg( target_arch = "wasm32" ) ]
1105
+ mod rawexit {
1106
+ pub const SUCCESS : i32 = 0 ;
1107
+ pub const FAILURE : i32 = 1 ;
1108
+ }
1109
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
1110
+ mod rawexit {
1111
+ use libc;
1112
+ pub const SUCCESS : i32 = libc:: EXIT_SUCCESS ;
1113
+ pub const FAILURE : i32 = libc:: EXIT_FAILURE ;
1114
+ }
1115
+
1116
+ #[ unstable( feature = "process_exitcode_placeholder" , issue = "43301" ) ]
1117
+ impl ExitCode {
1118
+ /// The canonical ExitCode for successful termination on this platform.
1119
+ ///
1120
+ /// Note that a `()`-returning `main` implicitly results in a successful
1121
+ /// termination, so there's no need to return this from `main` unless
1122
+ /// you're also returning other possible codes.
1123
+ #[ unstable( feature = "process_exitcode_placeholder" , issue = "43301" ) ]
1124
+ pub const SUCCESS : ExitCode = ExitCode ( rawexit:: SUCCESS ) ;
1125
+
1126
+ /// The canonical ExitCode for unsuccessful termination on this platform.
1127
+ ///
1128
+ /// If you're only returning this and `SUCCESS` from `main`, consider
1129
+ /// instead returning `Err(_)` and `Ok(())` respectively, which will
1130
+ /// return the same codes (but will also `eprintln!` the error).
1131
+ #[ unstable( feature = "process_exitcode_placeholder" , issue = "43301" ) ]
1132
+ pub const FAILURE : ExitCode = ExitCode ( rawexit:: FAILURE ) ;
1133
+ }
1134
+
1092
1135
impl Child {
1093
1136
/// Forces the child to exit. This is equivalent to sending a
1094
1137
/// SIGKILL on unix platforms.
@@ -1401,18 +1444,6 @@ pub fn id() -> u32 {
1401
1444
:: sys:: os:: getpid ( )
1402
1445
}
1403
1446
1404
- #[ cfg( target_arch = "wasm32" ) ]
1405
- mod exit {
1406
- pub const SUCCESS : i32 = 0 ;
1407
- pub const FAILURE : i32 = 1 ;
1408
- }
1409
- #[ cfg( not( target_arch = "wasm32" ) ) ]
1410
- mod exit {
1411
- use libc;
1412
- pub const SUCCESS : i32 = libc:: EXIT_SUCCESS ;
1413
- pub const FAILURE : i32 = libc:: EXIT_FAILURE ;
1414
- }
1415
-
1416
1447
/// A trait for implementing arbitrary return types in the `main` function.
1417
1448
///
1418
1449
/// The c-main function only supports to return integers as return type.
@@ -1433,18 +1464,15 @@ pub trait Termination {
1433
1464
1434
1465
#[ unstable( feature = "termination_trait_lib" , issue = "43301" ) ]
1435
1466
impl Termination for ( ) {
1436
- fn report ( self ) -> i32 { exit :: SUCCESS }
1467
+ fn report ( self ) -> i32 { ExitCode :: SUCCESS . report ( ) }
1437
1468
}
1438
1469
1439
1470
#[ unstable( feature = "termination_trait_lib" , issue = "43301" ) ]
1440
1471
impl < E : fmt:: Debug > Termination for Result < ( ) , E > {
1441
1472
fn report ( self ) -> i32 {
1442
1473
match self {
1443
- Ok ( val) => val. report ( ) ,
1444
- Err ( err) => {
1445
- eprintln ! ( "Error: {:?}" , err) ;
1446
- exit:: FAILURE
1447
- }
1474
+ Ok ( ( ) ) => ( ) . report ( ) ,
1475
+ Err ( err) => Err :: < !, _ > ( err) . report ( ) ,
1448
1476
}
1449
1477
}
1450
1478
}
@@ -1459,7 +1487,7 @@ impl<E: fmt::Debug> Termination for Result<!, E> {
1459
1487
fn report ( self ) -> i32 {
1460
1488
let Err ( err) = self ;
1461
1489
eprintln ! ( "Error: {:?}" , err) ;
1462
- exit :: FAILURE
1490
+ ExitCode :: FAILURE . report ( )
1463
1491
}
1464
1492
}
1465
1493
0 commit comments