Apply formatting

This commit is contained in:
Franz Busch authored and Franz Busch committed 2025-03-28 21:59:41 +01:00
1 parent ba33a225e9
commit d5b49aab17
97 files changed
+5078 -4440

No files matched your search

@@ -21,7 +21,7 @@ public func zip<Base1: AsyncSequence, Base2: AsyncSequence>(
/// 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, Sendable
where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: Sendable {
where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: Sendable {
public typealias Element = (Base1.Element, Base2.Element)
public typealias AsyncIterator = Iterator
@@ -71,4 +71,4 @@ public struct AsyncZip2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Asy
}
@available(*, unavailable)
extension AsyncZip2Sequence.Iterator: Sendable { }
extension AsyncZip2Sequence.Iterator: Sendable {}
@@ -21,8 +21,16 @@ public func zip<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence
/// 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, Sendable
where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: Sendable, Base3: Sendable, Base3.Element: Sendable {
public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: AsyncSequence,
Sendable
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
@@ -37,7 +45,8 @@ public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base
}
public func makeAsyncIterator() -> AsyncIterator {
Iterator(storage: .init(self.base1, self.base2, self.base3)
Iterator(
storage: .init(self.base1, self.base2, self.base3)
)
}
@@ -76,4 +85,4 @@ public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base
}
@available(*, unavailable)
extension AsyncZip3Sequence.Iterator: Sendable { }
extension AsyncZip3Sequence.Iterator: Sendable {}
@@ -14,18 +14,24 @@ struct ZipStateMachine<
Base1: AsyncSequence,
Base2: AsyncSequence,
Base3: AsyncSequence
>: Sendable where
>: Sendable
where
Base1: Sendable,
Base2: Sendable,
Base3: Sendable,
Base1.Element: Sendable,
Base2.Element: Sendable,
Base3.Element: Sendable {
typealias DownstreamContinuation = UnsafeContinuation<Result<(
Base1.Element,
Base2.Element,
Base3.Element?
)?, Error>, Never>
Base3.Element: Sendable
{
typealias DownstreamContinuation = UnsafeContinuation<
Result<
(
Base1.Element,
Base2.Element,
Base3.Element?
)?, Error
>, Never
>
private enum State: Sendable {
/// Small wrapper for the state of an upstream sequence.
@@ -101,7 +107,9 @@ struct ZipStateMachine<
case .zipping:
// An iterator was deinitialized while we have a suspended continuation.
preconditionFailure("Internal inconsistency current state \(self.state) and received iteratorDeinitialized()")
preconditionFailure(
"Internal inconsistency current state \(self.state) and received iteratorDeinitialized()"
)
case .waitingForDemand(let task, let upstreams):
// The iterator was dropped which signals that the consumer is finished.
@@ -110,7 +118,8 @@ struct ZipStateMachine<
return .cancelTaskAndUpstreamContinuations(
task: task,
upstreamContinuations: [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation].compactMap { $0 }
upstreamContinuations: [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation]
.compactMap { $0 }
)
case .finished:
@@ -159,7 +168,10 @@ struct ZipStateMachine<
)
}
mutating func childTaskSuspended(baseIndex: Int, continuation: UnsafeContinuation<Void, Error>) -> ChildTaskSuspendedAction? {
mutating func childTaskSuspended(
baseIndex: Int,
continuation: UnsafeContinuation<Void, Error>
) -> ChildTaskSuspendedAction? {
switch self.state {
case .initial:
// Child tasks are only created after we transitioned to `zipping`
@@ -179,7 +191,9 @@ struct ZipStateMachine<
upstreams.2.continuation = continuation
default:
preconditionFailure("Internal inconsistency current state \(self.state) and received childTaskSuspended() with base index \(baseIndex)")
preconditionFailure(
"Internal inconsistency current state \(self.state) and received childTaskSuspended() with base index \(baseIndex)"
)
}
self.state = .waitingForDemand(
@@ -194,9 +208,7 @@ struct ZipStateMachine<
// already then we store the continuation otherwise we just go ahead and resume it
switch baseIndex {
case 0:
if upstreams.0.element == nil {
return .resumeContinuation(upstreamContinuation: continuation)
} else {
guard upstreams.0.element == nil else {
self.state = .modifying
upstreams.0.continuation = continuation
self.state = .zipping(
@@ -206,11 +218,10 @@ struct ZipStateMachine<
)
return .none
}
return .resumeContinuation(upstreamContinuation: continuation)
case 1:
if upstreams.1.element == nil {
return .resumeContinuation(upstreamContinuation: continuation)
} else {
guard upstreams.1.element == nil else {
self.state = .modifying
upstreams.1.continuation = continuation
self.state = .zipping(
@@ -220,11 +231,10 @@ struct ZipStateMachine<
)
return .none
}
return .resumeContinuation(upstreamContinuation: continuation)
case 2:
if upstreams.2.element == nil {
return .resumeContinuation(upstreamContinuation: continuation)
} else {
guard upstreams.2.element == nil else {
self.state = .modifying
upstreams.2.continuation = continuation
self.state = .zipping(
@@ -234,9 +244,12 @@ struct ZipStateMachine<
)
return .none
}
return .resumeContinuation(upstreamContinuation: continuation)
default:
preconditionFailure("Internal inconsistency current state \(self.state) and received childTaskSuspended() with base index \(baseIndex)")
preconditionFailure(
"Internal inconsistency current state \(self.state) and received childTaskSuspended() with base index \(baseIndex)"
)
}
case .finished:
@@ -295,8 +308,9 @@ struct ZipStateMachine<
// Implementing this for the two arities without variadic generics is a bit awkward sadly.
if let first = upstreams.0.element,
let second = upstreams.1.element,
let third = upstreams.2.element {
let second = upstreams.1.element,
let third = upstreams.2.element
{
// We got an element from each upstream so we can resume the downstream now
self.state = .waitingForDemand(
task: task,
@@ -313,8 +327,9 @@ struct ZipStateMachine<
)
} else if let first = upstreams.0.element,
let second = upstreams.1.element,
self.numberOfUpstreamSequences == 2 {
let second = upstreams.1.element,
self.numberOfUpstreamSequences == 2
{
// We got an element from each upstream so we can resume the downstream now
self.state = .waitingForDemand(
task: task,
@@ -385,7 +400,8 @@ struct ZipStateMachine<
return .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
downstreamContinuation: downstreamContinuation,
task: task,
upstreamContinuations: [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation].compactMap { $0 }
upstreamContinuations: [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation]
.compactMap { $0 }
)
case .finished:
@@ -429,7 +445,8 @@ struct ZipStateMachine<
downstreamContinuation: downstreamContinuation,
error: error,
task: task,
upstreamContinuations: [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation].compactMap { $0 }
upstreamContinuations: [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation]
.compactMap { $0 }
)
case .finished:
@@ -460,9 +477,9 @@ struct ZipStateMachine<
mutating func cancelled() -> CancelledAction? {
switch self.state {
case .initial:
state = .finished
state = .finished
return .none
return .none
case .waitingForDemand(let task, let upstreams):
// The downstream task got cancelled so we need to cancel our upstream Task
@@ -471,7 +488,8 @@ struct ZipStateMachine<
return .cancelTaskAndUpstreamContinuations(
task: task,
upstreamContinuations: [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation].compactMap { $0 }
upstreamContinuations: [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation]
.compactMap { $0 }
)
case .zipping(let task, let upstreams, let downstreamContinuation):
@@ -482,7 +500,8 @@ struct ZipStateMachine<
return .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
downstreamContinuation: downstreamContinuation,
task: task,
upstreamContinuations: [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation].compactMap { $0 }
upstreamContinuations: [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation]
.compactMap { $0 }
)
case .finished:
@@ -524,7 +543,8 @@ struct ZipStateMachine<
// We also need to resume all upstream continuations now
self.state = .modifying
let upstreamContinuations = [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation].compactMap { $0 }
let upstreamContinuations = [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation]
.compactMap { $0 }
upstreams.0.continuation = nil
upstreams.1.continuation = nil
upstreams.2.continuation = nil
+34 -27
View File
@@ -10,7 +10,14 @@
//===----------------------------------------------------------------------===//
final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: Sendable
where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: Sendable, Base3: Sendable, Base3.Element: Sendable {
where
Base1: Sendable,
Base1.Element: Sendable,
Base2: Sendable,
Base2.Element: Sendable,
Base3: Sendable,
Base3.Element: Sendable
{
typealias StateMachine = ZipStateMachine<Base1, Base2, Base3>
private let stateMachine: ManagedCriticalState<StateMachine>
@@ -63,9 +70,9 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
switch action {
case .startTask:
// We are handling the startTask in the lock already because we want to avoid
// other inputs interleaving while starting the task
fatalError("Internal inconsistency")
// We are handling the startTask in the lock already because we want to avoid
// other inputs interleaving while starting the task
fatalError("Internal inconsistency")
case .resumeUpstreamContinuations(let upstreamContinuations):
// bases can be iterated over for 1 iteration so their next value can be retrieved
@@ -76,7 +83,7 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
continuation.resume(returning: .success(nil))
case .none:
break
break
}
}
@@ -304,30 +311,30 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
}
while !group.isEmpty {
do {
try await group.next()
} catch {
do {
try await group.next()
} catch {
// One of the upstream sequences threw an error
let action = self.stateMachine.withCriticalRegion { stateMachine in
stateMachine.upstreamThrew(error)
}
switch action {
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
let downstreamContinuation,
let error,
let task,
let upstreamContinuations
):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
task.cancel()
downstreamContinuation.resume(returning: .failure(error))
case .none:
break
}
group.cancelAll()
let action = self.stateMachine.withCriticalRegion { stateMachine in
stateMachine.upstreamThrew(error)
}
switch action {
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
let downstreamContinuation,
let error,
let task,
let upstreamContinuations
):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
task.cancel()
downstreamContinuation.resume(returning: .failure(error))
case .none:
break
}
group.cancelAll()
}
}
}
}