diff --git a/stdlib/public/core/CMakeLists.txt b/stdlib/public/core/CMakeLists.txt index 3e181ab60d403..42c41dcc156ec 100644 --- a/stdlib/public/core/CMakeLists.txt +++ b/stdlib/public/core/CMakeLists.txt @@ -44,6 +44,7 @@ set(SWIFTLIB_ESSENTIAL Comparable.swift CompilerProtocols.swift ContiguousArray.swift + ContiguousCollection.swift ContiguouslyStored.swift ClosedRange.swift ContiguousArrayBuffer.swift diff --git a/stdlib/public/core/ContiguousCollection.swift b/stdlib/public/core/ContiguousCollection.swift new file mode 100644 index 0000000000000..5acbaad46c65e --- /dev/null +++ b/stdlib/public/core/ContiguousCollection.swift @@ -0,0 +1,74 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +/// A collection that supports access to its underlying contiguous storage. +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +public protocol ContiguousCollection: Collection +where SubSequence: ContiguousCollection { + /// Calls a closure with a pointer to the array's contiguous storage. + func withUnsafeBufferPointer( + _ body: (UnsafeBufferPointer) throws -> R + ) rethrows -> R +} + +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +public extension ContiguousCollection { + @inlinable + func withUnsafeBufferPointer( + _ body: (UnsafeBufferPointer) throws -> R + ) rethrows -> R { + return try withContiguousStorageIfAvailable(body)! + } +} + +/// A collection that supports mutable access to its underlying contiguous +/// storage. +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +public protocol MutableContiguousCollection: ContiguousCollection, MutableCollection +where SubSequence: MutableContiguousCollection { + /// Calls the given closure with a pointer to the array's mutable contiguous + /// storage. + mutating func withUnsafeMutableBufferPointer( + _ body: (inout UnsafeMutableBufferPointer) throws -> R + ) rethrows -> R +} + +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +extension MutableContiguousCollection { + @inlinable + public mutating func withUnsafeMutableBufferPointer( + _ body: (inout UnsafeMutableBufferPointer) throws -> R + ) rethrows -> R { + return try withContiguousMutableStorageIfAvailable(body)! + } +} + +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +extension Array: MutableContiguousCollection { } + +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +extension ContiguousArray: MutableContiguousCollection { } + +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +extension ArraySlice: MutableContiguousCollection { } + +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +extension UnsafeBufferPointer: ContiguousCollection { } + +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +extension UnsafeMutableBufferPointer: MutableContiguousCollection { } + +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +extension Slice: ContiguousCollection where Base: ContiguousCollection { } + +@available(iOS 9999, OSX 9999, tvOS 9999, watchOS 9999, *) +extension Slice: MutableContiguousCollection where Base: MutableContiguousCollection { } diff --git a/stdlib/public/core/GroupInfo.json b/stdlib/public/core/GroupInfo.json index ca2c2ab879526..804ebf2e20b75 100644 --- a/stdlib/public/core/GroupInfo.json +++ b/stdlib/public/core/GroupInfo.json @@ -113,6 +113,7 @@ "CocoaArray.swift", "ContiguousArray.swift", "ContiguousArrayBuffer.swift", + "ContiguousCollection.swift", "ContiguouslyStored.swift", "FixedArray.swift", "SliceBuffer.swift",