mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
[ContainersPreview] Add a BorrowingIteratorProtocol.copy() algorithm to turn borrowing iterators into producers
This commit is contained in:
2 files changed
+67
No files matched your search
@@ -0,0 +1,63 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 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
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension BorrowingIteratorProtocol
|
||||
where
|
||||
Self: ~Copyable & ~Escapable,
|
||||
Element: Copyable
|
||||
{
|
||||
/// Turns a borrowing iterator into a producer by copying its elements.
|
||||
/// This allows producer algorithms (such as `collect(into:)`) to get
|
||||
/// invoked on borrowing iterators, while making it explicit that contents
|
||||
/// will get copied.
|
||||
@inlinable
|
||||
@_lifetime(copy self)
|
||||
public consuming func copy() -> BorrowingMapProducer<Self, Element, Never> {
|
||||
// FIXME: We could also just define a direct implementation that avoids the closure.
|
||||
BorrowingMapProducer(_base: self, transform: { $0 })
|
||||
}
|
||||
|
||||
// Note: We could also define `collect(into:)` directly on borrowing iterators,
|
||||
// like in the commented out example below.
|
||||
//
|
||||
// However, I think that would be counterproductive to our goal of predictable
|
||||
// performance, as it would make the copies implicit. Compare and contrast:
|
||||
//
|
||||
// items.borrow().collect(into: UniqueArray.self)
|
||||
//
|
||||
// items.borrow().copy().collect(into: UniqueArray.self)
|
||||
//
|
||||
// The latter is more verbose, but it is extremely clear that it is copying
|
||||
// elements -- it avoids sweeping the cost of that under the carpet.
|
||||
#if false // See above
|
||||
@inlinable
|
||||
public consuming func collect<
|
||||
R: DynamicContainer<Element> & ~Copyable
|
||||
>(
|
||||
into container: R.Type = R.self
|
||||
) -> R {
|
||||
var result = R()
|
||||
while true {
|
||||
let span = self.nextSpan()
|
||||
guard !span.isEmpty else { break }
|
||||
result.append(copying: span)
|
||||
}
|
||||
return result
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -202,6 +202,7 @@
|
||||
7DB257A32F58B1D3007B539F /* UniqueDictionary+Debugging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB257A22F58B1CC007B539F /* UniqueDictionary+Debugging.swift */; };
|
||||
7DC85F8D2F3FDC91004A4E3D /* String+Padding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC85F8C2F3FDC82004A4E3D /* String+Padding.swift */; };
|
||||
7DC85F8F2F43D065004A4E3D /* RigidDictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC85F8E2F43D05F004A4E3D /* RigidDictionaryTests.swift */; };
|
||||
7DD99BC12F68D49F00E2B373 /* BorrowingIteratorProtocol+Copy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DD99BC02F68D48500E2B373 /* BorrowingIteratorProtocol+Copy.swift */; };
|
||||
7DE448452F2D3705005853A7 /* BinaryInteger extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE448442F2D36F9005853A7 /* BinaryInteger extensions.swift */; };
|
||||
7DE91B2F29CA6721004483EB /* Collections.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7DE91B2629CA6721004483EB /* Collections.framework */; };
|
||||
7DE9200D29CA70F3004483EB /* OrderedDictionary+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE91E7529CA70F3004483EB /* OrderedDictionary+Equatable.swift */; };
|
||||
@@ -798,6 +799,7 @@
|
||||
7DB257A22F58B1CC007B539F /* UniqueDictionary+Debugging.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Debugging.swift"; sourceTree = "<group>"; };
|
||||
7DC85F8C2F3FDC82004A4E3D /* String+Padding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Padding.swift"; sourceTree = "<group>"; };
|
||||
7DC85F8E2F43D05F004A4E3D /* RigidDictionaryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RigidDictionaryTests.swift; sourceTree = "<group>"; };
|
||||
7DD99BC02F68D48500E2B373 /* BorrowingIteratorProtocol+Copy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BorrowingIteratorProtocol+Copy.swift"; sourceTree = "<group>"; };
|
||||
7DE448442F2D36F9005853A7 /* BinaryInteger extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BinaryInteger extensions.swift"; sourceTree = "<group>"; };
|
||||
7DE448462F2D3940005853A7 /* BitCollections reexports.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BitCollections reexports.swift"; sourceTree = "<group>"; };
|
||||
7DE448472F2D3984005853A7 /* DequeModule reexports.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DequeModule reexports.swift"; sourceTree = "<group>"; };
|
||||
@@ -1246,6 +1248,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
7D2A772A2F27F1E4005780C7 /* BorrowingIteratorProtocol.swift */,
|
||||
7DD99BC02F68D48500E2B373 /* BorrowingIteratorProtocol+Copy.swift */,
|
||||
7D57EDC12F647DB10029D5E3 /* BorrowingIteratorProtocol+ElementsEqual.swift */,
|
||||
7D57EDE82F64996D0029D5E3 /* BorrowingIteratorProtocol+Filter.swift */,
|
||||
7D57EDC22F647DB10029D5E3 /* BorrowingIteratorProtocol+Map.swift */,
|
||||
@@ -2945,6 +2948,7 @@
|
||||
7DE9202129CA70F3004483EB /* OrderedSet+Partial SetAlgebra isEqualSet.swift in Sources */,
|
||||
7D479CE32F47D63900A8CB59 /* UniqueDictionary+Insertions.swift in Sources */,
|
||||
7DE9206029CA70F4004483EB /* BigString+Summary.swift in Sources */,
|
||||
7DD99BC12F68D49F00E2B373 /* BorrowingIteratorProtocol+Copy.swift in Sources */,
|
||||
7DE9203829CA70F3004483EB /* OrderedSet+Partial SetAlgebra formIntersection.swift in Sources */,
|
||||
7DE9207629CA70F4004483EB /* BigString+Initializers.swift in Sources */,
|
||||
7DE9206129CA70F4004483EB /* BigString.swift in Sources */,
|
||||
|
||||
Reference in new issue
Block a user