|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2019 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 | +/// 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. |
| 18 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 19 | +public protocol AccelerateBuffer { |
| 20 | + /// The buffer's element type. |
| 21 | + associatedtype Element |
| 22 | + |
| 23 | + /// The number of elements in the buffer. |
| 24 | + var count: Int { get } |
| 25 | + |
| 26 | + /// Calls a closure with a pointer to the object's contiguous storage. |
| 27 | + func withUnsafeBufferPointer<R>( |
| 28 | + _ body: (UnsafeBufferPointer<Element>) throws -> R |
| 29 | + ) rethrows -> R |
| 30 | +} |
| 31 | + |
| 32 | +/// A mutable object composed of count elements that are stored contiguously |
| 33 | +/// in memory. |
| 34 | +/// |
| 35 | +/// In practice, most types conforming to this protocol will be |
| 36 | +/// MutableCollections, but they need not be. |
| 37 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 38 | +public protocol AccelerateMutableBuffer: AccelerateBuffer { |
| 39 | + /// Calls the given closure with a pointer to the object's mutable |
| 40 | + /// contiguous storage. |
| 41 | + mutating func withUnsafeMutableBufferPointer<R>( |
| 42 | + _ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R |
| 43 | + ) rethrows -> R |
| 44 | +} |
| 45 | + |
| 46 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 47 | +public extension AccelerateBuffer where Self: Collection { |
| 48 | + func withUnsafeBufferPointer<R>( |
| 49 | + _ body: (UnsafeBufferPointer<Element>) throws -> R |
| 50 | + ) rethrows -> R { |
| 51 | + return try withContiguousStorageIfAvailable(body)! |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 56 | +extension AccelerateMutableBuffer where Self: MutableCollection { |
| 57 | + public mutating func withUnsafeMutableBufferPointer<R>( |
| 58 | + _ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R |
| 59 | + ) rethrows -> R { |
| 60 | + return try withContiguousMutableStorageIfAvailable(body)! |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 65 | +extension Array: AccelerateMutableBuffer { } |
| 66 | + |
| 67 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 68 | +extension ContiguousArray: AccelerateMutableBuffer { } |
| 69 | + |
| 70 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 71 | +extension ArraySlice: AccelerateMutableBuffer { } |
| 72 | + |
| 73 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 74 | +extension UnsafeBufferPointer: AccelerateBuffer { } |
| 75 | + |
| 76 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 77 | +extension UnsafeMutableBufferPointer: AccelerateMutableBuffer { } |
| 78 | + |
| 79 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 80 | +extension Slice: AccelerateBuffer where Base: Collection { } |
| 81 | + |
| 82 | +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) |
| 83 | +extension Slice: AccelerateMutableBuffer where Base: MutableCollection { } |
0 commit comments