Skip to content

Commit d4e42c0

Browse files
authored
Merge pull request #20225 from phausler/inline_data_and_dataprotocol
DataProtocol and new inline Data
2 parents fbfd23e + d030354 commit d4e42c0

File tree

9 files changed

+2385
-995
lines changed

9 files changed

+2385
-995
lines changed

stdlib/public/Darwin/Foundation/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ add_swift_target_library(swiftFoundation ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES
88
Calendar.swift
99
CharacterSet.swift
1010
Codable.swift
11+
Collections+DataProtocol.swift
12+
ContiguousBytes.swift
1113
Data.swift
14+
DataProtocol.swift
15+
DispatchData+DataProtocol.swift
16+
NSData+DataProtocol.swift
17+
Pointers+DataProtocol.swift
1218
DataThunks.m
1319
Date.swift
1420
DateComponents.swift
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
//===--- DataProtocol -----------------------------------------------------===//
14+
15+
extension Array: DataProtocol where Element == UInt8 {
16+
public var regions: CollectionOfOne<Array<UInt8>> {
17+
return CollectionOfOne(self)
18+
}
19+
}
20+
21+
extension ArraySlice: DataProtocol where Element == UInt8 {
22+
public var regions: CollectionOfOne<ArraySlice<UInt8>> {
23+
return CollectionOfOne(self)
24+
}
25+
}
26+
27+
extension ContiguousArray: DataProtocol where Element == UInt8 {
28+
public var regions: CollectionOfOne<ContiguousArray<UInt8>> {
29+
return CollectionOfOne(self)
30+
}
31+
}
32+
33+
// FIXME: This currently crashes compilation in the Late Inliner.
34+
// extension CollectionOfOne : DataProtocol where Element == UInt8 {
35+
// public typealias Regions = CollectionOfOne<Data>
36+
//
37+
// public var regions: CollectionOfOne<Data> {
38+
// return CollectionOfOne<Data>(Data(self))
39+
// }
40+
// }
41+
42+
extension EmptyCollection : DataProtocol where Element == UInt8 {
43+
public var regions: EmptyCollection<Data> {
44+
return EmptyCollection<Data>()
45+
}
46+
}
47+
48+
extension Repeated: DataProtocol where Element == UInt8 {
49+
public typealias Regions = Repeated<Data>
50+
51+
public var regions: Repeated<Data> {
52+
guard self.count > 0 else { return repeatElement(Data(), count: 0) }
53+
return repeatElement(Data(CollectionOfOne(self.first!)), count: self.count)
54+
}
55+
}
56+
57+
//===--- MutableDataProtocol ----------------------------------------------===//
58+
59+
extension Array: MutableDataProtocol where Element == UInt8 { }
60+
61+
extension ContiguousArray: MutableDataProtocol where Element == UInt8 { }
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
//===--- ContiguousBytes --------------------------------------------------===//
14+
15+
/// Indicates that the conforming type is a contiguous collection of raw bytes
16+
/// whose underlying storage is directly accessible by withUnsafeBytes.
17+
public protocol ContiguousBytes {
18+
/// Calls the given closure with the contents of underlying storage.
19+
///
20+
/// - note: Calling `withUnsafeBytes` multiple times does not guarantee that
21+
/// the same buffer pointer will be passed in every time.
22+
/// - warning: The buffer argument to the body should not be stored or used
23+
/// outside of the lifetime of the call to the closure.
24+
func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R
25+
}
26+
27+
//===--- Collection Conformances ------------------------------------------===//
28+
29+
// FIXME: When possible, expand conformance to `where Element : Trivial`.
30+
extension Array : ContiguousBytes where Element == UInt8 { }
31+
32+
// FIXME: When possible, expand conformance to `where Element : Trivial`.
33+
extension ArraySlice : ContiguousBytes where Element == UInt8 { }
34+
35+
// FIXME: When possible, expand conformance to `where Element : Trivial`.
36+
extension ContiguousArray : ContiguousBytes where Element == UInt8 { }
37+
38+
//===--- Pointer Conformances ---------------------------------------------===//
39+
40+
extension UnsafeRawBufferPointer : ContiguousBytes {
41+
@inlinable
42+
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
43+
return try body(self)
44+
}
45+
}
46+
47+
extension UnsafeMutableRawBufferPointer : ContiguousBytes {
48+
@inlinable
49+
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
50+
return try body(UnsafeRawBufferPointer(self))
51+
}
52+
}
53+
54+
// FIXME: When possible, expand conformance to `where Element : Trivial`.
55+
extension UnsafeBufferPointer : ContiguousBytes where Element == UInt8 {
56+
@inlinable
57+
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
58+
return try body(UnsafeRawBufferPointer(self))
59+
}
60+
}
61+
62+
// FIXME: When possible, expand conformance to `where Element : Trivial`.
63+
extension UnsafeMutableBufferPointer : ContiguousBytes where Element == UInt8 {
64+
@inlinable
65+
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
66+
return try body(UnsafeRawBufferPointer(self))
67+
}
68+
}
69+
70+
// FIXME: When possible, expand conformance to `where Element : Trivial`.
71+
extension EmptyCollection : ContiguousBytes where Element == UInt8 {
72+
@inlinable
73+
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
74+
return try body(UnsafeRawBufferPointer(start: nil, count: 0))
75+
}
76+
}
77+
78+
// FIXME: When possible, expand conformance to `where Element : Trivial`.
79+
extension CollectionOfOne : ContiguousBytes where Element == UInt8 {
80+
@inlinable
81+
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
82+
let element = self.first!
83+
return try Swift.withUnsafeBytes(of: element) {
84+
return try body($0)
85+
}
86+
}
87+
}
88+
89+
//===--- Conditional Conformances -----------------------------------------===//
90+
91+
extension Slice : ContiguousBytes where Base : ContiguousBytes {
92+
public func withUnsafeBytes<ResultType>(_ body: (UnsafeRawBufferPointer) throws -> ResultType) rethrows -> ResultType {
93+
let offset = base.distance(from: base.startIndex, to: self.startIndex)
94+
return try base.withUnsafeBytes { ptr in
95+
let slicePtr = ptr.baseAddress?.advanced(by: offset)
96+
let sliceBuffer = UnsafeRawBufferPointer(start: slicePtr, count: self.count)
97+
return try body(sliceBuffer)
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)