mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
Iterable → BorrowingSequence, BorrowIterator → BorrowingIterator
This commit is contained in:
24 files changed
+287
-187
No files matched your search
@@ -0,0 +1,111 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2024 - 2026 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
#endif
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@_alwaysEmitIntoClient
|
||||
internal func withTemporaryOutputSpan<Element: ~Copyable, E: Error, R: ~Copyable>(
|
||||
of type: Element.Type,
|
||||
capacity: Int,
|
||||
_ body: (inout OutputSpan<Element>) throws(E) -> R
|
||||
) throws(E) -> R {
|
||||
try _withUnsafeTemporaryAllocation(
|
||||
of: Element.self, capacity: capacity
|
||||
) { buffer throws(E) in
|
||||
var span = OutputSpan(buffer: buffer, initializedCount: 0)
|
||||
return try body(&span)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension OutputSpan where Element: ~Copyable {
|
||||
/// Removes and returns the last element of this output span, if it exists.
|
||||
///
|
||||
/// - Returns: The last element of the original span if it wasn't empty;
|
||||
/// otherwise, `nil`.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable
|
||||
internal mutating func _popLast() -> Element? {
|
||||
// FIXME: This needs to be in the stdlib.
|
||||
withUnsafeMutableBufferPointer { buffer, count in
|
||||
guard count > 0 else { return nil }
|
||||
count &-= 1
|
||||
return buffer.moveElement(from: count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension OutputSpan where Element: ~Copyable {
|
||||
@_lifetime(source: copy source)
|
||||
@inlinable
|
||||
package mutating func _append(moving source: inout InputSpan<Element>) {
|
||||
// FIXME: This needs to be in the stdlib.
|
||||
self.withUnsafeMutableBufferPointer { dst, dstCount in
|
||||
source.withUnsafeMutableBufferPointer { src, srcCount in
|
||||
let dstEnd = dstCount + srcCount
|
||||
let srcStart = src.count - srcCount
|
||||
precondition(dstEnd <= dst.count, "OutputSpan capacity overflow")
|
||||
let srcItems = src._extracting(uncheckedFrom: srcStart, to: src.count)
|
||||
dst
|
||||
._extracting(uncheckedFrom: dstCount, to: dstEnd)
|
||||
.moveInitializeAll(fromContentsOf: srcItems)
|
||||
dstCount &+= srcCount
|
||||
srcCount = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_lifetime(source: copy source)
|
||||
@inlinable
|
||||
package mutating func _append(moving source: inout OutputSpan<Element>) {
|
||||
// FIXME: This needs to be in the stdlib.
|
||||
self.withUnsafeMutableBufferPointer { dst, dstCount in
|
||||
source.withUnsafeMutableBufferPointer { src, srcCount in
|
||||
let dstEnd = dstCount + srcCount
|
||||
precondition(dstEnd <= dst.count, "OutputSpan capacity overflow")
|
||||
let srcItems = src._extracting(uncheckedFrom: 0, to: srcCount)
|
||||
dst
|
||||
._extracting(uncheckedFrom: dstCount, to: dstEnd)
|
||||
.moveInitializeAll(fromContentsOf: srcItems)
|
||||
dstCount &+= srcCount
|
||||
srcCount = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension OutputSpan /* where Element: Copyable */ {
|
||||
@inlinable
|
||||
package mutating func _append(copying source: borrowing Span<Element>) {
|
||||
// FIXME: This needs to be in the stdlib.
|
||||
self.withUnsafeMutableBufferPointer { dst, dstCount in
|
||||
source.withUnsafeBufferPointer { src in
|
||||
let dstEnd = dstCount + src.count
|
||||
precondition(dstEnd <= dst.count, "OutputSpan capacity overflow")
|
||||
dst
|
||||
._extracting(uncheckedFrom: dstCount, to: dstEnd)
|
||||
.initializeAll(fromContentsOf: src)
|
||||
dstCount &+= src.count
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2024 - 2026 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
|
||||
/// A polyfill of the typed-throws supporting `withUnsafeTemporaryAllocation` in the 6.3 stdlib.
|
||||
@_alwaysEmitIntoClient @_transparent
|
||||
package func _withUnsafeTemporaryAllocation<
|
||||
T: ~Copyable, R: ~Copyable,
|
||||
E: Error
|
||||
>(
|
||||
of type: T.Type,
|
||||
capacity: Int,
|
||||
_ body: (UnsafeMutableBufferPointer<T>) throws(E) -> R
|
||||
) throws(E) -> R {
|
||||
#if compiler(>=6.3)
|
||||
try withUnsafeTemporaryAllocation(of: type, capacity: capacity, body)
|
||||
#else
|
||||
let r: Result<R, E> = withUnsafeTemporaryAllocation(
|
||||
of: T.self, capacity: capacity
|
||||
) { buffer in
|
||||
return Result(catching: { () throws(E) in try body(buffer) })
|
||||
}
|
||||
return try r.get()
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user