mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-24 04:17:52 +00:00
zip: remove Sendable constraint + limit task creation (#201)
This commit is contained in:
1 parent
1bd073999c
commit
76b27aebef
9 files changed
+1401
-394
No files matched your search
@@ -1,165 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// Creates an asynchronous sequence that concurrently awaits values from two `AsyncSequence` types
|
||||
/// and emits a tuple of the values.
|
||||
public func zip<Base1: AsyncSequence, Base2: AsyncSequence>(_ base1: Base1, _ base2: Base2) -> AsyncZip2Sequence<Base1, Base2>
|
||||
where Base1: Sendable,
|
||||
Base2: Sendable,
|
||||
Base1.AsyncIterator: Sendable,
|
||||
Base2.AsyncIterator: Sendable,
|
||||
Base1.Element: Sendable,
|
||||
Base2.Element: Sendable {
|
||||
AsyncZip2Sequence(base1, base2)
|
||||
}
|
||||
|
||||
/// An asynchronous sequence that concurrently awaits values from two `AsyncSequence` types
|
||||
/// and emits a tuple of the values.
|
||||
public struct AsyncZip2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Sendable
|
||||
where Base1: Sendable,
|
||||
Base2: Sendable,
|
||||
Base1.AsyncIterator: Sendable,
|
||||
Base2.AsyncIterator: Sendable,
|
||||
Base1.Element: Sendable,
|
||||
Base2.Element: Sendable {
|
||||
let base1: Base1
|
||||
let base2: Base2
|
||||
|
||||
init(_ base1: Base1, _ base2: Base2) {
|
||||
self.base1 = base1
|
||||
self.base2 = base2
|
||||
}
|
||||
}
|
||||
|
||||
extension AsyncZip2Sequence: AsyncSequence {
|
||||
public typealias Element = (Base1.Element, Base2.Element)
|
||||
|
||||
/// The iterator for an `AsyncZip2Sequence` instance.
|
||||
public struct Iterator: AsyncIteratorProtocol, Sendable {
|
||||
var base1: Base1.AsyncIterator?
|
||||
var base2: Base2.AsyncIterator?
|
||||
|
||||
enum Partial: Sendable {
|
||||
case first(Result<Base1.Element?, Error>, Base1.AsyncIterator)
|
||||
case second(Result<Base2.Element?, Error>, Base2.AsyncIterator)
|
||||
}
|
||||
|
||||
init(_ base1: Base1.AsyncIterator, _ base2: Base2.AsyncIterator) {
|
||||
self.base1 = base1
|
||||
self.base2 = base2
|
||||
}
|
||||
|
||||
public mutating func next() async rethrows -> (Base1.Element, Base2.Element)? {
|
||||
func iteration(
|
||||
_ group: inout TaskGroup<Partial>,
|
||||
_ value1: inout Base1.Element?,
|
||||
_ value2: inout Base2.Element?,
|
||||
_ iterator1: inout Base1.AsyncIterator?,
|
||||
_ iterator2: inout Base2.AsyncIterator?
|
||||
) async -> Result<(Base1.Element, Base2.Element)?, Error>? {
|
||||
guard let partial = await group.next() else {
|
||||
return .success(nil)
|
||||
}
|
||||
switch partial {
|
||||
case .first(let res, let iter):
|
||||
switch res {
|
||||
case .success(let value):
|
||||
if let value = value {
|
||||
value1 = value
|
||||
iterator1 = iter
|
||||
return nil
|
||||
} else {
|
||||
group.cancelAll()
|
||||
return .success(nil)
|
||||
}
|
||||
case .failure(let error):
|
||||
group.cancelAll()
|
||||
return .failure(error)
|
||||
}
|
||||
case .second(let res, let iter):
|
||||
switch res {
|
||||
case .success(let value):
|
||||
if let value = value {
|
||||
value2 = value
|
||||
iterator2 = iter
|
||||
return nil
|
||||
} else {
|
||||
group.cancelAll()
|
||||
return .success(nil)
|
||||
}
|
||||
case .failure(let error):
|
||||
group.cancelAll()
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
guard let base1 = base1, let base2 = base2 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let (result, iter1, iter2) = await withTaskGroup(of: Partial.self) { group -> (Result<(Base1.Element, Base2.Element)?, Error>, Base1.AsyncIterator?, Base2.AsyncIterator?) in
|
||||
group.addTask {
|
||||
var iterator = base1
|
||||
do {
|
||||
let value = try await iterator.next()
|
||||
return .first(.success(value), iterator)
|
||||
} catch {
|
||||
return .first(.failure(error), iterator)
|
||||
}
|
||||
}
|
||||
group.addTask {
|
||||
var iterator = base2
|
||||
do {
|
||||
let value = try await iterator.next()
|
||||
return .second(.success(value), iterator)
|
||||
} catch {
|
||||
return .second(.failure(error), iterator)
|
||||
}
|
||||
}
|
||||
var res1: Base1.Element?
|
||||
var res2: Base2.Element?
|
||||
var iter1: Base1.AsyncIterator?
|
||||
var iter2: Base2.AsyncIterator?
|
||||
|
||||
if let result = await iteration(&group, &res1, &res2, &iter1, &iter2) {
|
||||
return (result, nil, nil)
|
||||
}
|
||||
if let result = await iteration(&group, &res1, &res2, &iter1, &iter2) {
|
||||
return (result, nil, nil)
|
||||
}
|
||||
guard let res1 = res1, let res2 = res2 else {
|
||||
return (.success(nil), nil, nil)
|
||||
}
|
||||
|
||||
return (.success((res1, res2)), iter1, iter2)
|
||||
}
|
||||
do {
|
||||
guard let value = try result._rethrowGet() else {
|
||||
self.base1 = nil
|
||||
self.base2 = nil
|
||||
return nil
|
||||
}
|
||||
self.base1 = iter1
|
||||
self.base2 = iter2
|
||||
return value
|
||||
} catch {
|
||||
self.base1 = nil
|
||||
self.base2 = nil
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
Iterator(base1.makeAsyncIterator(), base2.makeAsyncIterator())
|
||||
}
|
||||
}
|
||||
@@ -1,210 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// Creates an asynchronous sequence that concurrently awaits values from three `AsyncSequence` types
|
||||
/// and emits a tuple of the values.
|
||||
public func zip<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>(_ base1: Base1, _ base2: Base2, _ base3: Base3) -> AsyncZip3Sequence<Base1, Base2, Base3>
|
||||
where Base1: Sendable,
|
||||
Base2: Sendable,
|
||||
Base3: Sendable,
|
||||
Base1.AsyncIterator: Sendable,
|
||||
Base2.AsyncIterator: Sendable,
|
||||
Base3.AsyncIterator: Sendable,
|
||||
Base1.Element: Sendable,
|
||||
Base2.Element: Sendable,
|
||||
Base3.Element: Sendable {
|
||||
AsyncZip3Sequence(base1, base2, base3)
|
||||
}
|
||||
|
||||
/// An asynchronous sequence that concurrently awaits values from three `AsyncSequence` types
|
||||
/// and emits a tuple of the values.
|
||||
public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: Sendable
|
||||
where Base1: Sendable,
|
||||
Base2: Sendable,
|
||||
Base3: Sendable,
|
||||
Base1.AsyncIterator: Sendable,
|
||||
Base2.AsyncIterator: Sendable,
|
||||
Base3.AsyncIterator: Sendable,
|
||||
Base1.Element: Sendable,
|
||||
Base2.Element: Sendable,
|
||||
Base3.Element: Sendable {
|
||||
let base1: Base1
|
||||
let base2: Base2
|
||||
let base3: Base3
|
||||
|
||||
init(_ base1: Base1, _ base2: Base2, _ base3: Base3) {
|
||||
self.base1 = base1
|
||||
self.base2 = base2
|
||||
self.base3 = base3
|
||||
}
|
||||
}
|
||||
|
||||
extension AsyncZip3Sequence: AsyncSequence {
|
||||
public typealias Element = (Base1.Element, Base2.Element, Base3.Element)
|
||||
|
||||
/// The iterator for an `AsyncZip3Sequence` instance.
|
||||
public struct Iterator: AsyncIteratorProtocol, Sendable {
|
||||
var base1: Base1.AsyncIterator?
|
||||
var base2: Base2.AsyncIterator?
|
||||
var base3: Base3.AsyncIterator?
|
||||
|
||||
enum Partial: Sendable {
|
||||
case first(Result<Base1.Element?, Error>, Base1.AsyncIterator)
|
||||
case second(Result<Base2.Element?, Error>, Base2.AsyncIterator)
|
||||
case third(Result<Base3.Element?, Error>, Base3.AsyncIterator)
|
||||
}
|
||||
|
||||
init(_ base1: Base1.AsyncIterator, _ base2: Base2.AsyncIterator, _ base3: Base3.AsyncIterator) {
|
||||
self.base1 = base1
|
||||
self.base2 = base2
|
||||
self.base3 = base3
|
||||
}
|
||||
|
||||
public mutating func next() async rethrows -> (Base1.Element, Base2.Element, Base3.Element)? {
|
||||
func iteration(
|
||||
_ group: inout TaskGroup<Partial>,
|
||||
_ value1: inout Base1.Element?,
|
||||
_ value2: inout Base2.Element?,
|
||||
_ value3: inout Base3.Element?,
|
||||
_ iterator1: inout Base1.AsyncIterator?,
|
||||
_ iterator2: inout Base2.AsyncIterator?,
|
||||
_ iterator3: inout Base3.AsyncIterator?
|
||||
) async -> Result<(Base1.Element, Base2.Element, Base3.Element)?, Error>? {
|
||||
guard let partial = await group.next() else {
|
||||
return .success(nil)
|
||||
}
|
||||
switch partial {
|
||||
case .first(let res, let iter):
|
||||
switch res {
|
||||
case .success(let value):
|
||||
if let value = value {
|
||||
value1 = value
|
||||
iterator1 = iter
|
||||
return nil
|
||||
} else {
|
||||
group.cancelAll()
|
||||
return .success(nil)
|
||||
}
|
||||
case .failure(let error):
|
||||
group.cancelAll()
|
||||
return .failure(error)
|
||||
}
|
||||
case .second(let res, let iter):
|
||||
switch res {
|
||||
case .success(let value):
|
||||
if let value = value {
|
||||
value2 = value
|
||||
iterator2 = iter
|
||||
return nil
|
||||
} else {
|
||||
group.cancelAll()
|
||||
return .success(nil)
|
||||
}
|
||||
case .failure(let error):
|
||||
group.cancelAll()
|
||||
return .failure(error)
|
||||
}
|
||||
case .third(let res, let iter):
|
||||
switch res {
|
||||
case .success(let value):
|
||||
if let value = value {
|
||||
value3 = value
|
||||
iterator3 = iter
|
||||
return nil
|
||||
} else {
|
||||
group.cancelAll()
|
||||
return .success(nil)
|
||||
}
|
||||
case .failure(let error):
|
||||
group.cancelAll()
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
guard let base1 = base1, let base2 = base2, let base3 = base3 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let (result, iter1, iter2, iter3) = await withTaskGroup(of: Partial.self) { group -> (Result<(Base1.Element, Base2.Element, Base3.Element)?, Error>, Base1.AsyncIterator?, Base2.AsyncIterator?, Base3.AsyncIterator?) in
|
||||
group.addTask {
|
||||
var iterator = base1
|
||||
do {
|
||||
let value = try await iterator.next()
|
||||
return .first(.success(value), iterator)
|
||||
} catch {
|
||||
return .first(.failure(error), iterator)
|
||||
}
|
||||
}
|
||||
group.addTask {
|
||||
var iterator = base2
|
||||
do {
|
||||
let value = try await iterator.next()
|
||||
return .second(.success(value), iterator)
|
||||
} catch {
|
||||
return .second(.failure(error), iterator)
|
||||
}
|
||||
}
|
||||
group.addTask {
|
||||
var iterator = base3
|
||||
do {
|
||||
let value = try await iterator.next()
|
||||
return .third(.success(value), iterator)
|
||||
} catch {
|
||||
return .third(.failure(error), iterator)
|
||||
}
|
||||
}
|
||||
var res1: Base1.Element?
|
||||
var res2: Base2.Element?
|
||||
var res3: Base3.Element?
|
||||
var iter1: Base1.AsyncIterator?
|
||||
var iter2: Base2.AsyncIterator?
|
||||
var iter3: Base3.AsyncIterator?
|
||||
|
||||
if let result = await iteration(&group, &res1, &res2, &res3, &iter1, &iter2, &iter3) {
|
||||
return (result, nil, nil, nil)
|
||||
}
|
||||
if let result = await iteration(&group, &res1, &res2, &res3, &iter1, &iter2, &iter3) {
|
||||
return (result, nil, nil, nil)
|
||||
}
|
||||
if let result = await iteration(&group, &res1, &res2, &res3, &iter1, &iter2, &iter3) {
|
||||
return (result, nil, nil, nil)
|
||||
}
|
||||
guard let res1 = res1, let res2 = res2, let res3 = res3 else {
|
||||
return (.success(nil), nil, nil, nil)
|
||||
}
|
||||
|
||||
return (.success((res1, res2, res3)), iter1, iter2, iter3)
|
||||
}
|
||||
do {
|
||||
guard let value = try result._rethrowGet() else {
|
||||
self.base1 = nil
|
||||
self.base2 = nil
|
||||
self.base3 = nil
|
||||
return nil
|
||||
}
|
||||
self.base1 = iter1
|
||||
self.base2 = iter2
|
||||
self.base3 = iter3
|
||||
return value
|
||||
} catch {
|
||||
self.base1 = nil
|
||||
self.base2 = nil
|
||||
self.base3 = nil
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
Iterator(base1.makeAsyncIterator(), base2.makeAsyncIterator(), base3.makeAsyncIterator())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// Creates an asynchronous sequence that concurrently awaits values from two `AsyncSequence` types
|
||||
/// and emits a tuple of the values.
|
||||
public func zip<Base1: AsyncSequence, Base2: AsyncSequence>(
|
||||
_ base1: Base1,
|
||||
_ base2: Base2
|
||||
) -> AsyncZip2Sequence<Base1, Base2> {
|
||||
AsyncZip2Sequence(base1, base2)
|
||||
}
|
||||
|
||||
/// An asynchronous sequence that concurrently awaits values from two `AsyncSequence` types
|
||||
/// and emits a tuple of the values.
|
||||
public struct AsyncZip2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: AsyncSequence
|
||||
where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: Sendable {
|
||||
public typealias Element = (Base1.Element, Base2.Element)
|
||||
public typealias AsyncIterator = Iterator
|
||||
|
||||
let base1: Base1
|
||||
let base2: Base2
|
||||
|
||||
init(_ base1: Base1, _ base2: Base2) {
|
||||
self.base1 = base1
|
||||
self.base2 = base2
|
||||
}
|
||||
|
||||
public func makeAsyncIterator() -> AsyncIterator {
|
||||
Iterator(
|
||||
base1,
|
||||
base2
|
||||
)
|
||||
}
|
||||
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
let runtime: Zip2Runtime<Base1, Base2>
|
||||
|
||||
init(_ base1: Base1, _ base2: Base2) {
|
||||
self.runtime = Zip2Runtime(base1, base2)
|
||||
}
|
||||
|
||||
public mutating func next() async rethrows -> Element? {
|
||||
try await self.runtime.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// Creates an asynchronous sequence that concurrently awaits values from three `AsyncSequence` types
|
||||
/// and emits a tuple of the values.
|
||||
public func zip<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>(
|
||||
_ base1: Base1,
|
||||
_ base2: Base2,
|
||||
_ base3: Base3
|
||||
) -> AsyncZip3Sequence<Base1, Base2, Base3> {
|
||||
AsyncZip3Sequence(base1, base2, base3)
|
||||
}
|
||||
|
||||
/// An asynchronous sequence that concurrently awaits values from three `AsyncSequence` types
|
||||
/// and emits a tuple of the values.
|
||||
public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: AsyncSequence
|
||||
where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: Sendable, Base3: Sendable, Base3.Element: Sendable {
|
||||
public typealias Element = (Base1.Element, Base2.Element, Base3.Element)
|
||||
public typealias AsyncIterator = Iterator
|
||||
|
||||
let base1: Base1
|
||||
let base2: Base2
|
||||
let base3: Base3
|
||||
|
||||
init(_ base1: Base1, _ base2: Base2, _ base3: Base3) {
|
||||
self.base1 = base1
|
||||
self.base2 = base2
|
||||
self.base3 = base3
|
||||
}
|
||||
|
||||
public func makeAsyncIterator() -> AsyncIterator {
|
||||
Iterator(
|
||||
base1,
|
||||
base2,
|
||||
base3
|
||||
)
|
||||
}
|
||||
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
let runtime: Zip3Runtime<Base1, Base2, Base3>
|
||||
|
||||
init(_ base1: Base1, _ base2: Base2, _ base3: Base3) {
|
||||
self.runtime = Zip3Runtime(base1, base2, base3)
|
||||
}
|
||||
|
||||
public mutating func next() async rethrows -> Element? {
|
||||
try await self.runtime.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
final class Zip2Runtime<Base1: AsyncSequence, Base2: AsyncSequence>: Sendable
|
||||
where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: Sendable {
|
||||
typealias ZipStateMachine = Zip2StateMachine<Base1.Element, Base2.Element>
|
||||
|
||||
private let stateMachine = ManagedCriticalState(ZipStateMachine())
|
||||
private let base1: Base1
|
||||
private let base2: Base2
|
||||
|
||||
init(_ base1: Base1, _ base2: Base2) {
|
||||
self.base1 = base1
|
||||
self.base2 = base2
|
||||
}
|
||||
|
||||
func next() async rethrows -> (Base1.Element, Base2.Element)? {
|
||||
try await withTaskCancellationHandler {
|
||||
let output = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.rootTaskIsCancelled()
|
||||
}
|
||||
// clean the allocated resources and state
|
||||
self.handle(rootTaskIsCancelledOutput: output)
|
||||
} operation: {
|
||||
let results = await withUnsafeContinuation { continuation in
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let output = stateMachine.newDemandFromConsumer(suspendedDemand: continuation)
|
||||
switch output {
|
||||
case .startTask(let suspendedDemand):
|
||||
// first iteration, we start one task per base to iterate over them
|
||||
self.startTask(stateMachine: &stateMachine, suspendedDemand: suspendedDemand)
|
||||
|
||||
case .resumeBases(let suspendedBases):
|
||||
// bases can be iterated over for 1 iteration so their next value can be retrieved
|
||||
suspendedBases.forEach { $0.resume() }
|
||||
|
||||
case .terminate(let suspendedDemand):
|
||||
// the async sequence is already finished, immediately resuming
|
||||
suspendedDemand.resume(returning: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
guard let results else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
// acknowledging the consumption of the zipped values, so we can begin another iteration on the bases
|
||||
stateMachine.demandIsFulfilled()
|
||||
}
|
||||
|
||||
return try (results.0._rethrowGet(), results.1._rethrowGet())
|
||||
}
|
||||
}
|
||||
|
||||
private func handle(rootTaskIsCancelledOutput: ZipStateMachine.RootTaskIsCancelledOutput) {
|
||||
switch rootTaskIsCancelledOutput {
|
||||
case .terminate(let task, let suspendedBases, let suspendedDemands):
|
||||
suspendedBases?.forEach { $0.resume() }
|
||||
suspendedDemands?.forEach { $0?.resume(returning: nil) }
|
||||
task?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private func startTask(
|
||||
stateMachine: inout ZipStateMachine,
|
||||
suspendedDemand: ZipStateMachine.SuspendedDemand
|
||||
) {
|
||||
let task = Task {
|
||||
await withTaskGroup(of: Void.self) { group in
|
||||
group.addTask {
|
||||
var base1Iterator = self.base1.makeAsyncIterator()
|
||||
|
||||
do {
|
||||
while true {
|
||||
await withUnsafeContinuation { continuation in
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.newLoopFromBase1(suspendedBase: continuation)
|
||||
}
|
||||
|
||||
self.handle(newLoopFromBaseOutput: output)
|
||||
}
|
||||
|
||||
guard let element1 = try await base1Iterator.next() else {
|
||||
break
|
||||
}
|
||||
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.base1HasProducedElement(element: element1)
|
||||
}
|
||||
|
||||
self.handle(baseHasProducedElementOutput: output)
|
||||
}
|
||||
} catch {
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.baseHasProducedFailure(error: error)
|
||||
}
|
||||
|
||||
self.handle(baseHasProducedFailureOutput: output)
|
||||
}
|
||||
|
||||
let output = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.baseIsFinished()
|
||||
}
|
||||
|
||||
self.handle(baseIsFinishedOutput: output)
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
var base2Iterator = self.base2.makeAsyncIterator()
|
||||
|
||||
do {
|
||||
while true {
|
||||
await withUnsafeContinuation { continuation in
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.newLoopFromBase2(suspendedBase: continuation)
|
||||
}
|
||||
|
||||
self.handle(newLoopFromBaseOutput: output)
|
||||
}
|
||||
|
||||
guard let element2 = try await base2Iterator.next() else {
|
||||
break
|
||||
}
|
||||
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.base2HasProducedElement(element: element2)
|
||||
}
|
||||
|
||||
self.handle(baseHasProducedElementOutput: output)
|
||||
}
|
||||
} catch {
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.baseHasProducedFailure(error: error)
|
||||
}
|
||||
|
||||
self.handle(baseHasProducedFailureOutput: output)
|
||||
}
|
||||
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.baseIsFinished()
|
||||
}
|
||||
|
||||
self.handle(baseIsFinishedOutput: output)
|
||||
}
|
||||
}
|
||||
}
|
||||
stateMachine.taskIsStarted(task: task, suspendedDemand: suspendedDemand)
|
||||
}
|
||||
|
||||
private func handle(newLoopFromBaseOutput: ZipStateMachine.NewLoopFromBaseOutput) {
|
||||
switch newLoopFromBaseOutput {
|
||||
case .none:
|
||||
break
|
||||
|
||||
case .resumeBases(let suspendedBases):
|
||||
suspendedBases.forEach { $0.resume() }
|
||||
|
||||
case .terminate(let suspendedBase):
|
||||
suspendedBase.resume()
|
||||
}
|
||||
}
|
||||
|
||||
private func handle(baseHasProducedElementOutput: ZipStateMachine.BaseHasProducedElementOutput) {
|
||||
switch baseHasProducedElementOutput {
|
||||
case .none:
|
||||
break
|
||||
|
||||
case .resumeDemand(let suspendedDemand, let result1, let result2):
|
||||
suspendedDemand?.resume(returning: (result1, result2))
|
||||
}
|
||||
}
|
||||
|
||||
private func handle(baseHasProducedFailureOutput: ZipStateMachine.BaseHasProducedFailureOutput) {
|
||||
switch baseHasProducedFailureOutput {
|
||||
case .none:
|
||||
break
|
||||
|
||||
case .resumeDemandAndTerminate(let task, let suspendedDemand, let suspendedBases, let result1, let result2):
|
||||
suspendedDemand?.resume(returning: (result1, result2))
|
||||
suspendedBases.forEach { $0.resume() }
|
||||
task?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private func handle(baseIsFinishedOutput: ZipStateMachine.BaseIsFinishedOutput) {
|
||||
switch baseIsFinishedOutput {
|
||||
case .terminate(let task, let suspendedBases, let suspendedDemands):
|
||||
suspendedBases?.forEach { $0.resume() }
|
||||
suspendedDemands?.forEach { $0?.resume(returning: nil) }
|
||||
task?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
// clean the allocated resources and state
|
||||
let output = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.rootTaskIsCancelled()
|
||||
}
|
||||
|
||||
self.handle(rootTaskIsCancelledOutput: output)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
struct Zip2StateMachine<Element1, Element2>: Sendable
|
||||
where Element1: Sendable, Element2: Sendable {
|
||||
typealias SuspendedDemand = UnsafeContinuation<(Result<Element1, Error>, Result<Element2, Error>)?, Never>
|
||||
|
||||
private enum State {
|
||||
case initial
|
||||
case awaitingDemandFromConsumer(
|
||||
task: Task<Void, Never>?,
|
||||
suspendedBases: [UnsafeContinuation<Void, Never>]
|
||||
)
|
||||
case awaitingBaseResults(
|
||||
task: Task<Void, Never>?,
|
||||
result1: Result<Element1, Error>?,
|
||||
result2: Result<Element2, Error>?,
|
||||
suspendedBases: [UnsafeContinuation<Void, Never>],
|
||||
suspendedDemand: SuspendedDemand?
|
||||
)
|
||||
case finished
|
||||
}
|
||||
|
||||
private var state: State = .initial
|
||||
|
||||
mutating func taskIsStarted(
|
||||
task: Task<Void, Never>,
|
||||
suspendedDemand: SuspendedDemand
|
||||
) {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: nil,
|
||||
result2: nil,
|
||||
suspendedBases: [],
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
|
||||
default:
|
||||
preconditionFailure("Inconsistent state, the task cannot start while the state is other than initial")
|
||||
}
|
||||
}
|
||||
|
||||
enum NewDemandFromConsumerOutput {
|
||||
case resumeBases(suspendedBases: [UnsafeContinuation<Void, Never>])
|
||||
case startTask(suspendedDemand: SuspendedDemand)
|
||||
case terminate(suspendedDemand: SuspendedDemand)
|
||||
}
|
||||
|
||||
mutating func newDemandFromConsumer(
|
||||
suspendedDemand: UnsafeContinuation<(Result<Element1, Error>, Result<Element2, Error>)?, Never>
|
||||
) -> NewDemandFromConsumerOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
return .startTask(suspendedDemand: suspendedDemand)
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, let suspendedBases):
|
||||
self.state = .awaitingBaseResults(task: task, result1: nil, result2: nil, suspendedBases: [], suspendedDemand: suspendedDemand)
|
||||
return .resumeBases(suspendedBases: suspendedBases)
|
||||
|
||||
case .awaitingBaseResults:
|
||||
preconditionFailure("Inconsistent state, a demand is already suspended")
|
||||
|
||||
case .finished:
|
||||
return .terminate(suspendedDemand: suspendedDemand)
|
||||
}
|
||||
}
|
||||
|
||||
enum NewLoopFromBaseOutput {
|
||||
case none
|
||||
case resumeBases(suspendedBases: [UnsafeContinuation<Void, Never>])
|
||||
case terminate(suspendedBase: UnsafeContinuation<Void, Never>)
|
||||
}
|
||||
|
||||
mutating func newLoopFromBase1(suspendedBase: UnsafeContinuation<Void, Never>) -> NewLoopFromBaseOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, var suspendedBases):
|
||||
precondition(suspendedBases.count < 2, "There cannot be more than 2 suspended bases at the same time")
|
||||
suspendedBases.append(suspendedBase)
|
||||
self.state = .awaitingDemandFromConsumer(task: task, suspendedBases: suspendedBases)
|
||||
return .none
|
||||
|
||||
case .awaitingBaseResults(let task, let result1, let result2, var suspendedBases, let suspendedDemand):
|
||||
precondition(suspendedBases.count < 2, "There cannot be more than 2 suspended bases at the same time")
|
||||
if result1 != nil {
|
||||
suspendedBases.append(suspendedBase)
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .none
|
||||
} else {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .resumeBases(suspendedBases: [suspendedBase])
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return .terminate(suspendedBase: suspendedBase)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func newLoopFromBase2(suspendedBase: UnsafeContinuation<Void, Never>) -> NewLoopFromBaseOutput {
|
||||
switch state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, var suspendedBases):
|
||||
precondition(suspendedBases.count < 2, "There cannot be more than 2 suspended bases at the same time")
|
||||
suspendedBases.append(suspendedBase)
|
||||
self.state = .awaitingDemandFromConsumer(task: task, suspendedBases: suspendedBases)
|
||||
return .none
|
||||
|
||||
case .awaitingBaseResults(let task, let result1, let result2, var suspendedBases, let suspendedDemand):
|
||||
precondition(suspendedBases.count < 2, "There cannot be more than 2 suspended bases at the same time")
|
||||
if result2 != nil {
|
||||
suspendedBases.append(suspendedBase)
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .none
|
||||
} else {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .resumeBases(suspendedBases: [suspendedBase])
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return .terminate(suspendedBase: suspendedBase)
|
||||
}
|
||||
}
|
||||
|
||||
enum BaseHasProducedElementOutput {
|
||||
case none
|
||||
case resumeDemand(
|
||||
suspendedDemand: SuspendedDemand?,
|
||||
result1: Result<Element1, Error>,
|
||||
result2: Result<Element2, Error>
|
||||
)
|
||||
}
|
||||
|
||||
mutating func base1HasProducedElement(element: Element1) -> BaseHasProducedElementOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer:
|
||||
preconditionFailure("Inconsistent state, a base can only produce an element when the consumer is awaiting for it")
|
||||
|
||||
case .awaitingBaseResults(let task, _, let result2, let suspendedBases, let suspendedDemand):
|
||||
if let result2 {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: .success(element),
|
||||
result2: result2,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: nil
|
||||
)
|
||||
return .resumeDemand(suspendedDemand: suspendedDemand, result1: .success(element), result2: result2)
|
||||
} else {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: .success(element),
|
||||
result2: nil,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .none
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
mutating func base2HasProducedElement(element: Element2) -> BaseHasProducedElementOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer:
|
||||
preconditionFailure("Inconsistent state, a base can only produce an element when the consumer is awaiting for it")
|
||||
|
||||
case .awaitingBaseResults(let task, let result1, _, let suspendedBases, let suspendedDemand):
|
||||
if let result1 {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: .success(element),
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: nil
|
||||
)
|
||||
return .resumeDemand(suspendedDemand: suspendedDemand, result1: result1, result2: .success(element))
|
||||
} else {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: nil,
|
||||
result2: .success(element),
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .none
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
enum BaseHasProducedFailureOutput {
|
||||
case none
|
||||
case resumeDemandAndTerminate(
|
||||
task: Task<Void, Never>?,
|
||||
suspendedDemand: SuspendedDemand?,
|
||||
suspendedBases: [UnsafeContinuation<Void, Never>],
|
||||
result1: Result<Element1, Error>,
|
||||
result2: Result<Element2, Error>
|
||||
)
|
||||
}
|
||||
|
||||
mutating func baseHasProducedFailure(error: any Error) -> BaseHasProducedFailureOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer:
|
||||
preconditionFailure("Inconsistent state, a base can only produce an element when the consumer is awaiting for it")
|
||||
|
||||
case .awaitingBaseResults(let task, _, _, let suspendedBases, let suspendedDemand):
|
||||
self.state = .finished
|
||||
return .resumeDemandAndTerminate(
|
||||
task: task,
|
||||
suspendedDemand: suspendedDemand,
|
||||
suspendedBases: suspendedBases,
|
||||
result1: .failure(error),
|
||||
result2: .failure(error)
|
||||
)
|
||||
|
||||
case .finished:
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
mutating func base2HasProducedFailure(error: Error) -> BaseHasProducedFailureOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer:
|
||||
preconditionFailure("Inconsistent state, a base can only produce an element when the consumer is awaiting for it")
|
||||
|
||||
case .awaitingBaseResults(let task, _, _, let suspendedBases, let suspendedDemand):
|
||||
self.state = .finished
|
||||
return .resumeDemandAndTerminate(
|
||||
task: task,
|
||||
suspendedDemand: suspendedDemand,
|
||||
suspendedBases: suspendedBases,
|
||||
result1: .failure(error),
|
||||
result2: .failure(error)
|
||||
)
|
||||
|
||||
case .finished:
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
mutating func demandIsFulfilled() {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer:
|
||||
preconditionFailure("Inconsistent state, results are not yet available to be acknowledged")
|
||||
|
||||
case .awaitingBaseResults(let task, let result1, let result2, let suspendedBases, let suspendedDemand):
|
||||
precondition(suspendedDemand == nil, "Inconsistent state, there cannot be a suspended demand when ackowledging the demand")
|
||||
precondition(result1 != nil && result2 != nil, "Inconsistent state, all results are not yet available to be acknowledged")
|
||||
self.state = .awaitingDemandFromConsumer(task: task, suspendedBases: suspendedBases)
|
||||
|
||||
case .finished:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
enum RootTaskIsCancelledOutput {
|
||||
case terminate(
|
||||
task: Task<Void, Never>?,
|
||||
suspendedBases: [UnsafeContinuation<Void, Never>]?,
|
||||
suspendedDemands: [SuspendedDemand?]?
|
||||
)
|
||||
}
|
||||
|
||||
mutating func rootTaskIsCancelled() -> RootTaskIsCancelledOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
assertionFailure("Inconsistent state, the task is not started")
|
||||
self.state = .finished
|
||||
return .terminate(task: nil, suspendedBases: nil, suspendedDemands: nil)
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, let suspendedBases):
|
||||
self.state = .finished
|
||||
return .terminate(task: task, suspendedBases: suspendedBases, suspendedDemands: nil)
|
||||
|
||||
case .awaitingBaseResults(let task, _, _, let suspendedBases, let suspendedDemand):
|
||||
self.state = .finished
|
||||
return .terminate(task: task, suspendedBases: suspendedBases, suspendedDemands: [suspendedDemand])
|
||||
|
||||
case .finished:
|
||||
return .terminate(task: nil, suspendedBases: nil, suspendedDemands: nil)
|
||||
}
|
||||
}
|
||||
|
||||
enum BaseIsFinishedOutput {
|
||||
case terminate(
|
||||
task: Task<Void, Never>?,
|
||||
suspendedBases: [UnsafeContinuation<Void, Never>]?,
|
||||
suspendedDemands: [SuspendedDemand?]?
|
||||
)
|
||||
}
|
||||
|
||||
mutating func baseIsFinished() -> BaseIsFinishedOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, let suspendedBases):
|
||||
self.state = .finished
|
||||
return .terminate(task: task, suspendedBases: suspendedBases, suspendedDemands: nil)
|
||||
|
||||
case .awaitingBaseResults(let task, _, _, let suspendedBases, let suspendedDemand):
|
||||
self.state = .finished
|
||||
return .terminate(task: task, suspendedBases: suspendedBases, suspendedDemands: [suspendedDemand])
|
||||
|
||||
case .finished:
|
||||
return .terminate(task: nil, suspendedBases: nil, suspendedDemands: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
final class Zip3Runtime<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: Sendable
|
||||
where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: Sendable, Base3: Sendable, Base3.Element: Sendable {
|
||||
typealias ZipStateMachine = Zip3StateMachine<Base1.Element, Base2.Element, Base3.Element>
|
||||
|
||||
private let stateMachine = ManagedCriticalState(ZipStateMachine())
|
||||
private let base1: Base1
|
||||
private let base2: Base2
|
||||
private let base3: Base3
|
||||
|
||||
init(_ base1: Base1, _ base2: Base2, _ base3: Base3) {
|
||||
self.base1 = base1
|
||||
self.base2 = base2
|
||||
self.base3 = base3
|
||||
}
|
||||
|
||||
func next() async rethrows -> (Base1.Element, Base2.Element, Base3.Element)? {
|
||||
try await withTaskCancellationHandler {
|
||||
let output = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.rootTaskIsCancelled()
|
||||
}
|
||||
// clean the allocated resources and state
|
||||
self.handle(rootTaskIsCancelledOutput: output)
|
||||
} operation: {
|
||||
let results = await withUnsafeContinuation { continuation in
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let output = stateMachine.newDemandFromConsumer(suspendedDemand: continuation)
|
||||
switch output {
|
||||
case .startTask(let suspendedDemand):
|
||||
// first iteration, we start one task per base to iterate over them
|
||||
self.startTask(stateMachine: &stateMachine, suspendedDemand: suspendedDemand)
|
||||
|
||||
case .resumeBases(let suspendedBases):
|
||||
// bases can be iterated over for 1 iteration so their next value can be retrieved
|
||||
suspendedBases.forEach { $0.resume() }
|
||||
|
||||
case .terminate(let suspendedDemand):
|
||||
// the async sequence is already finished, immediately resuming
|
||||
suspendedDemand.resume(returning: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
guard let results else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
// acknowledging the consumption of the zipped values, so we can begin another iteration on the bases
|
||||
stateMachine.demandIsFulfilled()
|
||||
}
|
||||
|
||||
return try (results.0._rethrowGet(), results.1._rethrowGet(), results.2._rethrowGet())
|
||||
}
|
||||
}
|
||||
|
||||
private func handle(rootTaskIsCancelledOutput: ZipStateMachine.RootTaskIsCancelledOutput) {
|
||||
switch rootTaskIsCancelledOutput {
|
||||
case .terminate(let task, let suspendedBases, let suspendedDemands):
|
||||
suspendedBases?.forEach { $0.resume() }
|
||||
suspendedDemands?.forEach { $0?.resume(returning: nil) }
|
||||
task?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private func startTask(
|
||||
stateMachine: inout ZipStateMachine,
|
||||
suspendedDemand: ZipStateMachine.SuspendedDemand
|
||||
) {
|
||||
let task = Task {
|
||||
await withTaskGroup(of: Void.self) { group in
|
||||
group.addTask {
|
||||
var base1Iterator = self.base1.makeAsyncIterator()
|
||||
|
||||
do {
|
||||
while true {
|
||||
await withUnsafeContinuation { continuation in
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.newLoopFromBase1(suspendedBase: continuation)
|
||||
}
|
||||
|
||||
self.handle(newLoopFromBaseOutput: output)
|
||||
}
|
||||
|
||||
guard let element1 = try await base1Iterator.next() else {
|
||||
break
|
||||
}
|
||||
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.base1HasProducedElement(element: element1)
|
||||
}
|
||||
|
||||
self.handle(baseHasProducedElementOutput: output)
|
||||
}
|
||||
} catch {
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.baseHasProducedFailure(error: error)
|
||||
}
|
||||
|
||||
self.handle(baseHasProducedFailureOutput: output)
|
||||
}
|
||||
|
||||
let output = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.baseIsFinished()
|
||||
}
|
||||
|
||||
self.handle(baseIsFinishedOutput: output)
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
var base2Iterator = self.base2.makeAsyncIterator()
|
||||
|
||||
do {
|
||||
while true {
|
||||
await withUnsafeContinuation { continuation in
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.newLoopFromBase2(suspendedBase: continuation)
|
||||
}
|
||||
|
||||
self.handle(newLoopFromBaseOutput: output)
|
||||
}
|
||||
|
||||
guard let element2 = try await base2Iterator.next() else {
|
||||
break
|
||||
}
|
||||
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.base2HasProducedElement(element: element2)
|
||||
}
|
||||
|
||||
self.handle(baseHasProducedElementOutput: output)
|
||||
}
|
||||
} catch {
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.baseHasProducedFailure(error: error)
|
||||
}
|
||||
|
||||
self.handle(baseHasProducedFailureOutput: output)
|
||||
}
|
||||
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.baseIsFinished()
|
||||
}
|
||||
|
||||
self.handle(baseIsFinishedOutput: output)
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
var base3Iterator = self.base3.makeAsyncIterator()
|
||||
|
||||
do {
|
||||
while true {
|
||||
await withUnsafeContinuation { continuation in
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.newLoopFromBase3(suspendedBase: continuation)
|
||||
}
|
||||
|
||||
self.handle(newLoopFromBaseOutput: output)
|
||||
}
|
||||
|
||||
guard let element3 = try await base3Iterator.next() else {
|
||||
break
|
||||
}
|
||||
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.base3HasProducedElement(element: element3)
|
||||
}
|
||||
|
||||
self.handle(baseHasProducedElementOutput: output)
|
||||
}
|
||||
} catch {
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.baseHasProducedFailure(error: error)
|
||||
}
|
||||
|
||||
self.handle(baseHasProducedFailureOutput: output)
|
||||
}
|
||||
|
||||
let output = self.stateMachine.withCriticalRegion { machine in
|
||||
machine.baseIsFinished()
|
||||
}
|
||||
|
||||
self.handle(baseIsFinishedOutput: output)
|
||||
}
|
||||
}
|
||||
}
|
||||
stateMachine.taskIsStarted(task: task, suspendedDemand: suspendedDemand)
|
||||
}
|
||||
|
||||
private func handle(newLoopFromBaseOutput: ZipStateMachine.NewLoopFromBaseOutput) {
|
||||
switch newLoopFromBaseOutput {
|
||||
case .none:
|
||||
break
|
||||
|
||||
case .resumeBases(let suspendedBases):
|
||||
suspendedBases.forEach { $0.resume() }
|
||||
|
||||
case .terminate(let suspendedBase):
|
||||
suspendedBase.resume()
|
||||
}
|
||||
}
|
||||
|
||||
private func handle(baseHasProducedElementOutput: ZipStateMachine.BaseHasProducedElementOutput) {
|
||||
switch baseHasProducedElementOutput {
|
||||
case .none:
|
||||
break
|
||||
|
||||
case .resumeDemand(let suspendedDemand, let result1, let result2, let result3):
|
||||
suspendedDemand?.resume(returning: (result1, result2, result3))
|
||||
}
|
||||
}
|
||||
|
||||
private func handle(baseHasProducedFailureOutput: ZipStateMachine.BaseHasProducedFailureOutput) {
|
||||
switch baseHasProducedFailureOutput {
|
||||
case .none:
|
||||
break
|
||||
|
||||
case .resumeDemandAndTerminate(let task, let suspendedDemand, let suspendedBases, let result1, let result2, let result3):
|
||||
suspendedDemand?.resume(returning: (result1, result2, result3))
|
||||
suspendedBases.forEach { $0.resume() }
|
||||
task?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private func handle(baseIsFinishedOutput: ZipStateMachine.BaseIsFinishedOutput) {
|
||||
switch baseIsFinishedOutput {
|
||||
case .terminate(let task, let suspendedBases, let suspendedDemands):
|
||||
suspendedBases?.forEach { $0.resume() }
|
||||
suspendedDemands?.forEach { $0?.resume(returning: nil) }
|
||||
task?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
// clean the allocated resources and state
|
||||
let output = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.rootTaskIsCancelled()
|
||||
}
|
||||
|
||||
self.handle(rootTaskIsCancelledOutput: output)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,439 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
struct Zip3StateMachine<Element1, Element2, Element3>: Sendable
|
||||
where Element1: Sendable, Element2: Sendable, Element3: Sendable {
|
||||
typealias SuspendedDemand = UnsafeContinuation<(Result<Element1, Error>, Result<Element2, Error>, Result<Element3, Error>)?, Never>
|
||||
|
||||
private enum State {
|
||||
case initial
|
||||
case awaitingDemandFromConsumer(
|
||||
task: Task<Void, Never>?,
|
||||
suspendedBases: [UnsafeContinuation<Void, Never>]
|
||||
)
|
||||
case awaitingBaseResults(
|
||||
task: Task<Void, Never>?,
|
||||
result1: Result<Element1, Error>?,
|
||||
result2: Result<Element2, Error>?,
|
||||
result3: Result<Element3, Error>?,
|
||||
suspendedBases: [UnsafeContinuation<Void, Never>],
|
||||
suspendedDemand: SuspendedDemand?
|
||||
)
|
||||
case finished
|
||||
}
|
||||
|
||||
private var state: State = .initial
|
||||
|
||||
mutating func taskIsStarted(
|
||||
task: Task<Void, Never>,
|
||||
suspendedDemand: SuspendedDemand
|
||||
) {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: nil,
|
||||
result2: nil,
|
||||
result3: nil,
|
||||
suspendedBases: [],
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
|
||||
default:
|
||||
preconditionFailure("Inconsistent state, the task cannot start while the state is other than initial")
|
||||
}
|
||||
}
|
||||
|
||||
enum NewDemandFromConsumerOutput {
|
||||
case resumeBases(suspendedBases: [UnsafeContinuation<Void, Never>])
|
||||
case startTask(suspendedDemand: SuspendedDemand)
|
||||
case terminate(suspendedDemand: SuspendedDemand)
|
||||
}
|
||||
|
||||
mutating func newDemandFromConsumer(suspendedDemand: SuspendedDemand) -> NewDemandFromConsumerOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
return .startTask(suspendedDemand: suspendedDemand)
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, let suspendedBases):
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: nil,
|
||||
result2: nil,
|
||||
result3: nil,
|
||||
suspendedBases: [],
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .resumeBases(suspendedBases: suspendedBases)
|
||||
|
||||
case .awaitingBaseResults:
|
||||
preconditionFailure("Inconsistent state, a demand is already suspended")
|
||||
|
||||
case .finished:
|
||||
return .terminate(suspendedDemand: suspendedDemand)
|
||||
}
|
||||
}
|
||||
|
||||
enum NewLoopFromBaseOutput {
|
||||
case none
|
||||
case resumeBases(suspendedBases: [UnsafeContinuation<Void, Never>])
|
||||
case terminate(suspendedBase: UnsafeContinuation<Void, Never>)
|
||||
}
|
||||
|
||||
mutating func newLoopFromBase1(suspendedBase: UnsafeContinuation<Void, Never>) -> NewLoopFromBaseOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, var suspendedBases):
|
||||
precondition(suspendedBases.count < 3, "There cannot be more than 3 suspended bases at the same time")
|
||||
suspendedBases.append(suspendedBase)
|
||||
self.state = .awaitingDemandFromConsumer(task: task, suspendedBases: suspendedBases)
|
||||
return .none
|
||||
|
||||
case .awaitingBaseResults(let task, let result1, let result2, let result3, var suspendedBases, let suspendedDemand):
|
||||
precondition(suspendedBases.count < 3, "There cannot be more than 3 suspended bases at the same time")
|
||||
if result1 != nil {
|
||||
suspendedBases.append(suspendedBase)
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
result3: result3,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .none
|
||||
} else {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
result3: result3,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .resumeBases(suspendedBases: [suspendedBase])
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return .terminate(suspendedBase: suspendedBase)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func newLoopFromBase2(suspendedBase: UnsafeContinuation<Void, Never>) -> NewLoopFromBaseOutput {
|
||||
switch state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, var suspendedBases):
|
||||
precondition(suspendedBases.count < 3, "There cannot be more than 3 suspended bases at the same time")
|
||||
suspendedBases.append(suspendedBase)
|
||||
self.state = .awaitingDemandFromConsumer(task: task, suspendedBases: suspendedBases)
|
||||
return .none
|
||||
|
||||
case .awaitingBaseResults(let task, let result1, let result2, let result3, var suspendedBases, let suspendedDemand):
|
||||
precondition(suspendedBases.count < 3, "There cannot be more than 3 suspended bases at the same time")
|
||||
if result2 != nil {
|
||||
suspendedBases.append(suspendedBase)
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
result3: result3,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .none
|
||||
} else {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
result3: result3,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .resumeBases(suspendedBases: [suspendedBase])
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return .terminate(suspendedBase: suspendedBase)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func newLoopFromBase3(suspendedBase: UnsafeContinuation<Void, Never>) -> NewLoopFromBaseOutput {
|
||||
switch state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, var suspendedBases):
|
||||
precondition(suspendedBases.count < 3, "There cannot be more than 3 suspended bases at the same time")
|
||||
suspendedBases.append(suspendedBase)
|
||||
self.state = .awaitingDemandFromConsumer(task: task, suspendedBases: suspendedBases)
|
||||
return .none
|
||||
|
||||
case .awaitingBaseResults(let task, let result1, let result2, let result3, var suspendedBases, let suspendedDemand):
|
||||
precondition(suspendedBases.count < 3, "There cannot be more than 3 suspended bases at the same time")
|
||||
if result3 != nil {
|
||||
suspendedBases.append(suspendedBase)
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
result3: result3,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .none
|
||||
} else {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
result3: result3,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .resumeBases(suspendedBases: [suspendedBase])
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return .terminate(suspendedBase: suspendedBase)
|
||||
}
|
||||
}
|
||||
|
||||
enum BaseHasProducedElementOutput {
|
||||
case none
|
||||
case resumeDemand(
|
||||
suspendedDemand: SuspendedDemand?,
|
||||
result1: Result<Element1, Error>,
|
||||
result2: Result<Element2, Error>,
|
||||
result3: Result<Element3, Error>
|
||||
)
|
||||
}
|
||||
|
||||
mutating func base1HasProducedElement(element: Element1) -> BaseHasProducedElementOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer:
|
||||
preconditionFailure("Inconsistent state, a base can only produce an element when the consumer is awaiting for it")
|
||||
|
||||
case .awaitingBaseResults(let task, _, let result2, let result3, let suspendedBases, let suspendedDemand):
|
||||
if let result2, let result3 {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: .success(element),
|
||||
result2: result2,
|
||||
result3: result3,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: nil
|
||||
)
|
||||
return .resumeDemand(suspendedDemand: suspendedDemand, result1: .success(element), result2: result2, result3: result3)
|
||||
} else {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: .success(element),
|
||||
result2: result2,
|
||||
result3: result3,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .none
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
mutating func base2HasProducedElement(element: Element2) -> BaseHasProducedElementOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer:
|
||||
preconditionFailure("Inconsistent state, a base can only produce an element when the consumer is awaiting for it")
|
||||
|
||||
case .awaitingBaseResults(let task, let result1, _, let result3, let suspendedBases, let suspendedDemand):
|
||||
if let result1, let result3 {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: .success(element),
|
||||
result3: result3,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: nil
|
||||
)
|
||||
return .resumeDemand(suspendedDemand: suspendedDemand, result1: result1, result2: .success(element), result3: result3)
|
||||
} else {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: .success(element),
|
||||
result3: result3,
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .none
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
mutating func base3HasProducedElement(element: Element3) -> BaseHasProducedElementOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer:
|
||||
preconditionFailure("Inconsistent state, a base can only produce an element when the consumer is awaiting for it")
|
||||
|
||||
case .awaitingBaseResults(let task, let result1, let result2, _, let suspendedBases, let suspendedDemand):
|
||||
if let result1, let result2 {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
result3: .success(element),
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: nil
|
||||
)
|
||||
return .resumeDemand(suspendedDemand: suspendedDemand, result1: result1, result2: result2, result3: .success(element))
|
||||
} else {
|
||||
self.state = .awaitingBaseResults(
|
||||
task: task,
|
||||
result1: result1,
|
||||
result2: result2,
|
||||
result3: .success(element),
|
||||
suspendedBases: suspendedBases,
|
||||
suspendedDemand: suspendedDemand
|
||||
)
|
||||
return .none
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
enum BaseHasProducedFailureOutput {
|
||||
case none
|
||||
case resumeDemandAndTerminate(
|
||||
task: Task<Void, Never>?,
|
||||
suspendedDemand: SuspendedDemand?,
|
||||
suspendedBases: [UnsafeContinuation<Void, Never>],
|
||||
result1: Result<Element1, Error>,
|
||||
result2: Result<Element2, Error>,
|
||||
result3: Result<Element3, Error>
|
||||
)
|
||||
}
|
||||
|
||||
mutating func baseHasProducedFailure(error: any Error) -> BaseHasProducedFailureOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer:
|
||||
preconditionFailure("Inconsistent state, a base can only produce an element when the consumer is awaiting for it")
|
||||
|
||||
case .awaitingBaseResults(let task, _, _, _, let suspendedBases, let suspendedDemand):
|
||||
self.state = .finished
|
||||
return .resumeDemandAndTerminate(
|
||||
task: task,
|
||||
suspendedDemand: suspendedDemand,
|
||||
suspendedBases: suspendedBases,
|
||||
result1: .failure(error),
|
||||
result2: .failure(error),
|
||||
result3: .failure(error)
|
||||
)
|
||||
|
||||
case .finished:
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
mutating func demandIsFulfilled() {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer:
|
||||
preconditionFailure("Inconsistent state, results are not yet available to be acknowledged")
|
||||
|
||||
case .awaitingBaseResults(let task, let result1, let result2, let result3, let suspendedBases, let suspendedDemand):
|
||||
precondition(suspendedDemand == nil, "Inconsistent state, there cannot be a suspended demand when ackowledging the demand")
|
||||
precondition(result1 != nil && result2 != nil && result3 != nil, "Inconsistent state, all results are not yet available to be acknowledged")
|
||||
self.state = .awaitingDemandFromConsumer(task: task, suspendedBases: suspendedBases)
|
||||
|
||||
case .finished:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
enum RootTaskIsCancelledOutput {
|
||||
case terminate(
|
||||
task: Task<Void, Never>?,
|
||||
suspendedBases: [UnsafeContinuation<Void, Never>]?,
|
||||
suspendedDemands: [SuspendedDemand?]?
|
||||
)
|
||||
}
|
||||
|
||||
mutating func rootTaskIsCancelled() -> RootTaskIsCancelledOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
assertionFailure("Inconsistent state, the task is not started")
|
||||
self.state = .finished
|
||||
return .terminate(task: nil, suspendedBases: nil, suspendedDemands: nil)
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, let suspendedBases):
|
||||
self.state = .finished
|
||||
return .terminate(task: task, suspendedBases: suspendedBases, suspendedDemands: nil)
|
||||
|
||||
case .awaitingBaseResults(let task, _, _, _, let suspendedBases, let suspendedDemand):
|
||||
self.state = .finished
|
||||
return .terminate(task: task, suspendedBases: suspendedBases, suspendedDemands: [suspendedDemand])
|
||||
|
||||
case .finished:
|
||||
return .terminate(task: nil, suspendedBases: nil, suspendedDemands: nil)
|
||||
}
|
||||
}
|
||||
|
||||
enum BaseIsFinishedOutput {
|
||||
case terminate(
|
||||
task: Task<Void, Never>?,
|
||||
suspendedBases: [UnsafeContinuation<Void, Never>]?,
|
||||
suspendedDemands: [SuspendedDemand?]?
|
||||
)
|
||||
}
|
||||
|
||||
mutating func baseIsFinished() -> BaseIsFinishedOutput {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Inconsistent state, the task is not started")
|
||||
|
||||
case .awaitingDemandFromConsumer(let task, let suspendedBases):
|
||||
self.state = .finished
|
||||
return .terminate(task: task, suspendedBases: suspendedBases, suspendedDemands: nil)
|
||||
|
||||
case .awaitingBaseResults(let task, _, _, _, let suspendedBases, let suspendedDemand):
|
||||
self.state = .finished
|
||||
return .terminate(task: task, suspendedBases: suspendedBases, suspendedDemands: [suspendedDemand])
|
||||
|
||||
case .finished:
|
||||
return .terminate(task: nil, suspendedBases: nil, suspendedDemands: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ final class TestZip2: XCTestCase {
|
||||
let actual = await Array(zip(a.async, b.async))
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_first_is_longer() async {
|
||||
let a = [1, 2, 3, 4, 5]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -30,7 +30,7 @@ final class TestZip2: XCTestCase {
|
||||
let actual = await Array(zip(a.async, b.async))
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_second_is_longer() async {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c", "d", "e"]
|
||||
@@ -39,7 +39,7 @@ final class TestZip2: XCTestCase {
|
||||
let actual = await Array(zip(a.async, b.async))
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_all_sequences_have_same_size() async {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -56,7 +56,7 @@ final class TestZip2: XCTestCase {
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_first_is_longer() async {
|
||||
let a = [1, 2, 3, 4, 5]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -73,7 +73,7 @@ final class TestZip2: XCTestCase {
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_second_is_longer() async {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c", "d", "e"]
|
||||
@@ -90,7 +90,7 @@ final class TestZip2: XCTestCase {
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_one_element_and_throws_when_first_produces_one_element_and_throws() async throws {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -112,7 +112,7 @@ final class TestZip2: XCTestCase {
|
||||
let pastEnd = try await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_one_element_and_throws_when_second_produces_one_element_and_throws() async throws {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -134,7 +134,7 @@ final class TestZip2: XCTestCase {
|
||||
let pastEnd = try await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_finishes_when_iteration_task_is_cancelled() async {
|
||||
let source1 = Indefinite(value: "test1")
|
||||
let source2 = Indefinite(value: "test2")
|
||||
@@ -170,7 +170,7 @@ final class TestZip3: XCTestCase {
|
||||
let actual = await Array(zip(a.async, b.async, c.async))
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_first_is_longer() async {
|
||||
let a = [1, 2, 3, 4, 5]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -180,7 +180,7 @@ final class TestZip3: XCTestCase {
|
||||
let actual = await Array(zip(a.async, b.async, c.async))
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_second_is_longer() async {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c", "d", "e"]
|
||||
@@ -190,7 +190,7 @@ final class TestZip3: XCTestCase {
|
||||
let actual = await Array(zip(a.async, b.async, c.async))
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_third_is_longer() async {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -200,7 +200,7 @@ final class TestZip3: XCTestCase {
|
||||
let actual = await Array(zip(a.async, b.async, c.async))
|
||||
XCTAssertEqual(expected, actual)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_all_sequences_have_same_size() async {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -218,7 +218,7 @@ final class TestZip3: XCTestCase {
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_first_is_longer() async {
|
||||
let a = [1, 2, 3, 4, 5]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -236,7 +236,7 @@ final class TestZip3: XCTestCase {
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_second_is_longer() async {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c", "d", "e"]
|
||||
@@ -254,7 +254,7 @@ final class TestZip3: XCTestCase {
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_third_is_longer() async {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -272,7 +272,7 @@ final class TestZip3: XCTestCase {
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_one_element_and_throws_when_first_produces_one_element_and_throws() async throws {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -295,7 +295,7 @@ final class TestZip3: XCTestCase {
|
||||
let pastEnd = try await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_one_element_and_throws_when_second_produces_one_element_and_throws() async throws {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -318,7 +318,7 @@ final class TestZip3: XCTestCase {
|
||||
let pastEnd = try await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_produces_one_element_and_throws_when_third_produces_one_element_and_throws() async throws {
|
||||
let a = [1, 2, 3]
|
||||
let b = ["a", "b", "c"]
|
||||
@@ -341,7 +341,7 @@ final class TestZip3: XCTestCase {
|
||||
let pastEnd = try await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
|
||||
func test_zip_finishes_when_iteration_task_is_cancelled() async {
|
||||
let source1 = Indefinite(value: "test1")
|
||||
let source2 = Indefinite(value: "test2")
|
||||
|
||||
Reference in new issue
Block a user