Skip to content

Commit c1029b6

Browse files
authored
Merge pull request #18443 from phausler/data_init_availability
2 parents 8f0fa4b + b65faee commit c1029b6

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

stdlib/public/SDK/Foundation/Data.swift

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

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

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

test/stdlib/TestData_Swift4.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
runAllTests()

0 commit comments

Comments
 (0)