Skip to content

Commit 68f32fc

Browse files
authored
Merge pull request #18469 from phausler/swift-4.2-branch-data_init_availability
[Foundation] handle ambiguity in swift 4 mode for Data.init with byte sequences
2 parents 44a88d4 + d530f85 commit 68f32fc

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

stdlib/public/SDK/Foundation/Data.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,11 +1249,22 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
12491249
self.init(backing: backing, range: 0..<backing._length)
12501250
}
12511251

1252+
@available(swift, introduced: 4.2)
12521253
@inlinable
12531254
public init<S: Sequence>(bytes elements: S) where S.Iterator.Element == UInt8 {
12541255
self.init(elements)
12551256
}
12561257

1258+
@available(swift, obsoleted: 4.2)
1259+
public init(bytes: Array<UInt8>) {
1260+
self.init(bytes)
1261+
}
1262+
1263+
@available(swift, obsoleted: 4.2)
1264+
public init(bytes: ArraySlice<UInt8>) {
1265+
self.init(bytes)
1266+
}
1267+
12571268
@usableFromInline
12581269
internal init(backing: _DataStorage, range: Range<Index>) {
12591270
_backing = backing

test/stdlib/TestData_Swift4.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
3+
// Licensed under Apache License v2.0 with Runtime Library Exception
4+
//
5+
// See https://swift.org/LICENSE.txt for license information
6+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// RUN: %empty-directory(%t)
11+
// RUN: %target-build-swift -swift-version 4 %s -o %t/TestData_Swift4
12+
// RUN: %target-run %t/TestData_Swift4
13+
// REQUIRES: executable_test
14+
// REQUIRES: objc_interop
15+
16+
import Foundation
17+
import StdlibUnittest
18+
19+
var DataTests = TestSuite("TestDataSwift4")
20+
21+
DataTests.test("functional map init usage") {
22+
let res1 = [[UInt8(0), UInt8(1), UInt8(2)]].map(Data.init) // previously this could be done without being ambiguous (however in swift 4.2 an overload was added that makes it ambiguous as a function ref)
23+
// the following two strategies are preferred to the previous version
24+
let res2 = [[UInt8(0), UInt8(1), UInt8(2)]].map(Data.init(_:))
25+
let res3 = [[UInt8(0), UInt8(1), UInt8(2)]].map { Data($0) }
26+
27+
expectEqual(res1.count, 1)
28+
expectEqual(res2.count, 1)
29+
expectEqual(res3.count, 1)
30+
31+
expectEqual(res1[0], res2[0])
32+
expectEqual(res2[0], res3[0])
33+
}
34+
35+
36+
runAllTests()

0 commit comments

Comments
 (0)