mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
* Implementations for operators and async functions on AsyncSequence per SE-0298 * Add trailing new lines to AsyncSequence adopter files * Remove extra // from preambles * Transition sequence properties and sequence initializers to internal * Rename Upstream to Base * Rename "Failable" asynchronous sequences to "Throwing" * Make iteration consistent with closure lifetimes and track failure via state * Use for await syntax for async function extensions on AsyncSequence * Work-around rethrows calculations being incorrect for conformance + closure rethrowing * Update testing names to reflect Throwing versus Failable * Incorperate runAsyncAndBlock into the test function for suite calls * Remove @frozen from AsyncSequence operators * Back out reduce family functions for now due to IRGen bug * Additional corrections for throwing cases and fix up potential linux crasher * Add a upstream and throwing tests and correct flatMap's throwing behavior * Unify parameter names to count * Add a more helpful precondition on prefix * Add tests for throwing cases of non closure async functions on AsyncSequence * Correct missing count transition in dropFirst * Correct missing , in assertion * [NFC] add commentary on optimization in dropFirst on dropFirst * Disable collect tests for non macOS platforms for now * Disable all async sequence tests for non macOS platforms for now
113 lines
3.2 KiB
Swift
113 lines
3.2 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2021 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
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Swift
|
|
|
|
extension AsyncSequence {
|
|
@inlinable
|
|
public __consuming func flatMap<SegmentOfResult: AsyncSequence>(
|
|
_ transform: @escaping (Element) async throws -> SegmentOfResult
|
|
) -> AsyncThrowingFlatMapSequence<Self, SegmentOfResult> {
|
|
return AsyncThrowingFlatMapSequence(self, transform: transform)
|
|
}
|
|
}
|
|
|
|
public struct AsyncThrowingFlatMapSequence<Base: AsyncSequence, SegmentOfResult: AsyncSequence> {
|
|
@usableFromInline
|
|
let base: Base
|
|
|
|
@usableFromInline
|
|
let transform: (Base.Element) async throws -> SegmentOfResult
|
|
|
|
@usableFromInline
|
|
init(
|
|
_ base: Base,
|
|
transform: @escaping (Base.Element) async throws -> SegmentOfResult
|
|
) {
|
|
self.base = base
|
|
self.transform = transform
|
|
}
|
|
}
|
|
|
|
extension AsyncThrowingFlatMapSequence: AsyncSequence {
|
|
public typealias Element = SegmentOfResult.Element
|
|
public typealias AsyncIterator = Iterator
|
|
|
|
public struct Iterator: AsyncIteratorProtocol {
|
|
@usableFromInline
|
|
var baseIterator: Base.AsyncIterator
|
|
|
|
@usableFromInline
|
|
let transform: (Base.Element) async throws -> SegmentOfResult
|
|
|
|
@usableFromInline
|
|
var currentIterator: SegmentOfResult.AsyncIterator?
|
|
|
|
@usableFromInline
|
|
var finished = false
|
|
|
|
@usableFromInline
|
|
init(
|
|
_ baseIterator: Base.AsyncIterator,
|
|
transform: @escaping (Base.Element) async throws -> SegmentOfResult
|
|
) {
|
|
self.baseIterator = baseIterator
|
|
self.transform = transform
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func next() async throws -> SegmentOfResult.Element? {
|
|
while !finished {
|
|
if var iterator = currentIterator {
|
|
do {
|
|
guard let element = try await iterator.next() else {
|
|
currentIterator = nil
|
|
continue
|
|
}
|
|
// restore the iterator since we just mutated it with next
|
|
currentIterator = iterator
|
|
return element
|
|
} catch {
|
|
finished = true
|
|
throw error
|
|
}
|
|
} else {
|
|
guard let item = try await baseIterator.next() else {
|
|
return nil
|
|
}
|
|
let segment: SegmentOfResult
|
|
do {
|
|
segment = try await transform(item)
|
|
var iterator = segment.makeAsyncIterator()
|
|
guard let element = try await iterator.next() else {
|
|
currentIterator = nil
|
|
continue
|
|
}
|
|
currentIterator = iterator
|
|
return element
|
|
} catch {
|
|
finished = true
|
|
currentIterator = nil
|
|
throw error
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
public __consuming func makeAsyncIterator() -> Iterator {
|
|
return Iterator(base.makeAsyncIterator(), transform: transform)
|
|
}
|
|
}
|