@@ -1392,6 +1392,73 @@ pub fn id() -> u32 {
1392
1392
:: sys:: os:: getpid ( )
1393
1393
}
1394
1394
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
+
1395
1462
#[ cfg( all( test, not( any( target_os = "cloudabi" , target_os = "emscripten" ) ) ) ) ]
1396
1463
mod tests {
1397
1464
use io:: prelude:: * ;
0 commit comments