From 2904f9ead35bb4abea8175b1902caa55e3ac4832 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 5 Feb 2021 19:35:18 -0800 Subject: [PATCH] Add symbolic names for standard file descriptors --- Sources/System/FileDescriptor.swift | 17 ++++++++++++++++- Tests/SystemTests/FileTypesTest.swift | 8 +++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Sources/System/FileDescriptor.swift b/Sources/System/FileDescriptor.swift index 031a6ab..8659c3f 100644 --- a/Sources/System/FileDescriptor.swift +++ b/Sources/System/FileDescriptor.swift @@ -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 @@ -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. diff --git a/Tests/SystemTests/FileTypesTest.swift b/Tests/SystemTests/FileTypesTest.swift index f6dc7f0..26eceb4 100644 --- a/Tests/SystemTests/FileTypesTest.swift +++ b/Tests/SystemTests/FileTypesTest.swift @@ -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 @@ -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)