Skip to content

Commit e91bcfc

Browse files
committed
Change from _ContiguousCollection to AccelerateBuffer.
Since we are not adding ContiguousCollection to the standard library at this point in time, we can and should use something more narrowly tailored to Accelerate's needs. In particular, Accelerate does not require almost anything from Collection; it only needs withUnsafe[Mutable]BufferPointer, count, and the Element associatedtype. So let's pare the protocol back to only those things, and give it a name scoped as well: [Mutable]AccelerateBuffer. In practice, most types people use with these API will be Collections anyway, but this enables a few useful additional use cases: - Fixed-size buffers - Retroactive conformance of SIMD types
1 parent 173e500 commit e91bcfc

File tree

4 files changed

+248
-243
lines changed

4 files changed

+248
-243
lines changed

stdlib/public/Darwin/Accelerate/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ add_swift_target_library(swiftAccelerate ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES
1010
GYB_SOURCES
1111
BNNS.swift.gyb
1212

13-
"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"
14-
1513
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
1614
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
1715
TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR WATCHOS WATCHOS_SIMULATOR

stdlib/public/Darwin/Accelerate/ContiguousCollection.swift

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// A collection that supports access to its underlying contiguous storage.
13+
/// An object composed of count elements that are stored contiguously in memory.
14+
///
15+
/// In practice, most types conforming to this protocol will be Collections,
16+
/// but they need not be--they need only have an Element type and count, and
17+
/// provide the withUnsafeBufferPointer function.
1418
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
15-
public protocol _ContiguousCollection: Collection
16-
where SubSequence: _ContiguousCollection {
17-
/// Calls a closure with a pointer to the array's contiguous storage.
19+
public protocol AccelerateBuffer {
20+
/// The buffer's element type.
21+
associatedtype Element
22+
/// The number of elements in the buffer.
23+
var count: Int { get }
24+
/// Calls a closure with a pointer to the object's contiguous storage.
1825
func withUnsafeBufferPointer<R>(
1926
_ body: (UnsafeBufferPointer<Element>) throws -> R
2027
) rethrows -> R
2128
}
2229

30+
/// A mutable object composed of count elements that are stored contiguously
31+
/// in memory.
32+
///
33+
/// In practice, most types conforming to this protocol will be
34+
/// MutableCollections, but they need not be.
2335
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
24-
public extension _ContiguousCollection {
25-
func withUnsafeBufferPointer<R>(
26-
_ body: (UnsafeBufferPointer<Element>) throws -> R
27-
) rethrows -> R {
28-
return try withContiguousStorageIfAvailable(body)!
29-
}
30-
}
31-
32-
/// A collection that supports mutable access to its underlying contiguous
33-
/// storage.
34-
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
35-
public protocol _MutableContiguousCollection: _ContiguousCollection, MutableCollection
36-
where SubSequence: _MutableContiguousCollection {
36+
public protocol MutableAccelerateBuffer: AccelerateBuffer {
3737
/// Calls the given closure with a pointer to the array's mutable contiguous
3838
/// storage.
3939
mutating func withUnsafeMutableBufferPointer<R>(
@@ -42,7 +42,16 @@ where SubSequence: _MutableContiguousCollection {
4242
}
4343

4444
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
45-
extension _MutableContiguousCollection {
45+
public extension AccelerateBuffer where Self: Collection {
46+
func withUnsafeBufferPointer<R>(
47+
_ body: (UnsafeBufferPointer<Element>) throws -> R
48+
) rethrows -> R {
49+
return try withContiguousStorageIfAvailable(body)!
50+
}
51+
}
52+
53+
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
54+
extension MutableAccelerateBuffer where Self: MutableCollection {
4655
public mutating func withUnsafeMutableBufferPointer<R>(
4756
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
4857
) rethrows -> R {
@@ -51,22 +60,22 @@ extension _MutableContiguousCollection {
5160
}
5261

5362
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
54-
extension Array: _MutableContiguousCollection { }
63+
extension Array: MutableAccelerateBuffer { }
5564

5665
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
57-
extension ContiguousArray: _MutableContiguousCollection { }
66+
extension ContiguousArray: MutableAccelerateBuffer { }
5867

5968
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
60-
extension ArraySlice: _MutableContiguousCollection { }
69+
extension ArraySlice: MutableAccelerateBuffer { }
6170

6271
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
63-
extension UnsafeBufferPointer: _ContiguousCollection { }
72+
extension UnsafeBufferPointer: AccelerateBuffer { }
6473

6574
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
66-
extension UnsafeMutableBufferPointer: _MutableContiguousCollection { }
75+
extension UnsafeMutableBufferPointer: MutableAccelerateBuffer { }
6776

6877
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
69-
extension Slice: _ContiguousCollection where Base: _ContiguousCollection { }
78+
extension Slice: AccelerateBuffer where Base: Collection { }
7079

7180
@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *)
72-
extension Slice: _MutableContiguousCollection where Base: _MutableContiguousCollection { }
81+
extension Slice: MutableAccelerateBuffer where Base: MutableCollection { }

stdlib/public/Darwin/Accelerate/Quadrature.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import Accelerate
14-
1513
// MARK: Quadrature
1614

1715
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)

0 commit comments

Comments
 (0)