|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2023–2025 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +private import _TestingInternals |
| 12 | + |
| 13 | +/// A type representing a test content record's `kind` field. |
| 14 | +/// |
| 15 | +/// Test content kinds are 32-bit unsigned integers and are stored as such when |
| 16 | +/// test content records are emitted at compile time. |
| 17 | +/// |
| 18 | +/// This type lets you represent a kind value as an integer literal or as a |
| 19 | +/// string literal in Swift code. In particular, when adding a conformance to |
| 20 | +/// the ``DiscoverableAsTestContent`` protocol, the protocol's |
| 21 | +/// ``DiscoverableAsTestContent/testContentKind`` property must be an instance |
| 22 | +/// of this type. |
| 23 | +/// |
| 24 | +/// For a list of reserved values, or to reserve a value for your own use, see |
| 25 | +/// `ABI/TestContent.md`. |
| 26 | +/// |
| 27 | +/// @Comment { |
| 28 | +/// This type is `@frozen` and most of its members are `@inlinable` because it |
| 29 | +/// represents the underlying `kind` field which has a fixed layout. In the |
| 30 | +/// future, we may want to use this type in test content records, but that |
| 31 | +/// will require the type be publicly visible and that `@const` is implemented |
| 32 | +/// in the compiler. |
| 33 | +/// } |
| 34 | +@_spi(Experimental) @_spi(ForToolsIntegrationOnly) |
| 35 | +@frozen public struct TestContentKind: Sendable, RawRepresentable { |
| 36 | + public var rawValue: UInt32 |
| 37 | + |
| 38 | + @inlinable public init(rawValue: UInt32) { |
| 39 | + self.rawValue = rawValue |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// MARK: - Equatable, Hashable |
| 44 | + |
| 45 | +extension TestContentKind: Equatable, Hashable { |
| 46 | + @inlinable public static func ==(lhs: Self, rhs: Self) -> Bool { |
| 47 | + lhs.rawValue == rhs.rawValue |
| 48 | + } |
| 49 | + |
| 50 | + @inlinable public func hash(into hasher: inout Hasher) { |
| 51 | + hasher.combine(rawValue) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// MARK: - Codable |
| 56 | + |
| 57 | +extension TestContentKind: Codable {} |
| 58 | + |
| 59 | +// MARK: - ExpressibleByStringLiteral, ExpressibleByIntegerLiteral |
| 60 | + |
| 61 | +extension TestContentKind: ExpressibleByStringLiteral, ExpressibleByIntegerLiteral { |
| 62 | + @inlinable public init(stringLiteral stringValue: StaticString) { |
| 63 | + precondition(stringValue.utf8CodeUnitCount == MemoryLayout<UInt32>.stride, #""\#(stringValue)".utf8CodeUnitCount = \#(stringValue.utf8CodeUnitCount), expected \#(MemoryLayout<UInt32>.stride)"#) |
| 64 | + let rawValue = stringValue.withUTF8Buffer { stringValue in |
| 65 | + let bigEndian = UnsafeRawBufferPointer(stringValue).loadUnaligned(as: UInt32.self) |
| 66 | + return UInt32(bigEndian: bigEndian) |
| 67 | + } |
| 68 | + self.init(rawValue: rawValue) |
| 69 | + } |
| 70 | + |
| 71 | + @inlinable public init(integerLiteral: UInt32) { |
| 72 | + self.init(rawValue: integerLiteral) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// MARK: - CustomStringConvertible |
| 77 | + |
| 78 | +extension TestContentKind: CustomStringConvertible { |
| 79 | + /// This test content type's kind value as an ASCII string (of the form |
| 80 | + /// `"abcd"`) if it looks like it might be a [FourCC](https://en.wikipedia.org/wiki/FourCC) |
| 81 | + /// value, or `nil` if not. |
| 82 | + private var _fourCCValue: String? { |
| 83 | + withUnsafeBytes(of: rawValue.bigEndian) { bytes in |
| 84 | + if bytes.allSatisfy(Unicode.ASCII.isASCII) { |
| 85 | + let characters = String(decoding: bytes, as: Unicode.ASCII.self) |
| 86 | + let allAlphanumeric = characters.allSatisfy { $0.isLetter || $0.isWholeNumber } |
| 87 | + if allAlphanumeric { |
| 88 | + return characters |
| 89 | + } |
| 90 | + } |
| 91 | + return nil |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public var description: String { |
| 96 | + let hexValue = "0x" + String(rawValue, radix: 16) |
| 97 | + if let fourCCValue = _fourCCValue { |
| 98 | + return "'\(fourCCValue)' (\(hexValue))" |
| 99 | + } |
| 100 | + return hexValue |
| 101 | + } |
| 102 | +} |
0 commit comments