forked from apple/swift-system
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `exit` as suggested in the roadmap. There are a few outstanding tasks with this API - [ ] Discuss API naming on the Swift forum. - [ ] Availablility annotation (I would like some suggestion from a reviewer as opposed to guessing it myself.) - [ ] Improve the documantations if deemed necessary. [roadmap]: apple#16
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
This source file is part of the Swift System open source project | ||
|
||
Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
*/ | ||
|
||
|
||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) | ||
import Darwin | ||
#elseif os(Linux) || os(FreeBSD) || os(Android) | ||
import Glibc | ||
#elseif os(Windows) | ||
import ucrt | ||
#else | ||
#error("Unsupported Platform") | ||
#endif | ||
|
||
public struct ExitStatus: RawRepresentable, Hashable, Codable { | ||
/// The raw exit status code for C standard library. | ||
@_alwaysEmitIntoClient | ||
public let rawValue: CInt | ||
|
||
/// Create an exit status. | ||
/// | ||
/// - Parameter rawValue: The raw exit status code for C standard library. | ||
@_alwaysEmitIntoClient | ||
public init(rawValue: CInt) { self.rawValue = rawValue } | ||
|
||
/// Success exit status. | ||
@_alwaysEmitIntoClient | ||
public static var success: Self { .init(rawValue: EXIT_SUCCESS) } | ||
|
||
/// Failure exit status. | ||
@_alwaysEmitIntoClient | ||
public static var failure: Self { .init(rawValue: EXIT_FAILURE) } | ||
} | ||
|
||
|
||
/// Terminate the current process. | ||
/// | ||
/// - Parameter status: The exit status for the finished process. | ||
@_alwaysEmitIntoClient | ||
public func exit(with status: ExitStatus) -> Never { | ||
exit(status.rawValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
This source file is part of the Swift System open source project | ||
|
||
Copyright (c) 2020 Apple Inc. and the Swift System project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
*/ | ||
|
||
import XCTest | ||
|
||
#if SYSTEM_PACKAGE | ||
import SystemPackage | ||
#else | ||
import System | ||
#endif | ||
|
||
final class ExitTest: XCTestCase { | ||
func testConstants() { | ||
XCTAssert(EXIT_SUCCESS == ExitStatus.success.rawValue) | ||
XCTAssert(EXIT_FAILURE == ExitStatus.failure.rawValue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters