mirror of
https://github.com/apple/swift-collections
synced 2026-11-22 04:20:40 +00:00
121 lines
3.3 KiB
Swift
121 lines
3.3 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 IterableIteratorProtocol_
|
|
where
|
|
Self: ~Copyable & ~Escapable,
|
|
Element_: ~Copyable
|
|
{
|
|
@inlinable
|
|
@_lifetime(copy self)
|
|
public consuming func map<T: ~Copyable>(
|
|
_ transform: @escaping (borrowing Element_) -> T
|
|
) -> BorrowingMapProducer<Self, T> {
|
|
BorrowingMapProducer(
|
|
_base: self,
|
|
transform: { v throws(Failure_) in
|
|
// FIXME: Conversion from non-throwing to throwing closure should be implicit
|
|
transform(v)
|
|
})
|
|
}
|
|
|
|
@inlinable
|
|
@_lifetime(copy self)
|
|
public consuming func map2<T: ~Copyable>( // FIXME: We can't overload on the type of error thrown.
|
|
_ transform: @escaping (borrowing Element_) throws(Failure_) -> T
|
|
) -> BorrowingMapProducer<Self, T> {
|
|
BorrowingMapProducer(_base: self, transform: transform)
|
|
}
|
|
|
|
@inlinable
|
|
@_lifetime(copy self)
|
|
public consuming func map2<T: ~Copyable, E: Error>( // FIXME: We can't overload on the type of error thrown.
|
|
_ transform: @escaping (borrowing Element_) throws(E) -> T
|
|
) -> BorrowingMapProducer<ErrorMappedIterator<Self, E>, T>
|
|
where Failure_ == Never {
|
|
BorrowingMapProducer(_base: self.mapError(), transform: transform)
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
public struct BorrowingMapProducer<
|
|
Base: IterableIteratorProtocol_ & ~Copyable & ~Escapable,
|
|
Element: ~Copyable,
|
|
>: ~Copyable, ~Escapable {
|
|
@_alwaysEmitIntoClient
|
|
public let _transform: (borrowing Base.Element_) throws(Failure) -> Element
|
|
|
|
@_alwaysEmitIntoClient
|
|
public var _it: Base
|
|
|
|
@inlinable
|
|
@_lifetime(copy _base)
|
|
internal init(
|
|
_base: consuming Base,
|
|
transform: @escaping (borrowing Base.Element_) throws(Failure) -> Element
|
|
) {
|
|
self._transform = transform
|
|
self._it = _base
|
|
}
|
|
}
|
|
|
|
// FIXME: Sendable
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension BorrowingMapProducer: Producer
|
|
where
|
|
Base: ~Copyable & ~Escapable,
|
|
Element: ~Copyable
|
|
{
|
|
public typealias Failure = Base.Failure_
|
|
|
|
@inlinable
|
|
public var underestimatedCount: Int {
|
|
0 // FIXME
|
|
}
|
|
|
|
@inlinable
|
|
@_lifetime(self: copy self)
|
|
public mutating func next() throws(Failure) -> Element? {
|
|
let span = try _it.nextSpan_(maximumCount: 1)
|
|
guard !span.isEmpty else { return nil }
|
|
return try _transform(span[unchecked: 0])
|
|
}
|
|
|
|
@inlinable
|
|
@discardableResult
|
|
@_lifetime(target: copy target)
|
|
@_lifetime(self: copy self)
|
|
public mutating func generate(
|
|
into target: inout OutputSpan<Element>
|
|
) throws(Failure) -> Bool {
|
|
var success = false
|
|
while !target.isFull {
|
|
let span = try _it.nextSpan_(maximumCount: target.freeCapacity)
|
|
guard !span.isEmpty else { break }
|
|
success = true
|
|
var i = 0
|
|
while i < span.count {
|
|
target.append(try _transform(span[unchecked: i]))
|
|
i &+= 1
|
|
}
|
|
}
|
|
return success
|
|
}
|
|
}
|
|
|
|
#endif
|