Skip to content

Commit

Permalink
Move ExpressibleByArrayLiteral conformance of _TinyArray to the d…
Browse files Browse the repository at this point in the history
…eclaring module (#157)

Motivation

Nightly compilers start complaining about retroactive conformances.

Modifications

- move conformance to declaring module
- optimise code to not allocate

Result

no warning, optimal generated code for:

```swift
func foo1(int: Int) -> _TinyArray<Int> {
    [int]
}
```

compiles down to:
```
output.foo1(int: Swift.Int) -> output._TinyArray<Swift.Int>:
        mov     rax, rdi
        xor     edx, edx
        ret
```

https://godbolt.org/z/a91fPe6or
  • Loading branch information
dnadoba authored Dec 5, 2023
1 parent 12e0773 commit 66143b8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Sources/X509/CryptographicMessageSyntax/CMSSignature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//

import SwiftASN1
#if canImport(Darwin)
#if canImport(Darwin) || swift(>=5.9.1)
import Foundation
#else
@preconcurrency import Foundation
Expand Down
2 changes: 1 addition & 1 deletion Sources/X509/OCSP/OCSPPolicy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import SwiftASN1
import Crypto
#if canImport(Darwin)
#if canImport(Darwin) || swift(>=5.9.1)
import Foundation
#else
@preconcurrency import Foundation
Expand Down
14 changes: 14 additions & 0 deletions Sources/_CertificateInternals/_TinyArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ extension _TinyArray: Equatable where Element: Equatable {}
extension _TinyArray: Hashable where Element: Hashable {}
extension _TinyArray: Sendable where Element: Sendable {}

extension _TinyArray: ExpressibleByArrayLiteral {
@inlinable
public init(arrayLiteral elements: Element...) {
switch elements.count {
case 0:
self = .init()
case 1:
self = .init(CollectionOfOne(elements[0]))
default:
self = .init(elements)
}
}
}

extension _TinyArray: RandomAccessCollection {
public typealias Element = Element

Expand Down
17 changes: 9 additions & 8 deletions Tests/CertificateInternalsTests/TinyArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ final class TinyArrayTests: XCTestCase {
XCTAssertEqual(Array(_TinyArray<Int>([1, 2, 3, 4, 5])), [1, 2, 3, 4, 5])
}

func testExpressibleByArrayLiteral() {
XCTAssertEqual(Array([] as _TinyArray<Int>), [])
XCTAssertEqual(Array([1] as _TinyArray<Int>), [1])
XCTAssertEqual(Array([1, 2] as _TinyArray<Int>), [1, 2])
XCTAssertEqual(Array([1, 2, 3] as _TinyArray<Int>), [1, 2, 3])
XCTAssertEqual(Array([1, 2, 3, 4] as _TinyArray<Int>), [1, 2, 3, 4])
XCTAssertEqual(Array([1, 2, 3, 4, 5] as _TinyArray<Int>), [1, 2, 3, 4, 5])
}

func testAppend() {
assertEqual([1]) { array in
array.append(1)
Expand Down Expand Up @@ -339,11 +348,3 @@ final class TinyArrayTests: XCTestCase {
)
}
}

extension _TinyArray: ExpressibleByArrayLiteral {
public typealias ArrayLiteralElement = Element

public init(arrayLiteral elements: Element...) {
self.init(elements)
}
}
2 changes: 1 addition & 1 deletion Tests/X509Tests/OCSPPolicyVerifierTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import XCTest
import Crypto
import SwiftASN1
@testable import X509
#if canImport(Darwin)
#if canImport(Darwin) || swift(>=5.9.1)
import Foundation
#else
@preconcurrency import Foundation
Expand Down

0 comments on commit 66143b8

Please sign in to comment.