Skip to content

Add symbolic names for standard file descriptors #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Sources/System/FileDescriptor.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift System open source project

Copyright (c) 2020 Apple Inc. and the Swift System project authors
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
Expand All @@ -25,6 +25,21 @@ public struct FileDescriptor: RawRepresentable, Hashable, Codable {
public init(rawValue: CInt) { self.rawValue = rawValue }
}

// Standard file descriptors
extension FileDescriptor {
/// The standard input file descriptor, with a numeric value of 0.
@_alwaysEmitIntoClient
public static var standardInput: FileDescriptor { .init(rawValue: 0) }

/// The standard output file descriptor, with a numeric value of 1.
@_alwaysEmitIntoClient
public static var standardOutput: FileDescriptor { .init(rawValue: 1) }

/// The standard error file descriptor, with a numeric value of 2.
@_alwaysEmitIntoClient
public static var standardError: FileDescriptor { .init(rawValue: 2) }
}

// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
extension FileDescriptor {
/// The desired read and write access for a newly opened file.
Expand Down
8 changes: 7 additions & 1 deletion Tests/SystemTests/FileTypesTest.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift System open source project

Copyright (c) 2020 Apple Inc. and the Swift System project authors
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
Expand All @@ -12,6 +12,12 @@ import SystemPackage

// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
final class FileDescriptorTest: XCTestCase {
func testStandardDescriptors() {
XCTAssertEqual(FileDescriptor.standardInput.rawValue, 0)
XCTAssertEqual(FileDescriptor.standardOutput.rawValue, 1)
XCTAssertEqual(FileDescriptor.standardError.rawValue, 2)
}

// Test the constants match the C header values. For various reasons,
func testConstants() {
XCTAssertEqual(O_RDONLY, FileDescriptor.AccessMode.readOnly.rawValue)
Expand Down