mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
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.
61 lines
2.1 KiB
Swift
61 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 Producer where Self: ~Copyable & ~Escapable, Element: ~Copyable {
|
|
@inlinable
|
|
public consuming func collect<
|
|
R: DynamicContainer<Element> & ~Copyable
|
|
>(
|
|
into container: R.Type = R.self
|
|
) throws(Failure) -> R {
|
|
try R(from: self)
|
|
}
|
|
|
|
/// Collect all items generated by this producer into the provided
|
|
/// range-replaceable container instance, which must be able to accommodate
|
|
/// every new element.
|
|
@inlinable
|
|
public consuming func collect(
|
|
into container: inout some RangeReplaceableContainer<Element> & ~Copyable & ~Escapable
|
|
) throws(Failure) {
|
|
// Note: this is the same algorithm as `append(from:)` on `DynamicContainer`,
|
|
// except it applies to RRCs.
|
|
while true {
|
|
let c = Swift.max(self.underestimatedCount, container.freeCapacity)
|
|
if c > 0 {
|
|
try container.append(addingCount: c, from: &self)
|
|
}
|
|
guard let next = try self.next() else { break }
|
|
// If `container` is of a fixed-capacity type, then this will likely
|
|
// trap (as expected). On the other hand, if it is a dynamically resizing
|
|
// type, then this will reallocate its storage, setting up the bulk
|
|
// append in the next iteration.
|
|
container.append(next)
|
|
}
|
|
}
|
|
|
|
/// Collect all items generated by this producer into the provided
|
|
/// dynamic container instance, resizing it if necessary.
|
|
@inlinable
|
|
public consuming func collect(
|
|
into container: inout some DynamicContainer<Element> & ~Copyable
|
|
) throws(Failure) {
|
|
try container.append(from: self)
|
|
}
|
|
}
|
|
|
|
#endif
|