@@ -1080,14 +1080,45 @@ 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`]: #associatedconstant.SUCCESS
1093
+ /// [`FAILURE`]: #associatedconstant.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
- #[ unstable( feature = "process_exitcode_placeholder" , issue = "43301" ) ]
1090
- pub struct ExitCode ( pub i32 ) ;
1101
+ #[ unstable( feature = "process_exitcode_placeholder" , issue = "48711" ) ]
1102
+ pub struct ExitCode ( imp:: ExitCode ) ;
1103
+
1104
+ #[ unstable( feature = "process_exitcode_placeholder" , issue = "48711" ) ]
1105
+ impl ExitCode {
1106
+ /// The canonical ExitCode for successful termination on this platform.
1107
+ ///
1108
+ /// Note that a `()`-returning `main` implicitly results in a successful
1109
+ /// termination, so there's no need to return this from `main` unless
1110
+ /// you're also returning other possible codes.
1111
+ #[ unstable( feature = "process_exitcode_placeholder" , issue = "48711" ) ]
1112
+ pub const SUCCESS : ExitCode = ExitCode ( imp:: ExitCode :: SUCCESS ) ;
1113
+
1114
+ /// The canonical ExitCode for unsuccessful termination on this platform.
1115
+ ///
1116
+ /// If you're only returning this and `SUCCESS` from `main`, consider
1117
+ /// instead returning `Err(_)` and `Ok(())` respectively, which will
1118
+ /// return the same codes (but will also `eprintln!` the error).
1119
+ #[ unstable( feature = "process_exitcode_placeholder" , issue = "48711" ) ]
1120
+ pub const FAILURE : ExitCode = ExitCode ( imp:: ExitCode :: FAILURE ) ;
1121
+ }
1091
1122
1092
1123
impl Child {
1093
1124
/// Forces the child to exit. This is equivalent to sending a
@@ -1401,18 +1432,6 @@ pub fn id() -> u32 {
1401
1432
:: sys:: os:: getpid ( )
1402
1433
}
1403
1434
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
1435
/// A trait for implementing arbitrary return types in the `main` function.
1417
1436
///
1418
1437
/// The c-main function only supports to return integers as return type.
@@ -1433,18 +1452,15 @@ pub trait Termination {
1433
1452
1434
1453
#[ unstable( feature = "termination_trait_lib" , issue = "43301" ) ]
1435
1454
impl Termination for ( ) {
1436
- fn report ( self ) -> i32 { exit :: SUCCESS }
1455
+ fn report ( self ) -> i32 { ExitCode :: SUCCESS . report ( ) }
1437
1456
}
1438
1457
1439
1458
#[ unstable( feature = "termination_trait_lib" , issue = "43301" ) ]
1440
1459
impl < E : fmt:: Debug > Termination for Result < ( ) , E > {
1441
1460
fn report ( self ) -> i32 {
1442
1461
match self {
1443
- Ok ( val) => val. report ( ) ,
1444
- Err ( err) => {
1445
- eprintln ! ( "Error: {:?}" , err) ;
1446
- exit:: FAILURE
1447
- }
1462
+ Ok ( ( ) ) => ( ) . report ( ) ,
1463
+ Err ( err) => Err :: < !, _ > ( err) . report ( ) ,
1448
1464
}
1449
1465
}
1450
1466
}
@@ -1459,15 +1475,14 @@ impl<E: fmt::Debug> Termination for Result<!, E> {
1459
1475
fn report ( self ) -> i32 {
1460
1476
let Err ( err) = self ;
1461
1477
eprintln ! ( "Error: {:?}" , err) ;
1462
- exit :: FAILURE
1478
+ ExitCode :: FAILURE . report ( )
1463
1479
}
1464
1480
}
1465
1481
1466
1482
#[ unstable( feature = "termination_trait_lib" , issue = "43301" ) ]
1467
1483
impl Termination for ExitCode {
1468
1484
fn report ( self ) -> i32 {
1469
- let ExitCode ( code) = self ;
1470
- code
1485
+ self . 0 . as_i32 ( )
1471
1486
}
1472
1487
}
1473
1488
0 commit comments