Files
swift-collections/Sources/ContainersPreview/Protocols/BorrowingIteratorProtocol+Copy.swift
T
Franz Busch 3c373b900c Use the defines from traits directly
Package traits already pass defines with their trait name when building. Instead of adding our own conditional defines based on the traits we can use the trait provided defines directly. I observed that various tooling behaves better with the trait provided defines.
2026-05-06 07:41:02 -07:00

64 lines
2.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// 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.4) && UnstableContainersPreview
@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