mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-29 04:54:21 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
042e1c4d9d | ||
|
|
57b0814edf | ||
|
|
9fcf11df15 | ||
|
|
08bb1bb014 | ||
|
|
5878737018 | ||
|
|
552856a85c | ||
|
|
ba30f2051b | ||
|
|
d5b49aab17 | ||
|
|
ba33a225e9 | ||
|
|
4c3ea81f81 | ||
|
|
5c8bd186f4 | ||
|
|
2503842d68 | ||
|
|
e83857ca55 | ||
|
|
6ae9a051f7 | ||
|
|
46b4464735 | ||
|
|
d162617838 | ||
|
|
14729ea7f9 | ||
|
|
da4e36f865 | ||
|
|
5bbdcc1d37 | ||
|
|
cb417003f9 | ||
|
|
8cfdf03b51 | ||
|
|
6360ca0344 | ||
|
|
0a3866daec | ||
|
|
260e198221 | ||
|
|
f56556930f | ||
|
|
220f86f792 | ||
|
|
c889832c64 | ||
|
|
6dfbfd5e49 | ||
|
|
281e27c5d0 | ||
|
|
4a1fb99f00 | ||
|
|
b0ec4694d2 | ||
|
|
e639e5c889 | ||
|
|
7ac31f979f | ||
|
|
f5d5fb6483 | ||
|
|
df46b4c235 | ||
|
|
07a0c1ee08 | ||
|
|
adb12bfccc | ||
|
|
2ace7010e9 | ||
|
|
a42b72ad84 | ||
|
|
156bb4bf49 | ||
|
|
936a68d012 | ||
|
|
92747901e0 | ||
|
|
ac9309b2ac | ||
|
|
b3394663fd | ||
|
|
0c00af2372 | ||
|
|
9d83fff5b8 | ||
|
|
e6b21d2a6d | ||
|
|
7e45005473 | ||
|
|
ffa6ee9f0b | ||
|
|
9cfed92b02 | ||
|
|
ca2ac60495 | ||
|
|
bf0ec24bde | ||
|
|
0ebc8058fc | ||
|
|
a07a3f41b5 | ||
|
|
e7b9b6039d | ||
|
|
3f12423c47 |
No files matched your search
@@ -0,0 +1,15 @@
|
|||||||
|
name: Pull request
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
tests:
|
||||||
|
name: Test
|
||||||
|
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
|
||||||
|
soundness:
|
||||||
|
name: Soundness
|
||||||
|
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
|
||||||
|
with:
|
||||||
|
license_header_check_project_name: "Swift Async Algorithms"
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
@@===----------------------------------------------------------------------===@@
|
||||||
|
@@
|
||||||
|
@@ This source file is part of the Swift Async Algorithms open source project
|
||||||
|
@@
|
||||||
|
@@ Copyright (c) YEARS 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
|
||||||
|
@@
|
||||||
|
@@===----------------------------------------------------------------------===@@
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
.gitignore
|
||||||
|
**/.gitignore
|
||||||
|
.licenseignore
|
||||||
|
.gitattributes
|
||||||
|
.mailfilter
|
||||||
|
.mailmap
|
||||||
|
.spi.yml
|
||||||
|
.swift-format
|
||||||
|
.editorconfig
|
||||||
|
.github/*
|
||||||
|
*.md
|
||||||
|
*.txt
|
||||||
|
*.yml
|
||||||
|
*.yaml
|
||||||
|
*.json
|
||||||
|
Package.swift
|
||||||
|
**/Package.swift
|
||||||
|
Package@-*.swift
|
||||||
|
Package@swift-*.swift
|
||||||
|
**/Package@-*.swift
|
||||||
|
Package.resolved
|
||||||
|
**/Package.resolved
|
||||||
|
Makefile
|
||||||
|
*.modulemap
|
||||||
|
**/*.modulemap
|
||||||
|
**/*.docc/*
|
||||||
|
*.xcprivacy
|
||||||
|
**/*.xcprivacy
|
||||||
|
*.symlink
|
||||||
|
**/*.symlink
|
||||||
|
Dockerfile
|
||||||
|
**/Dockerfile
|
||||||
|
Snippets/*
|
||||||
|
dev/git.commit.template
|
||||||
|
*.crt
|
||||||
|
**/*.crt
|
||||||
|
*.pem
|
||||||
|
**/*.pem
|
||||||
|
*.der
|
||||||
|
**/*.der
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
version: 1
|
||||||
|
builder:
|
||||||
|
configs:
|
||||||
|
- documentation_targets: [AsyncAlgorithms]
|
||||||
|
swift_version: 5.8
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
{
|
||||||
|
"fileScopedDeclarationPrivacy" : {
|
||||||
|
"accessLevel" : "private"
|
||||||
|
},
|
||||||
|
"indentation" : {
|
||||||
|
"spaces" : 2
|
||||||
|
},
|
||||||
|
"indentConditionalCompilationBlocks" : false,
|
||||||
|
"indentSwitchCaseLabels" : false,
|
||||||
|
"lineBreakAroundMultilineExpressionChainComponents" : false,
|
||||||
|
"lineBreakBeforeControlFlowKeywords" : false,
|
||||||
|
"lineBreakBeforeEachArgument" : true,
|
||||||
|
"lineBreakBeforeEachGenericRequirement" : true,
|
||||||
|
"lineLength" : 120,
|
||||||
|
"maximumBlankLines" : 1,
|
||||||
|
"prioritizeKeepingFunctionOutputTogether" : true,
|
||||||
|
"respectsExistingLineBreaks" : true,
|
||||||
|
"rules" : {
|
||||||
|
"AllPublicDeclarationsHaveDocumentation" : false,
|
||||||
|
"AlwaysUseLowerCamelCase" : false,
|
||||||
|
"AmbiguousTrailingClosureOverload" : true,
|
||||||
|
"BeginDocumentationCommentWithOneLineSummary" : false,
|
||||||
|
"DoNotUseSemicolons" : true,
|
||||||
|
"DontRepeatTypeInStaticProperties" : true,
|
||||||
|
"FileScopedDeclarationPrivacy" : true,
|
||||||
|
"FullyIndirectEnum" : true,
|
||||||
|
"GroupNumericLiterals" : true,
|
||||||
|
"IdentifiersMustBeASCII" : true,
|
||||||
|
"NeverForceUnwrap" : false,
|
||||||
|
"NeverUseForceTry" : false,
|
||||||
|
"NeverUseImplicitlyUnwrappedOptionals" : false,
|
||||||
|
"NoAccessLevelOnExtensionDeclaration" : true,
|
||||||
|
"NoAssignmentInExpressions" : true,
|
||||||
|
"NoBlockComments" : true,
|
||||||
|
"NoCasesWithOnlyFallthrough" : true,
|
||||||
|
"NoEmptyTrailingClosureParentheses" : true,
|
||||||
|
"NoLabelsInCasePatterns" : false,
|
||||||
|
"NoLeadingUnderscores" : false,
|
||||||
|
"NoParensAroundConditions" : true,
|
||||||
|
"NoVoidReturnOnFunctionSignature" : true,
|
||||||
|
"OneCasePerLine" : true,
|
||||||
|
"OneVariableDeclarationPerLine" : true,
|
||||||
|
"OnlyOneTrailingClosureArgument" : true,
|
||||||
|
"OrderedImports" : false,
|
||||||
|
"ReturnVoidInsteadOfEmptyTuple" : true,
|
||||||
|
"UseEarlyExits" : true,
|
||||||
|
"UseLetInEveryBoundCaseVariable" : false,
|
||||||
|
"UseShorthandTypeNames" : true,
|
||||||
|
"UseSingleLinePropertyGetter" : false,
|
||||||
|
"UseSynthesizedInitializer" : false,
|
||||||
|
"UseTripleSlashForDocumentationComments" : true,
|
||||||
|
"UseWhereClausesInForLoops" : false,
|
||||||
|
"ValidateDocumentationComments" : false
|
||||||
|
},
|
||||||
|
"spacesAroundRangeFormationOperators" : false,
|
||||||
|
"tabWidth" : 4,
|
||||||
|
"version" : 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
extends: default
|
||||||
|
|
||||||
|
rules:
|
||||||
|
line-length: false
|
||||||
|
document-start: false
|
||||||
|
truthy:
|
||||||
|
check-keys: false # Otherwise we get a false positive on GitHub action's `on` key
|
||||||
@@ -51,7 +51,7 @@ would become part of a public API? If so, what kinds of changes can be
|
|||||||
made without breaking ABI? Can this feature be added/removed without
|
made without breaking ABI? Can this feature be added/removed without
|
||||||
breaking ABI? For more information about the resilience model, see the
|
breaking ABI? For more information about the resilience model, see the
|
||||||
[library evolution
|
[library evolution
|
||||||
document](https://github.com/apple/swift/blob/master/docs/LibraryEvolution.rst)
|
document](https://github.com/apple/swift/blob/main/docs/LibraryEvolution.rst)
|
||||||
in the Swift repository.
|
in the Swift repository.
|
||||||
|
|
||||||
## Alternatives considered
|
## Alternatives considered
|
||||||
|
|||||||
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
* Proposal: [SAA-0001](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0001-zip.md)
|
* Proposal: [SAA-0001](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0001-zip.md)
|
||||||
* Authors: [Philippe Hausler](https://github.com/phausler)
|
* Authors: [Philippe Hausler](https://github.com/phausler)
|
||||||
* Status: **Implemented**
|
* Status: **Accepted**
|
||||||
|
|
||||||
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncZip2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncZip3Sequence.swift) |
|
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Zip/AsyncZip2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Zip/AsyncZip3Sequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestZip.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestZip.swift)]
|
||||||
* Decision Notes:
|
* Decision Notes:
|
||||||
* Bugs:
|
* Bugs:
|
||||||
@@ -45,8 +45,7 @@ public func zip<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence
|
|||||||
public struct AsyncZip2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Sendable
|
public struct AsyncZip2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Sendable
|
||||||
where
|
where
|
||||||
Base1: Sendable, Base2: Sendable,
|
Base1: Sendable, Base2: Sendable,
|
||||||
Base1.Element: Sendable, Base2.Element: Sendable,
|
Base1.Element: Sendable, Base2.Element: Sendable {
|
||||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable {
|
|
||||||
public typealias Element = (Base1.Element, Base2.Element)
|
public typealias Element = (Base1.Element, Base2.Element)
|
||||||
|
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@@ -59,8 +58,7 @@ public struct AsyncZip2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Sen
|
|||||||
public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: Sendable
|
public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: Sendable
|
||||||
where
|
where
|
||||||
Base1: Sendable, Base2: Sendable, Base3: Sendable
|
Base1: Sendable, Base2: Sendable, Base3: Sendable
|
||||||
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable
|
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable {
|
||||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable, Base3.AsyncIterator: Sendable {
|
|
||||||
public typealias Element = (Base1.Element, Base2.Element, Base3.Element)
|
public typealias Element = (Base1.Element, Base2.Element, Base3.Element)
|
||||||
|
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
|||||||
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
* Proposal: [SAA-0002](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0002-merge.md)
|
* Proposal: [SAA-0002](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0002-merge.md)
|
||||||
* Authors: [Philippe Hausler](https://github.com/phausler)
|
* Authors: [Philippe Hausler](https://github.com/phausler)
|
||||||
* Status: **Implemented**
|
* Status: **Accepted**
|
||||||
|
|
||||||
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Asyncmerge2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncMerge3Sequence.swift) |
|
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Merge/AsyncMerge2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Merge/AsyncMerge3Sequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestMerge.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestMerge.swift)]
|
||||||
* Decision Notes:
|
* Decision Notes:
|
||||||
* Bugs:
|
* Bugs:
|
||||||
@@ -46,8 +46,7 @@ public struct AsyncMerge2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: S
|
|||||||
where
|
where
|
||||||
Base1.Element == Base2.Element,
|
Base1.Element == Base2.Element,
|
||||||
Base1: Sendable, Base2: Sendable,
|
Base1: Sendable, Base2: Sendable,
|
||||||
Base1.Element: Sendable, Base2.Element: Sendable,
|
Base1.Element: Sendable, Base2.Element: Sendable {
|
||||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable {
|
|
||||||
public typealias Element = Base1.Element
|
public typealias Element = Base1.Element
|
||||||
|
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@@ -61,8 +60,7 @@ public struct AsyncMerge3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Ba
|
|||||||
where
|
where
|
||||||
Base1.Element == Base2.Element, Base1.Element == Base3.Element,
|
Base1.Element == Base2.Element, Base1.Element == Base3.Element,
|
||||||
Base1: Sendable, Base2: Sendable, Base3: Sendable
|
Base1: Sendable, Base2: Sendable, Base3: Sendable
|
||||||
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable
|
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable {
|
||||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable, Base3.AsyncIterator: Sendable {
|
|
||||||
public typealias Element = Base1.Element
|
public typealias Element = Base1.Element
|
||||||
|
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
# Compacted
|
# Compacted
|
||||||
|
|
||||||
* Proposal: [0003](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0003-compacted.md)
|
* Proposal: [SAA-0003](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0003-compacted.md)
|
||||||
* Authors: [Philippe Hausler](https://github.com/phausler)
|
* Authors: [Philippe Hausler](https://github.com/phausler)
|
||||||
* Status: **Implemented**
|
* Status: **Accepted**
|
||||||
|
|
||||||
* Implementation: [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncCompactedSequence.swift)
|
* Implementation: [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncCompactedSequence.swift)
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestCompacted.swift)
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestCompacted.swift)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
* Proposal: [SAA-0004](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0004-joined.md)
|
* Proposal: [SAA-0004](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0004-joined.md)
|
||||||
* Authors: [Philippe Hausler](https://github.com/phausler)
|
* Authors: [Philippe Hausler](https://github.com/phausler)
|
||||||
* Review Manager: [Franz Busch](https://github.com/FranzBusch)
|
* Review Manager: [Franz Busch](https://github.com/FranzBusch)
|
||||||
* Status: **Implemented**
|
* Status: **Accepted**
|
||||||
|
|
||||||
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncJoinedSequence.swift) |
|
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncJoinedSequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestJoin.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestJoin.swift)]
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
* Proposal: [SAA-0005](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0005-adjacent-pairs.md)
|
* Proposal: [SAA-0005](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0005-adjacent-pairs.md)
|
||||||
* Author(s): [László Teveli](https://github.com/tevelee)
|
* Author(s): [László Teveli](https://github.com/tevelee)
|
||||||
* Review Manager: [Philippe Hausler](https://github.com/phausler)
|
* Review Manager: [Philippe Hausler](https://github.com/phausler)
|
||||||
* Status: **Implemented**
|
* Status: **Accepted**
|
||||||
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncAdjacentPairsSequence.swift) |
|
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncAdjacentPairsSequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestAdjacentPairs.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestAdjacentPairs.swift)]
|
||||||
* Decision Notes:
|
* Decision Notes:
|
||||||
|
|||||||
@@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
* Proposal: [SAA-0006](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0006-combineLatest.md)
|
* Proposal: [SAA-0006](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0006-combineLatest.md)
|
||||||
* Authors: [Philippe Hausler](https://github.com/phausler)
|
* Authors: [Philippe Hausler](https://github.com/phausler)
|
||||||
* Status: **Implemented**
|
* Status: **Accepted**
|
||||||
|
|
||||||
|
|
||||||
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncCombineLatest2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncCombineLatest3Sequence.swift) |
|
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/CombineLatest/AsyncCombineLatest2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/CombineLatest/AsyncCombineLatest3Sequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestCombineLatest.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestCombineLatest.swift)]
|
||||||
|
|
||||||
* Decision Notes:
|
* Decision Notes:
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
Similar to the `zip` algorithm there is a need to combine the latest values from multiple input asynchronous sequences. Since `AsyncSequence` augments the concept of sequence with the characteristic of time it means that the composition of elements may not just be pairwise emissions but instead be temporal composition. This means that it is useful to emit a new tuple _when_ a value is produced. The `combineLatest` algorithm provides precicely that.
|
Similar to the `zip` algorithm there is a need to combine the latest values from multiple input asynchronous sequences. Since `AsyncSequence` augments the concept of sequence with the characteristic of time it means that the composition of elements may not just be pairwise emissions but instead be temporal composition. This means that it is useful to emit a new tuple _when_ a value is produced. The `combineLatest` algorithm provides precisely that.
|
||||||
|
|
||||||
## Detailed Design
|
## Detailed Design
|
||||||
|
|
||||||
@@ -47,8 +47,7 @@ public func combineLatest<Base1: AsyncSequence, Base2: AsyncSequence, Base3: Asy
|
|||||||
public struct AsyncCombineLatest2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Sendable
|
public struct AsyncCombineLatest2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Sendable
|
||||||
where
|
where
|
||||||
Base1: Sendable, Base2: Sendable,
|
Base1: Sendable, Base2: Sendable,
|
||||||
Base1.Element: Sendable, Base2.Element: Sendable,
|
Base1.Element: Sendable, Base2.Element: Sendable {
|
||||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable {
|
|
||||||
public typealias Element = (Base1.Element, Base2.Element)
|
public typealias Element = (Base1.Element, Base2.Element)
|
||||||
|
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@@ -61,8 +60,7 @@ public struct AsyncCombineLatest2Sequence<Base1: AsyncSequence, Base2: AsyncSequ
|
|||||||
public struct AsyncCombineLatest3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: Sendable
|
public struct AsyncCombineLatest3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: Sendable
|
||||||
where
|
where
|
||||||
Base1: Sendable, Base2: Sendable, Base3: Sendable
|
Base1: Sendable, Base2: Sendable, Base3: Sendable
|
||||||
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable
|
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable {
|
||||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable, Base3.AsyncIterator: Sendable {
|
|
||||||
public typealias Element = (Base1.Element, Base2.Element, Base3.Element)
|
public typealias Element = (Base1.Element, Base2.Element, Base3.Element)
|
||||||
|
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
* Proposal: [SAA-0007](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0007-chain.md)
|
* Proposal: [SAA-0007](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0007-chain.md)
|
||||||
* Authors: [Philippe Hausler](https://github.com/phausler)
|
* Authors: [Philippe Hausler](https://github.com/phausler)
|
||||||
* Status: **Implemented**
|
* Status: **Accepted**
|
||||||
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChain2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChain3Sequence.swift) |
|
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChain2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChain3Sequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChain.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChain.swift)]
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
* Proposal: [SAA-0008](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0008-bytes.md)
|
* Proposal: [SAA-0008](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0008-bytes.md)
|
||||||
* Authors: [David Smith](https://github.com/Catfish-Man), [Philippe Hausler](https://github.com/phausler)
|
* Authors: [David Smith](https://github.com/Catfish-Man), [Philippe Hausler](https://github.com/phausler)
|
||||||
* Status: **Implemented**
|
* Status: **Accepted**
|
||||||
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncBufferedByteIterator.swift) |
|
* Implementation: [[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncBufferedByteIterator.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestBufferedByteIterator.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestBufferedByteIterator.swift)]
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ struct AsyncBytes: AsyncSequence {
|
|||||||
## Detailed Design
|
## Detailed Design
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
public struct AsyncBufferedByteIterator: AsyncIteratorProtocol, Sendable {
|
public struct AsyncBufferedByteIterator: AsyncIteratorProtocol {
|
||||||
public typealias Element = UInt8
|
public typealias Element = UInt8
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
# AsyncLazySequence
|
# AsyncSyncSequence
|
||||||
|
|
||||||
* Proposal: [NNNN](NNNN-lazy.md)
|
* Proposal: [SAA-0009](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0009-async.md)
|
||||||
* Authors: [Philippe Hausler](https://github.com/phausler)
|
* Authors: [Philippe Hausler](https://github.com/phausler)
|
||||||
* Status: **Implemented**
|
* Status: **Implemented**
|
||||||
|
|
||||||
* Implementation:
|
* Implementation:
|
||||||
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncLazySequence.swift) |
|
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncSyncSequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestLazy.swift)
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestLazy.swift)
|
||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
`AsyncLazySequence` converts a non-asynchronous sequence into an asynchronous one.
|
`AsyncSyncSequence` converts a non-asynchronous sequence into an asynchronous one.
|
||||||
|
|
||||||
This operation is available for all `Sequence` types.
|
This operation is available for all `Sequence` types.
|
||||||
|
|
||||||
@@ -22,19 +22,19 @@ let characters = "abcde".async
|
|||||||
This transformation can be useful to test operations specifically available on `AsyncSequence` but also is useful
|
This transformation can be useful to test operations specifically available on `AsyncSequence` but also is useful
|
||||||
to combine with other `AsyncSequence` types to provide well known sources of data.
|
to combine with other `AsyncSequence` types to provide well known sources of data.
|
||||||
|
|
||||||
The `.async` property returns an `AsyncLazySequence` that is generic upon the base `Sequence` it was constructed from.
|
The `.async` property returns an `AsyncSyncSequence` that is generic upon the base `Sequence` it was constructed from.
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
extension Sequence {
|
extension Sequence {
|
||||||
public var async: AsyncLazySequence<Self> { get }
|
public var async: AsyncSyncSequence<Self> { get }
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AsyncLazySequence<Base: Sequence>: AsyncSequence {
|
public struct AsyncSyncSequence<Base: Sequence>: AsyncSequence {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncLazySequence: Sendable where Base: Sendable { }
|
extension AsyncSyncSequence: Sendable where Base: Sendable { }
|
||||||
extension AsyncLazySequence.Iterator: Sendable where Base.Iterator: Sendable { }
|
extension AsyncSyncSequence.Iterator: Sendable where Base.Iterator: Sendable { }
|
||||||
```
|
```
|
||||||
|
|
||||||
### Naming
|
### Naming
|
||||||
@@ -45,4 +45,4 @@ succinct name in inspiration from `.lazy`, and the type is named in reference to
|
|||||||
|
|
||||||
## Effect on API resilience
|
## Effect on API resilience
|
||||||
|
|
||||||
`AsyncLazySequence` has a trivial implementation and is marked as `@frozen` and `@inlinable`. This removes the ability of this type and functions to be ABI resilient boundaries at the benefit of being highly optimizable.
|
`AsyncSyncSequence` has a trivial implementation and is marked as `@frozen` and `@inlinable`. This removes the ability of this type and functions to be ABI resilient boundaries at the benefit of being highly optimizable.
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
# Buffer
|
||||||
|
|
||||||
|
* Proposal: [SAA-0010](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0010-buffer.md)
|
||||||
|
* Author(s): [Thibault Wittemberg](https://github.com/twittemb)
|
||||||
|
* Status: **Accepted**
|
||||||
|
* Implementation: [
|
||||||
|
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Buffer/AsyncBufferSequence.swift) |
|
||||||
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestBuffer.swift)
|
||||||
|
]
|
||||||
|
* Decision Notes:
|
||||||
|
* Bugs:
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
Buffering is a technique that balances supply and demand by temporarily storing elements to even out fluctuations in production and consumption rates. `AsyncStream` facilitates this process by allowing you to control the size of the buffer and the strategy for handling elements that exceed that size. However, this approach may not be suitable for all situations, and it doesn't provide a way to adapt other `AsyncSequence` types to incorporate buffering.
|
||||||
|
|
||||||
|
This proposal presents a new type that addresses these more advanced requirements and offers a comprehensive solution for buffering in asynchronous sequences.
|
||||||
|
|
||||||
|
## Motivation
|
||||||
|
|
||||||
|
As an `AsyncSequence` operates as a pull system, the production of elements is directly tied to the demand expressed by the consumer. The slower the consumer is in requesting elements, the slower the production of these elements will be. This can negatively impact the software that produces the elements, as demonstrated in the following example.
|
||||||
|
|
||||||
|
Consider an `AsyncSequence` that reads and returns a line from a file every time a new element is requested. To ensure exclusive access to the file, a lock is maintained while reading. Ideally, the lock should be held for as short a duration as possible to allow other processes to access the file. However, if the consumer is slow in processing received lines or has a fluctuating pace, the lock will be maintained for an extended period, reducing the performance of the system.
|
||||||
|
|
||||||
|
To mitigate this issue, a buffer operator can be employed to streamline the consumption of the `AsyncSequence`. This operator allows an internal iteration of the `AsyncSequence` independently from the consumer. Each element would then be made available for consumption by using a queuing mechanism.
|
||||||
|
|
||||||
|
By applying the buffer operator to the previous example, the file can be read as efficiently as possible, allowing the lock to be released in a timely manner. This results in improved performance, as the consumer can consume elements at its own pace without negatively affecting the system. The buffer operator ensures that the production of elements is not limited by the pace of the consumer, allowing both the producer and consumer to operate at optimal levels.
|
||||||
|
|
||||||
|
## Proposed Solution
|
||||||
|
|
||||||
|
We propose to extend `AsyncSequence` with a `buffer()` operator. This operator will return an `AsyncBufferSequence` that wraps the source `AsyncSequence` and handle the buffering mechanism.
|
||||||
|
|
||||||
|
This operator will accept an `AsyncBufferSequencePolicy`. The policy will dictate the behaviour in case of a buffer overflow.
|
||||||
|
|
||||||
|
As of now we propose 4 different behaviours:
|
||||||
|
|
||||||
|
```swift
|
||||||
|
public struct AsyncBufferSequencePolicy: Sendable {
|
||||||
|
public static func bounded(_ limit: Int)
|
||||||
|
public static var unbounded
|
||||||
|
public static func bufferingLatest(_ limit: Int)
|
||||||
|
public static func bufferingOldest(_ limit: Int)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
And the public API of `AsyncBufferSequence` will be:
|
||||||
|
|
||||||
|
```swift
|
||||||
|
extension AsyncSequence where Self: Sendable {
|
||||||
|
public func buffer(
|
||||||
|
policy: AsyncBufferSequencePolicy
|
||||||
|
) -> AsyncBufferSequence<Self> {
|
||||||
|
AsyncBufferSequence<Self>(base: self, policy: policy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct AsyncBufferSequence<Base: AsyncSequence & Sendable>: AsyncSequence {
|
||||||
|
public typealias Element = Base.Element
|
||||||
|
|
||||||
|
public func makeAsyncIterator() -> Iterator
|
||||||
|
|
||||||
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
public mutating func next() async rethrows -> Element?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension AsyncBufferSequence: Sendable where Base: Sendable { }
|
||||||
|
```
|
||||||
|
|
||||||
|
## Notes on Sendable
|
||||||
|
|
||||||
|
Since all buffering means that the base asynchronous sequence must be iterated independently of the consumption (to resolve the production versus consumption issue) the base `AsyncSequence` needs to be able to be sent across task boundaries (the iterator does not need this requirement).
|
||||||
|
|
||||||
|
## Detailed Design
|
||||||
|
|
||||||
|
The choice of the buffering policy is made through an enumeration whose values are related to a storage type. To date, two types of storage are implemented:
|
||||||
|
|
||||||
|
- `BoundedBufferStorage` backing the `AsyncBufferSequencePolicy.bounded(_:)` policy,
|
||||||
|
- `UnboundedBufferStorage` backing the `AsyncBufferSequencePolicy.unbounded`, `AsyncBufferSequencePolicy.bufferingNewest(_:)` and `AsyncBufferSequencePolicy.bufferingLatest(_:)` policies.
|
||||||
|
|
||||||
|
Both storage types rely on a Mealy state machine stored in a `ManagedCriticalState`. They drive the mutations of the internal buffer, while ensuring that concurrent access to the state are safe.
|
||||||
|
|
||||||
|
### BoundedBufferStorage
|
||||||
|
|
||||||
|
`BoundedBufferStorage` is instantiated with the upstream `AsyncSequence` and a buffer maximum size. Upon the first call to the `next()` method, a task is spawned to support the iteration over this `AsyncSequence`. The iteration retrieves elements and adds them to an internal buffer as long as the buffer limit has not been reached. Meanwhile, the downstream `AsyncSequence` can access and consume these elements from the buffer. If the rate of consumption is slower than the rate of production, the buffer will eventually become full. In this case, the iteration is temporarily suspended until additional space becomes available.
|
||||||
|
|
||||||
|
### UnboundedBufferStorage
|
||||||
|
|
||||||
|
`UnboundedBufferStorage` is instantiated with the upstream `AsyncSequence` and a buffering policy. Upon the first call to the `next()` method, a task is spawned to support the iteration over this `AsyncSequence`.
|
||||||
|
|
||||||
|
From there the behaviour will depend on the buffering policy.
|
||||||
|
|
||||||
|
#### Unbounded
|
||||||
|
If the policy is `unbounded`, the iteration retrieves elements and adds them to an internal buffer until the upstream `AsyncSequence` finishes or fails. Meanwhile, the downstream `AsyncSequence` can access and consume these elements from the buffer.
|
||||||
|
|
||||||
|
#### BufferingLatest
|
||||||
|
If the policy is `bufferingLatest(_:)`, the iteration retrieves elements and adds them to an internal buffer. Meanwhile, the downstream `AsyncSequence` can access and consume these elements from the buffer. If the rate of consumption is slower than the rate of production, the buffer will eventually become full. In this case the oldest buffered elements will be removed from the buffer and the latest ones will be added.
|
||||||
|
|
||||||
|
#### BufferingOldest
|
||||||
|
If the policy is `bufferingOldest(_:)`, the iteration retrieves elements and adds them to an internal buffer. Meanwhile, the downstream `AsyncSequence` can access and consume these elements from the buffer. If the rate of consumption is slower than the rate of production, the buffer will eventually become full. In this case the latest element will be discarded and never added to the buffer.
|
||||||
|
|
||||||
|
### Terminal events and cancellation
|
||||||
|
|
||||||
|
Terminal events from the upstream `AsyncSequence`, such as normal completion or failure, are delayed, ensuring that the consumer will receive all the elements before receiving the termination.
|
||||||
|
|
||||||
|
If the consuming `Task` is cancelled, so will be the `Task` supporting the iteration of the upstream `AsyncSequence`.
|
||||||
|
|
||||||
|
## Alternatives Considered
|
||||||
|
|
||||||
|
The buffering mechanism was originally thought to rely on an open implementation of an `AsyncBuffer` protocol, which would be constrained to an `Actor` conformance. It was meant for developers to be able to provide their own implementation of a buffering algorithm. This buffer was designed to control the behavior of elements when pushed and popped, in an isolated manner.
|
||||||
|
|
||||||
|
A default implementation was provided to offers a queuing strategy for buffering elements, with options for unbounded, oldest-first, or newest-first buffering.
|
||||||
|
|
||||||
|
This implementation was eventually discarded due to the potentially higher cost of calling isolated functions on an `Actor`, compared to using a low-level locking mechanism like the one employed in other operators through the use of the `ManagedCriticalState`.
|
||||||
@@ -0,0 +1,319 @@
|
|||||||
|
# Feature name
|
||||||
|
|
||||||
|
* Proposal: [SAA-0011](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0011-interspersed.md)
|
||||||
|
* Authors: [Philippe Hausler](https://github.com/phausler)
|
||||||
|
* Review Manager: [Franz Busch](https://github.com/FranzBusch)
|
||||||
|
* Status: **Implemented**
|
||||||
|
|
||||||
|
* Implementation:
|
||||||
|
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Interspersed/AsyncInterspersedSequence.swift) |
|
||||||
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestLazy.swift)
|
||||||
|
|
||||||
|
## Motivation
|
||||||
|
|
||||||
|
A common transformation that is applied to async sequences is to intersperse the elements with
|
||||||
|
a separator element.
|
||||||
|
|
||||||
|
## Proposed solution
|
||||||
|
|
||||||
|
We propose to add a new method on `AsyncSequence` that allows to intersperse
|
||||||
|
a separator between every n emitted element. This proposed API looks like this
|
||||||
|
|
||||||
|
```swift
|
||||||
|
public extension AsyncSequence {
|
||||||
|
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
|
||||||
|
/// the given separator between each element.
|
||||||
|
///
|
||||||
|
/// Any value of this asynchronous sequence's element type can be used as the separator.
|
||||||
|
///
|
||||||
|
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let input = ["A", "B", "C"].async
|
||||||
|
/// let interspersed = input.interspersed(with: "-")
|
||||||
|
/// for await element in interspersed {
|
||||||
|
/// print(element)
|
||||||
|
/// }
|
||||||
|
/// // Prints "A" "-" "B" "-" "C"
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - every: Dictates after how many elements a separator should be inserted.
|
||||||
|
/// - separator: The value to insert in between each of this async sequence’s elements.
|
||||||
|
/// - Returns: The interspersed asynchronous sequence of elements.
|
||||||
|
@inlinable
|
||||||
|
func interspersed(every: Int = 1, with separator: Element) -> AsyncInterspersedSequence<Self> {
|
||||||
|
AsyncInterspersedSequence(self, every: every, separator: separator)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
|
||||||
|
/// the given separator between each element.
|
||||||
|
///
|
||||||
|
/// Any value of this asynchronous sequence's element type can be used as the separator.
|
||||||
|
///
|
||||||
|
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let input = ["A", "B", "C"].async
|
||||||
|
/// let interspersed = input.interspersed(with: "-")
|
||||||
|
/// for await element in interspersed {
|
||||||
|
/// print(element)
|
||||||
|
/// }
|
||||||
|
/// // Prints "A" "-" "B" "-" "C"
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - every: Dictates after how many elements a separator should be inserted.
|
||||||
|
/// - separator: A closure that produces the value to insert in between each of this async sequence’s elements.
|
||||||
|
/// - Returns: The interspersed asynchronous sequence of elements.
|
||||||
|
@inlinable
|
||||||
|
func interspersed(every: Int = 1, with separator: @Sendable @escaping () -> Element) -> AsyncInterspersedSequence<Self> {
|
||||||
|
AsyncInterspersedSequence(self, every: every, separator: separator)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
|
||||||
|
/// the given separator between each element.
|
||||||
|
///
|
||||||
|
/// Any value of this asynchronous sequence's element type can be used as the separator.
|
||||||
|
///
|
||||||
|
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let input = ["A", "B", "C"].async
|
||||||
|
/// let interspersed = input.interspersed(with: "-")
|
||||||
|
/// for await element in interspersed {
|
||||||
|
/// print(element)
|
||||||
|
/// }
|
||||||
|
/// // Prints "A" "-" "B" "-" "C"
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - every: Dictates after how many elements a separator should be inserted.
|
||||||
|
/// - separator: A closure that produces the value to insert in between each of this async sequence’s elements.
|
||||||
|
/// - Returns: The interspersed asynchronous sequence of elements.
|
||||||
|
@inlinable
|
||||||
|
func interspersed(every: Int = 1, with separator: @Sendable @escaping () async -> Element) -> AsyncInterspersedSequence<Self> {
|
||||||
|
AsyncInterspersedSequence(self, every: every, separator: separator)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
|
||||||
|
/// the given separator between each element.
|
||||||
|
///
|
||||||
|
/// Any value of this asynchronous sequence's element type can be used as the separator.
|
||||||
|
///
|
||||||
|
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let input = ["A", "B", "C"].async
|
||||||
|
/// let interspersed = input.interspersed(with: "-")
|
||||||
|
/// for await element in interspersed {
|
||||||
|
/// print(element)
|
||||||
|
/// }
|
||||||
|
/// // Prints "A" "-" "B" "-" "C"
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - every: Dictates after how many elements a separator should be inserted.
|
||||||
|
/// - separator: A closure that produces the value to insert in between each of this async sequence’s elements.
|
||||||
|
/// - Returns: The interspersed asynchronous sequence of elements.
|
||||||
|
@inlinable
|
||||||
|
public func interspersed(every: Int = 1, with separator: @Sendable @escaping () throws -> Element) -> AsyncThrowingInterspersedSequence<Self> {
|
||||||
|
AsyncThrowingInterspersedSequence(self, every: every, separator: separator)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
|
||||||
|
/// the given separator between each element.
|
||||||
|
///
|
||||||
|
/// Any value of this asynchronous sequence's element type can be used as the separator.
|
||||||
|
///
|
||||||
|
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let input = ["A", "B", "C"].async
|
||||||
|
/// let interspersed = input.interspersed(with: "-")
|
||||||
|
/// for await element in interspersed {
|
||||||
|
/// print(element)
|
||||||
|
/// }
|
||||||
|
/// // Prints "A" "-" "B" "-" "C"
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - every: Dictates after how many elements a separator should be inserted.
|
||||||
|
/// - separator: A closure that produces the value to insert in between each of this async sequence’s elements.
|
||||||
|
/// - Returns: The interspersed asynchronous sequence of elements.
|
||||||
|
@inlinable
|
||||||
|
public func interspersed(every: Int = 1, with separator: @Sendable @escaping () async throws -> Element) -> AsyncThrowingInterspersedSequence<Self> {
|
||||||
|
AsyncThrowingInterspersedSequence(self, every: every, separator: separator)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Detailed design
|
||||||
|
|
||||||
|
The bulk of the implementation of the new `interspersed` method is inside the new
|
||||||
|
`AsyncInterspersedSequence` struct. It constructs an iterator to the base async sequence
|
||||||
|
inside its own iterator. The `AsyncInterspersedSequence.Iterator.next()` is forwarding the demand
|
||||||
|
to the base iterator.
|
||||||
|
|
||||||
|
Below is the implementation of the `AsyncInterspersedSequence`.
|
||||||
|
```swift
|
||||||
|
/// An asynchronous sequence that presents the elements of a base asynchronous sequence of
|
||||||
|
/// elements with a separator between each of those elements.
|
||||||
|
public struct AsyncInterspersedSequence<Base: AsyncSequence> {
|
||||||
|
@usableFromInline
|
||||||
|
internal enum Separator {
|
||||||
|
case element(Element)
|
||||||
|
case syncClosure(@Sendable () -> Element)
|
||||||
|
case asyncClosure(@Sendable () async -> Element)
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let base: Base
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let separator: Separator
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let every: Int
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ base: Base, every: Int, separator: Element) {
|
||||||
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
|
self.base = base
|
||||||
|
self.separator = .element(separator)
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () -> Element) {
|
||||||
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
|
self.base = base
|
||||||
|
self.separator = .syncClosure(separator)
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () async -> Element) {
|
||||||
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
|
self.base = base
|
||||||
|
self.separator = .asyncClosure(separator)
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension AsyncInterspersedSequence: AsyncSequence {
|
||||||
|
public typealias Element = Base.Element
|
||||||
|
|
||||||
|
/// The iterator for an `AsyncInterspersedSequence` asynchronous sequence.
|
||||||
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
@usableFromInline
|
||||||
|
internal enum State {
|
||||||
|
case start(Element?)
|
||||||
|
case element(Int)
|
||||||
|
case separator
|
||||||
|
case finished
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal var iterator: Base.AsyncIterator
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let separator: Separator
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let every: Int
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal var state = State.start(nil)
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ iterator: Base.AsyncIterator, every: Int, separator: Separator) {
|
||||||
|
self.iterator = iterator
|
||||||
|
self.separator = separator
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
|
||||||
|
public mutating func next() async rethrows -> Base.Element? {
|
||||||
|
// After the start, the state flips between element and separator. Before
|
||||||
|
// returning a separator, a check is made for the next element as a
|
||||||
|
// separator is only returned between two elements. The next element is
|
||||||
|
// stored to allow it to be returned in the next iteration. However, if
|
||||||
|
// the checking the next element throws, the separator is emitted before
|
||||||
|
// rethrowing that error.
|
||||||
|
switch state {
|
||||||
|
case var .start(element):
|
||||||
|
do {
|
||||||
|
if element == nil {
|
||||||
|
element = try await self.iterator.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
if let element = element {
|
||||||
|
if every == 1 {
|
||||||
|
state = .separator
|
||||||
|
} else {
|
||||||
|
state = .element(1)
|
||||||
|
}
|
||||||
|
return element
|
||||||
|
} else {
|
||||||
|
state = .finished
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
state = .finished
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
case .separator:
|
||||||
|
do {
|
||||||
|
if let element = try await iterator.next() {
|
||||||
|
state = .start(element)
|
||||||
|
switch separator {
|
||||||
|
case let .element(element):
|
||||||
|
return element
|
||||||
|
|
||||||
|
case let .syncClosure(closure):
|
||||||
|
return closure()
|
||||||
|
|
||||||
|
case let .asyncClosure(closure):
|
||||||
|
return await closure()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state = .finished
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
state = .finished
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
case let .element(count):
|
||||||
|
do {
|
||||||
|
if let element = try await iterator.next() {
|
||||||
|
let newCount = count + 1
|
||||||
|
if every == newCount {
|
||||||
|
state = .separator
|
||||||
|
} else {
|
||||||
|
state = .element(newCount)
|
||||||
|
}
|
||||||
|
return element
|
||||||
|
} else {
|
||||||
|
state = .finished
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
state = .finished
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
public func makeAsyncIterator() -> AsyncInterspersedSequence<Base>.Iterator {
|
||||||
|
Iterator(base.makeAsyncIterator(), every: every, separator: separator)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
# Channel
|
||||||
|
|
||||||
|
* Author(s): [Philippe Hausler](https://github.com/phausler)
|
||||||
|
|
||||||
|
[
|
||||||
|
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChannel.swift),
|
||||||
|
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncThrowingChannel.swift) |
|
||||||
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChannel.swift)
|
||||||
|
]
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
`AsyncStream` introduced a mechanism to send buffered elements from a context that doesn't use Swift concurrency into one that does. That design only addressed a portion of the potential use cases; the missing portion was the back pressure excerpted across two concurrency domains.
|
||||||
|
|
||||||
|
## Proposed Solution
|
||||||
|
|
||||||
|
To achieve a system that supports back pressure and allows for the communication of more than one value from one task to another we are introducing a new type, the _channel_. The channel will be a reference-type asynchronous sequence with an asynchronous sending capability that awaits the consumption of iteration. Each value sent by the channel will await the consumption of that value by iteration. That awaiting behavior will allow for the affordance of back pressure applied from the consumption site to be transmitted to the production site. This means that the rate of production cannot exceed the rate of consumption, and that the rate of consumption cannot exceed the rate of production. Sending a terminal event to the channel will instantly resume all pending operations for every producers and consumers.
|
||||||
|
|
||||||
|
## Detailed Design
|
||||||
|
|
||||||
|
Similar to the `AsyncStream` and `AsyncThrowingStream` types, the type for sending elements via back pressure will come in two versions. These two versions will account for the throwing nature or non-throwing nature of the elements being produced.
|
||||||
|
|
||||||
|
Each type will have functions to send elements and to send terminal events.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||||
|
public struct Iterator: AsyncIteratorProtocol, Sendable {
|
||||||
|
public mutating func next() async -> Element?
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(element elementType: Element.Type = Element.self)
|
||||||
|
|
||||||
|
public func send(_ element: Element) async
|
||||||
|
public func finish()
|
||||||
|
|
||||||
|
public func makeAsyncIterator() -> Iterator
|
||||||
|
}
|
||||||
|
|
||||||
|
public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: AsyncSequence, Sendable {
|
||||||
|
public struct Iterator: AsyncIteratorProtocol, Sendable {
|
||||||
|
public mutating func next() async throws -> Element?
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(element elementType: Element.Type = Element.self, failure failureType: Failure.Type = Failure.self)
|
||||||
|
|
||||||
|
public func send(_ element: Element) async
|
||||||
|
public func fail(_ error: Error) where Failure == Error
|
||||||
|
public func finish()
|
||||||
|
|
||||||
|
public func makeAsyncIterator() -> Iterator
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Channels are intended to be used as communication types between tasks. Particularly when one task produces values and another task consumes said values. On the one hand, the back pressure applied by `send(_:)` via the suspension/resume ensures that the production of values does not exceed the consumption of values from iteration. This method suspends after enqueuing the event and is resumed when the next call to `next()` on the `Iterator` is made. On the other hand, the call to `finish()` or `fail(_:)` immediately resumes all the pending operations for every producers and consumers. Thus, every suspended `send(_:)` operations instantly resume, so as every suspended `next()` operations by producing a nil value, or by throwing an error, indicating the termination of the iterations. Further calls to `send(_:)` will immediately resume. The calls to `send(:)` and `next()` will immediately resume when their supporting task is cancelled, other operations from other tasks will remain active.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
let channel = AsyncChannel<String>()
|
||||||
|
Task {
|
||||||
|
while let resultOfLongCalculation = doLongCalculations() {
|
||||||
|
await channel.send(resultOfLongCalculation)
|
||||||
|
}
|
||||||
|
channel.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
for await calculationResult in channel {
|
||||||
|
print(calculationResult)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The example above uses a task to perform intense calculations; each of which are sent to the other task via the `send(_:)` method. That call to `send(_:)` returns when the next iteration of the channel is invoked.
|
||||||
|
|
||||||
|
## Alternatives Considered
|
||||||
|
|
||||||
|
The use of the name "subject" was considered, due to its heritage as a name for a sync-to-async adapter type.
|
||||||
|
|
||||||
|
It was considered to make `AsyncChannel` and `AsyncThrowingChannel` actors, however due to the cancellation internals it would imply that these types would need to create new tasks to handle cancel events. The advantages of an actor in this particular case did not outweigh the impact of adjusting the implementations to be actors.
|
||||||
|
|
||||||
|
## Future Directions
|
||||||
|
|
||||||
|
`AsyncChannel` and `AsyncThrowingChannel` are just the prominent members of the channel-like behavior algorithms. It is reasonable to have as its own distinct type a buffering channel that provides more [tuned back pressure per a given buffer of elements](https://forums.swift.org/t/asyncchannel-should-we-allow-to-buffer/60876). These other members of the same category of algorithm should be considered on their own as distinct proposals.
|
||||||
|
|
||||||
|
## Credits/Inspiration
|
||||||
|
|
||||||
|
`AsyncChannel` and `AsyncThrowingChannel` was heavily inspired from `Subject` but with the key difference that it uses Swift concurrency to apply back pressure.
|
||||||
|
|
||||||
|
https://developer.apple.com/documentation/combine/subject/
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
When events can potentially happen faster than the desired consumption rate, there are multiple ways to handle the situation. One approach is to only emit values after a given period of time of inactivity, or "quiescence", has elapsed. This algorithm is commonly referred to as debouncing. A very close reelativee is an apporach to emit values after a given period has elapsed. These emitted values can be reduced from the values encountered during the waiting period. This algorithm is commonly referred to as throttling.
|
When events can potentially happen faster than the desired consumption rate, there are multiple ways to handle the situation. One approach is to only emit values after a given period of time of inactivity, or "quiescence", has elapsed. This algorithm is commonly referred to as debouncing. A very close relative is an approach to emit values after a given period has elapsed. These emitted values can be reduced from the values encountered during the waiting period. This algorithm is commonly referred to as throttling.
|
||||||
|
|
||||||
## Proposed Solution
|
## Proposed Solution
|
||||||
|
|
||||||
|
|||||||
+66
-31
@@ -1,51 +1,86 @@
|
|||||||
// swift-tools-version: 5.6
|
// swift-tools-version: 5.8
|
||||||
|
|
||||||
import PackageDescription
|
import PackageDescription
|
||||||
|
import CompilerPluginSupport
|
||||||
|
|
||||||
|
// Availability Macros
|
||||||
|
let availabilityTags = [Availability("AsyncAlgorithms")]
|
||||||
|
let versionNumbers = ["1.0"]
|
||||||
|
|
||||||
|
// Availability Macro Utilities
|
||||||
|
enum OSAvailability: String {
|
||||||
|
// This should match the package's deployment target
|
||||||
|
case initialIntroduction = "macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0"
|
||||||
|
case pending = "macOS 9999, iOS 9999, tvOS 9999, watchOS 9999"
|
||||||
|
// Use 10000 for future availability to avoid compiler magic around
|
||||||
|
// the 9999 version number but ensure it is greater than 9999
|
||||||
|
case future = "macOS 10000, iOS 10000, tvOS 10000, watchOS 10000"
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Availability {
|
||||||
|
let name: String
|
||||||
|
let osAvailability: OSAvailability
|
||||||
|
|
||||||
|
init(_ name: String, availability: OSAvailability = .initialIntroduction) {
|
||||||
|
self.name = name
|
||||||
|
self.osAvailability = availability
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let availabilityMacros: [SwiftSetting] = versionNumbers.flatMap { version in
|
||||||
|
availabilityTags.map {
|
||||||
|
.enableExperimentalFeature("AvailabilityMacro=\($0.name) \(version):\($0.osAvailability.rawValue)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let package = Package(
|
let package = Package(
|
||||||
name: "AsyncAlgorithms",
|
name: "swift-async-algorithms",
|
||||||
platforms: [
|
|
||||||
.macOS("10.15"),
|
|
||||||
.iOS("13.0"),
|
|
||||||
.tvOS("13.0"),
|
|
||||||
.watchOS("6.0")
|
|
||||||
],
|
|
||||||
products: [
|
products: [
|
||||||
.library(name: "AsyncAlgorithms", targets: ["AsyncAlgorithms"]),
|
.library(name: "AsyncAlgorithms", targets: ["AsyncAlgorithms"])
|
||||||
.library(name: "AsyncSequenceValidation", targets: ["AsyncSequenceValidation"]),
|
|
||||||
.library(name: "_CAsyncSequenceValidationSupport", type: .static, targets: ["AsyncSequenceValidation"]),
|
|
||||||
.library(name: "AsyncAlgorithms_XCTest", targets: ["AsyncAlgorithms_XCTest"]),
|
|
||||||
],
|
],
|
||||||
dependencies: [.package(url: "https://github.com/apple/swift-collections.git", .upToNextMajor(from: "1.0.3"))],
|
|
||||||
targets: [
|
targets: [
|
||||||
.target(
|
.target(
|
||||||
name: "AsyncAlgorithms",
|
name: "AsyncAlgorithms",
|
||||||
dependencies: [.product(name: "Collections", package: "swift-collections")]
|
dependencies: [
|
||||||
|
.product(name: "OrderedCollections", package: "swift-collections"),
|
||||||
|
.product(name: "DequeModule", package: "swift-collections"),
|
||||||
|
],
|
||||||
|
swiftSettings: availabilityMacros + [
|
||||||
|
.enableExperimentalFeature("StrictConcurrency=complete")
|
||||||
|
]
|
||||||
),
|
),
|
||||||
.target(
|
.target(
|
||||||
name: "AsyncSequenceValidation",
|
name: "AsyncSequenceValidation",
|
||||||
dependencies: ["_CAsyncSequenceValidationSupport", "AsyncAlgorithms"]),
|
dependencies: ["_CAsyncSequenceValidationSupport", "AsyncAlgorithms"],
|
||||||
|
swiftSettings: availabilityMacros + [
|
||||||
|
.enableExperimentalFeature("StrictConcurrency=complete")
|
||||||
|
]
|
||||||
|
),
|
||||||
.systemLibrary(name: "_CAsyncSequenceValidationSupport"),
|
.systemLibrary(name: "_CAsyncSequenceValidationSupport"),
|
||||||
.target(
|
.target(
|
||||||
name: "AsyncAlgorithms_XCTest",
|
name: "AsyncAlgorithms_XCTest",
|
||||||
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation"]),
|
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation"],
|
||||||
|
swiftSettings: availabilityMacros + [
|
||||||
|
.enableExperimentalFeature("StrictConcurrency=complete")
|
||||||
|
]
|
||||||
|
),
|
||||||
.testTarget(
|
.testTarget(
|
||||||
name: "AsyncAlgorithmsTests",
|
name: "AsyncAlgorithmsTests",
|
||||||
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation", "AsyncAlgorithms_XCTest"]),
|
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation", "AsyncAlgorithms_XCTest"],
|
||||||
|
swiftSettings: availabilityMacros + [
|
||||||
|
.enableExperimentalFeature("StrictConcurrency=complete")
|
||||||
|
]
|
||||||
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
#if canImport(Darwin)
|
if Context.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
|
||||||
import Darwin
|
package.dependencies += [
|
||||||
let buildingDocs = getenv("BUILDING_FOR_DOCUMENTATION_GENERATION") != nil
|
.package(url: "https://github.com/apple/swift-collections.git", from: "1.1.0"),
|
||||||
#elseif canImport(Glibc)
|
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
|
||||||
import Glibc
|
]
|
||||||
let buildingDocs = getenv("BUILDING_FOR_DOCUMENTATION_GENERATION") != nil
|
} else {
|
||||||
#else
|
package.dependencies += [
|
||||||
let buildingDocs = false
|
.package(path: "../swift-collections")
|
||||||
#endif
|
]
|
||||||
|
}
|
||||||
// Only require the docc plugin when building documentation
|
|
||||||
package.dependencies += buildingDocs ? [
|
|
||||||
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
|
|
||||||
] : []
|
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
// swift-tools-version: 5.6
|
||||||
|
|
||||||
|
import PackageDescription
|
||||||
|
|
||||||
|
let package = Package(
|
||||||
|
name: "swift-async-algorithms",
|
||||||
|
platforms: [
|
||||||
|
.macOS("10.15"),
|
||||||
|
.iOS("13.0"),
|
||||||
|
.tvOS("13.0"),
|
||||||
|
.watchOS("6.0"),
|
||||||
|
],
|
||||||
|
products: [
|
||||||
|
.library(name: "AsyncAlgorithms", targets: ["AsyncAlgorithms"]),
|
||||||
|
.library(name: "AsyncSequenceValidation", targets: ["AsyncSequenceValidation"]),
|
||||||
|
.library(name: "_CAsyncSequenceValidationSupport", type: .static, targets: ["AsyncSequenceValidation"]),
|
||||||
|
.library(name: "AsyncAlgorithms_XCTest", targets: ["AsyncAlgorithms_XCTest"]),
|
||||||
|
],
|
||||||
|
dependencies: [
|
||||||
|
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
|
||||||
|
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
|
||||||
|
],
|
||||||
|
targets: [
|
||||||
|
.target(
|
||||||
|
name: "AsyncAlgorithms",
|
||||||
|
dependencies: [.product(name: "Collections", package: "swift-collections")]
|
||||||
|
),
|
||||||
|
.target(
|
||||||
|
name: "AsyncSequenceValidation",
|
||||||
|
dependencies: ["_CAsyncSequenceValidationSupport", "AsyncAlgorithms"]
|
||||||
|
),
|
||||||
|
.systemLibrary(name: "_CAsyncSequenceValidationSupport"),
|
||||||
|
.target(
|
||||||
|
name: "AsyncAlgorithms_XCTest",
|
||||||
|
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation"]
|
||||||
|
),
|
||||||
|
.testTarget(
|
||||||
|
name: "AsyncAlgorithmsTests",
|
||||||
|
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation", "AsyncAlgorithms_XCTest"]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
@@ -67,7 +67,7 @@ To use the `AsyncAlgorithms` library in a SwiftPM project,
|
|||||||
add the following line to the dependencies in your `Package.swift` file:
|
add the following line to the dependencies in your `Package.swift` file:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
.package(url: "https://github.com/apple/swift-async-algorithms"),
|
.package(url: "https://github.com/apple/swift-async-algorithms", from: "1.0.0"),
|
||||||
```
|
```
|
||||||
|
|
||||||
Include `"AsyncAlgorithms"` as a dependency for your executable target:
|
Include `"AsyncAlgorithms"` as a dependency for your executable target:
|
||||||
|
|||||||
@@ -9,8 +9,34 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncSequence {
|
||||||
|
/// An `AsyncSequence` that iterates over the adjacent pairs of the original
|
||||||
|
/// original `AsyncSequence`.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// for await (first, second) in (1...5).async.adjacentPairs() {
|
||||||
|
/// print("First: \(first), Second: \(second)")
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// // First: 1, Second: 2
|
||||||
|
/// // First: 2, Second: 3
|
||||||
|
/// // First: 3, Second: 4
|
||||||
|
/// // First: 4, Second: 5
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Returns: An `AsyncSequence` where the element is a tuple of two adjacent elements
|
||||||
|
/// or the original `AsyncSequence`.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
@inlinable
|
||||||
|
public func adjacentPairs() -> AsyncAdjacentPairsSequence<Self> {
|
||||||
|
AsyncAdjacentPairsSequence(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An `AsyncSequence` that iterates over the adjacent pairs of the original
|
/// An `AsyncSequence` that iterates over the adjacent pairs of the original
|
||||||
/// `AsyncSequence`.
|
/// `AsyncSequence`.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct AsyncAdjacentPairsSequence<Base: AsyncSequence>: AsyncSequence {
|
public struct AsyncAdjacentPairsSequence<Base: AsyncSequence>: AsyncSequence {
|
||||||
public typealias Element = (Base.Element, Base.Element)
|
public typealias Element = (Base.Element, Base.Element)
|
||||||
@@ -60,30 +86,8 @@ public struct AsyncAdjacentPairsSequence<Base: AsyncSequence>: AsyncSequence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncSequence {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
/// An `AsyncSequence` that iterates over the adjacent pairs of the original
|
extension AsyncAdjacentPairsSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
/// original `AsyncSequence`.
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// for await (first, second) in (1...5).async.adjacentPairs() {
|
|
||||||
/// print("First: \(first), Second: \(second)")
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// // First: 1, Second: 2
|
|
||||||
/// // First: 2, Second: 3
|
|
||||||
/// // First: 3, Second: 4
|
|
||||||
/// // First: 4, Second: 5
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// - Returns: An `AsyncSequence` where the element is a tuple of two adjacent elements
|
|
||||||
/// or the original `AsyncSequence`.
|
|
||||||
@inlinable
|
|
||||||
public func adjacentPairs() -> AsyncAdjacentPairsSequence<Self> {
|
|
||||||
AsyncAdjacentPairsSequence(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension AsyncAdjacentPairsSequence: Sendable where Base: Sendable, Base.Element: Sendable { }
|
|
||||||
|
|
||||||
@available(*, unavailable)
|
@available(*, unavailable)
|
||||||
extension AsyncAdjacentPairsSequence.Iterator: Sendable { }
|
extension AsyncAdjacentPairsSequence.Iterator: Sendable {}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
# AsyncBufferedByteIterator
|
# AsyncBufferedByteIterator
|
||||||
|
|
||||||
|
Provides a highly efficient iterator useful for iterating byte sequences derived from asynchronous read functions.
|
||||||
|
|
||||||
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncBufferedByteIterator.swift) |
|
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncBufferedByteIterator.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestBufferedByteIterator.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestBufferedByteIterator.swift)]
|
||||||
|
|
||||||
Provides a highly efficient iterator useful for iterating byte sequences derived from asynchronous read functions.
|
|
||||||
|
|
||||||
This type provides infrastructure for creating `AsyncSequence` types with an `Element` of `UInt8` backed by file descriptors or similar read sources.
|
This type provides infrastructure for creating `AsyncSequence` types with an `Element` of `UInt8` backed by file descriptors or similar read sources.
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
# Chain
|
# Chain
|
||||||
|
|
||||||
|
Chains two or more asynchronous sequences together sequentially.
|
||||||
|
|
||||||
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChain2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChain3Sequence.swift) |
|
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChain2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChain3Sequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChain.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChain.swift)]
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
* Author(s): [Philippe Hausler](https://github.com/phausler)
|
* Author(s): [Philippe Hausler](https://github.com/phausler)
|
||||||
|
|
||||||
[
|
[
|
||||||
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChannel.swift),
|
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Channels/AsyncChannel.swift),
|
||||||
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncThrowingChannel.swift) |
|
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Channels/AsyncThrowingChannel.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChannel.swift)
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChannel.swift)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -213,7 +213,7 @@ If both count and signal are specified, the chunking asynchronous sequence emits
|
|||||||
Like the example above, this code emits up to 1024-byte `Data` instances, but a chunk will also be emitted every second.
|
Like the example above, this code emits up to 1024-byte `Data` instances, but a chunk will also be emitted every second.
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let packets = bytes.chunks(ofCount: 1024 or: .repeating(every: .seconds(1)), into: Data.self)
|
let packets = bytes.chunks(ofCount: 1024, or: .repeating(every: .seconds(1)), into: Data.self)
|
||||||
for try await packet in packets {
|
for try await packet in packets {
|
||||||
write(packet)
|
write(packet)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# Combine Latest
|
# Combine Latest
|
||||||
|
|
||||||
|
Combines the latest values produced from two or more asynchronous sequences into an asynchronous sequence of tuples.
|
||||||
|
|
||||||
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/CombineLatest/AsyncCombineLatest2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/CombineLatest/AsyncCombineLatest3Sequence.swift) |
|
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/CombineLatest/AsyncCombineLatest2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/CombineLatest/AsyncCombineLatest3Sequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestCombineLatest.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestCombineLatest.swift)]
|
||||||
|
|
||||||
Combines the latest values produced from two or more asynchronous sequences into an asynchronous sequence of tuples.
|
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let appleFeed = URL("http://www.example.com/ticker?symbol=AAPL").lines
|
let appleFeed = URL("http://www.example.com/ticker?symbol=AAPL").lines
|
||||||
let nasdaqFeed = URL("http://www.example.com/ticker?symbol=^IXIC").lines
|
let nasdaqFeed = URL("http://www.example.com/ticker?symbol=^IXIC").lines
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
# Effects
|
||||||
|
|
||||||
|
Lists the effects of all async algorithms.
|
||||||
|
|
||||||
| Type | Throws | Sendability |
|
| Type | Throws | Sendability |
|
||||||
|-----------------------------------------------------|--------------|-------------|
|
|-----------------------------------------------------|--------------|-------------|
|
||||||
| `AsyncAdjacentPairsSequence` | rethrows | Conditional |
|
| `AsyncAdjacentPairsSequence` | rethrows | Conditional |
|
||||||
@@ -34,8 +38,8 @@
|
|||||||
| `AsyncInterspersedSequence.Iterator` | rethrows | Not Sendable|
|
| `AsyncInterspersedSequence.Iterator` | rethrows | Not Sendable|
|
||||||
| `AsyncJoinedSequence` | rethrows | Conditional |
|
| `AsyncJoinedSequence` | rethrows | Conditional |
|
||||||
| `AsyncJoinedSequence.Iterator` | rethrows | Not Sendable|
|
| `AsyncJoinedSequence.Iterator` | rethrows | Not Sendable|
|
||||||
| `AsyncLazySequence` | non-throwing | Conditional |
|
| `AsyncSyncSequence` | non-throwing | Conditional |
|
||||||
| `AsyncLazySequence.Iterator` | non-throwing | Not Sendable|
|
| `AsyncSyncSequence.Iterator` | non-throwing | Not Sendable|
|
||||||
| `AsyncLimitBuffer` | non-throwing | Sendable |
|
| `AsyncLimitBuffer` | non-throwing | Sendable |
|
||||||
| `AsyncMerge2Sequence` | rethrows | Sendable |
|
| `AsyncMerge2Sequence` | rethrows | Sendable |
|
||||||
| `AsyncMerge2Sequence.Iterator` | rethrows | Not Sendable|
|
| `AsyncMerge2Sequence.Iterator` | rethrows | Not Sendable|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# Intersperse
|
# Intersperse
|
||||||
|
|
||||||
|
Places a given value in between each element of the asynchronous sequence.
|
||||||
|
|
||||||
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncInterspersedSequence.swift) |
|
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncInterspersedSequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestInterspersed.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestInterspersed.swift)]
|
||||||
|
|
||||||
Places a given value in between each element of the asynchronous sequence.
|
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let numbers = [1, 2, 3].async.interspersed(with: 0)
|
let numbers = [1, 2, 3].async.interspersed(with: 0)
|
||||||
for await number in numbers {
|
for await number in numbers {
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
# AsyncLazySequence
|
# AsyncSyncSequence
|
||||||
|
|
||||||
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncLazySequence.swift) |
|
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestLazy.swift)]
|
|
||||||
|
|
||||||
Converts a non-asynchronous sequence into an asynchronous one.
|
|
||||||
|
|
||||||
This operation is available for all `Sequence` types.
|
This operation is available for all `Sequence` types.
|
||||||
|
|
||||||
|
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncSyncSequence.swift) |
|
||||||
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestLazy.swift)]
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let numbers = [1, 2, 3, 4].async
|
let numbers = [1, 2, 3, 4].async
|
||||||
let characters = "abcde".async
|
let characters = "abcde".async
|
||||||
@@ -17,19 +15,19 @@ to combine with other `AsyncSequence` types to provide well known sources of dat
|
|||||||
|
|
||||||
## Detailed Design
|
## Detailed Design
|
||||||
|
|
||||||
The `.async` property returns an `AsyncLazySequence` that is generic upon the base `Sequence` it was constructed from.
|
The `.async` property returns an `AsyncSyncSequence` that is generic upon the base `Sequence` it was constructed from.
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
extension Sequence {
|
extension Sequence {
|
||||||
public var async: AsyncLazySequence<Self> { get }
|
public var async: AsyncSyncSequence<Self> { get }
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AsyncLazySequence<Base: Sequence>: AsyncSequence {
|
public struct AsyncSyncSequence<Base: Sequence>: AsyncSequence {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncLazySequence: Sendable where Base: Sendable { }
|
extension AsyncSyncSequence: Sendable where Base: Sendable { }
|
||||||
extension AsyncLazySequence.Iterator: Sendable where Base.Iterator: Sendable { }
|
extension AsyncSyncSequence.Iterator: Sendable where Base.Iterator: Sendable { }
|
||||||
```
|
```
|
||||||
|
|
||||||
### Naming
|
### Naming
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# Merge
|
# Merge
|
||||||
|
|
||||||
|
Merges two or more asynchronous sequences sharing the same element type into one singular asynchronous sequence.
|
||||||
|
|
||||||
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Merge/AsyncMerge2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Merge/AsyncMerge3Sequence.swift) |
|
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Merge/AsyncMerge2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Merge/AsyncMerge3Sequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestMerge.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestMerge.swift)]
|
||||||
|
|
||||||
Merges two or more asynchronous sequences sharing the same element type into one singular asynchronous sequence.
|
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let appleFeed = URL(string: "http://www.example.com/ticker?symbol=AAPL")!.lines.map { "AAPL: " + $0 }
|
let appleFeed = URL(string: "http://www.example.com/ticker?symbol=AAPL")!.lines.map { "AAPL: " + $0 }
|
||||||
let nasdaqFeed = URL(string:"http://www.example.com/ticker?symbol=^IXIC")!.lines.map { "^IXIC: " + $0 }
|
let nasdaqFeed = URL(string:"http://www.example.com/ticker?symbol=^IXIC")!.lines.map { "^IXIC: " + $0 }
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ extension AsyncTimerSequence: Sendable { }
|
|||||||
extension AsyncTimerSequence.Iterator: Sendable { }
|
extension AsyncTimerSequence.Iterator: Sendable { }
|
||||||
```
|
```
|
||||||
|
|
||||||
Since all the types comprising `AsyncTimerSequence` and it's `Iterator` are `Sendable` these types are also `Sendable`.
|
Since all the types comprising `AsyncTimerSequence` are `Sendable` these types are also `Sendable`.
|
||||||
|
|
||||||
## Credits/Inspiration
|
## Credits/Inspiration
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# Zip
|
# Zip
|
||||||
|
|
||||||
|
Combines the latest values produced from two or more asynchronous sequences into an asynchronous sequence of tuples.
|
||||||
|
|
||||||
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Zip/AsyncZip2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Zip/AsyncZip3Sequence.swift) |
|
[[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Zip/AsyncZip2Sequence.swift), [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Zip/AsyncZip3Sequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestZip.swift)]
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestZip.swift)]
|
||||||
|
|
||||||
Combines the latest values produced from two or more asynchronous sequences into an asynchronous sequence of tuples.
|
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let appleFeed = URL(string: "http://www.example.com/ticker?symbol=AAPL")!.lines
|
let appleFeed = URL(string: "http://www.example.com/ticker?symbol=AAPL")!.lines
|
||||||
let nasdaqFeed = URL(string: "http://www.example.com/ticker?symbol=^IXIC")!.lines
|
let nasdaqFeed = URL(string: "http://www.example.com/ticker?symbol=^IXIC")!.lines
|
||||||
|
|||||||
@@ -1,308 +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
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
actor AsyncBufferState<Input: Sendable, Output: Sendable> {
|
|
||||||
enum TerminationState: Sendable, CustomStringConvertible {
|
|
||||||
case running
|
|
||||||
case baseFailure(Error) // An error from the base sequence has occurred. We need to process any buffered items before throwing the error. We can rely on it not emitting any more items.
|
|
||||||
case baseTermination
|
|
||||||
case terminal
|
|
||||||
|
|
||||||
var description: String {
|
|
||||||
switch self {
|
|
||||||
case .running: return "running"
|
|
||||||
case .baseFailure: return "base failure"
|
|
||||||
case .baseTermination: return "base termination"
|
|
||||||
case .terminal: return "terminal"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var pending = [UnsafeContinuation<Result<Output?, Error>, Never>]()
|
|
||||||
var terminationState = TerminationState.running
|
|
||||||
|
|
||||||
init() { }
|
|
||||||
|
|
||||||
func drain<Buffer: AsyncBuffer>(buffer: Buffer) async where Buffer.Input == Input, Buffer.Output == Output {
|
|
||||||
guard pending.count > 0 else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
if let value = try await buffer.pop() {
|
|
||||||
pending.removeFirst().resume(returning: .success(value))
|
|
||||||
} else {
|
|
||||||
switch terminationState {
|
|
||||||
case .running:
|
|
||||||
// There's no value to report, because it was probably grabbed by next() before we could grab it. The pending continuation was either resumed by next() directly, or will be by a future enqueued value or base termination/failure.
|
|
||||||
break
|
|
||||||
case .baseFailure(let error):
|
|
||||||
// Now that there are no more items in the buffer, we can finally report the base sequence's error and enter terminal state.
|
|
||||||
pending.removeFirst().resume(returning: .failure(error))
|
|
||||||
self.terminate()
|
|
||||||
case .terminal, .baseTermination:
|
|
||||||
self.terminate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Errors thrown by the buffer immediately terminate the sequence.
|
|
||||||
pending.removeFirst().resume(returning: .failure(error))
|
|
||||||
self.terminate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func enqueue<Buffer: AsyncBuffer>(_ item: Input, buffer: Buffer) async where Buffer.Input == Input, Buffer.Output == Output {
|
|
||||||
await buffer.push(item)
|
|
||||||
await drain(buffer: buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func fail<Buffer: AsyncBuffer>(_ error: Error, buffer: Buffer) async where Buffer.Input == Input, Buffer.Output == Output {
|
|
||||||
terminationState = .baseFailure(error)
|
|
||||||
await drain(buffer: buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func finish<Buffer: AsyncBuffer>(buffer: Buffer) async where Buffer.Input == Input, Buffer.Output == Output {
|
|
||||||
if case .running = terminationState {
|
|
||||||
terminationState = .baseTermination
|
|
||||||
}
|
|
||||||
await drain(buffer: buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func terminate() {
|
|
||||||
terminationState = .terminal
|
|
||||||
let oldPending = pending
|
|
||||||
pending = []
|
|
||||||
for continuation in oldPending {
|
|
||||||
continuation.resume(returning: .success(nil))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func next<Buffer: AsyncBuffer>(buffer: Buffer) async throws -> Buffer.Output? where Buffer.Input == Input, Buffer.Output == Output {
|
|
||||||
if case .terminal = terminationState {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
while let value = try await buffer.pop() {
|
|
||||||
if let continuation = pending.first {
|
|
||||||
pending.removeFirst()
|
|
||||||
continuation.resume(returning: .success(value))
|
|
||||||
} else {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Errors thrown by the buffer immediately terminate the sequence.
|
|
||||||
self.terminate()
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
|
|
||||||
switch terminationState {
|
|
||||||
case .running:
|
|
||||||
break
|
|
||||||
case .baseFailure(let error):
|
|
||||||
self.terminate()
|
|
||||||
throw error
|
|
||||||
case .baseTermination, .terminal:
|
|
||||||
self.terminate()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
let result: Result<Output?, Error> = await withUnsafeContinuation { continuation in
|
|
||||||
pending.append(continuation)
|
|
||||||
}
|
|
||||||
return try result._rethrowGet()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An asynchronous buffer storage actor protocol used for buffering
|
|
||||||
/// elements to an `AsyncBufferSequence`.
|
|
||||||
@rethrows
|
|
||||||
public protocol AsyncBuffer: Actor {
|
|
||||||
associatedtype Input: Sendable
|
|
||||||
associatedtype Output: Sendable
|
|
||||||
|
|
||||||
/// Push an element to enqueue to the buffer
|
|
||||||
func push(_ element: Input) async
|
|
||||||
|
|
||||||
/// Pop an element from the buffer.
|
|
||||||
///
|
|
||||||
/// Implementors of `pop()` may throw. In cases where types
|
|
||||||
/// throw from this function, that throwing behavior contributes to
|
|
||||||
/// the rethrowing characteristics of `AsyncBufferSequence`.
|
|
||||||
func pop() async throws -> Output?
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A buffer that limits pushed items by a certain count.
|
|
||||||
public actor AsyncLimitBuffer<Element: Sendable>: AsyncBuffer {
|
|
||||||
/// A policy for buffering elements to an `AsyncLimitBuffer`
|
|
||||||
public enum Policy: Sendable {
|
|
||||||
/// A policy for no bounding limit of pushed elements.
|
|
||||||
case unbounded
|
|
||||||
/// A policy for limiting to a specific number of oldest values.
|
|
||||||
case bufferingOldest(Int)
|
|
||||||
/// A policy for limiting to a specific number of newest values.
|
|
||||||
case bufferingNewest(Int)
|
|
||||||
}
|
|
||||||
|
|
||||||
var buffer = [Element]()
|
|
||||||
let policy: Policy
|
|
||||||
|
|
||||||
init(policy: Policy) {
|
|
||||||
// limits should always be greater than 0 items
|
|
||||||
switch policy {
|
|
||||||
case .bufferingNewest(let limit):
|
|
||||||
precondition(limit > 0)
|
|
||||||
case .bufferingOldest(let limit):
|
|
||||||
precondition(limit > 0)
|
|
||||||
default: break
|
|
||||||
}
|
|
||||||
self.policy = policy
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Push an element to enqueue to the buffer.
|
|
||||||
public func push(_ element: Element) async {
|
|
||||||
switch policy {
|
|
||||||
case .unbounded:
|
|
||||||
buffer.append(element)
|
|
||||||
case .bufferingOldest(let limit):
|
|
||||||
if buffer.count < limit {
|
|
||||||
buffer.append(element)
|
|
||||||
}
|
|
||||||
case .bufferingNewest(let limit):
|
|
||||||
if buffer.count < limit {
|
|
||||||
// there is space available
|
|
||||||
buffer.append(element)
|
|
||||||
} else {
|
|
||||||
// no space is available and this should make some room
|
|
||||||
buffer.removeFirst()
|
|
||||||
buffer.append(element)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Pop an element from the buffer.
|
|
||||||
public func pop() async -> Element? {
|
|
||||||
guard buffer.count > 0 else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return buffer.removeFirst()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension AsyncSequence where Element: Sendable, Self: Sendable {
|
|
||||||
/// Creates an asynchronous sequence that buffers elements using a buffer created from a supplied closure.
|
|
||||||
///
|
|
||||||
/// Use the `buffer(_:)` method to account for `AsyncSequence` types that may produce elements faster
|
|
||||||
/// than they are iterated. The `createBuffer` closure returns a backing buffer for storing elements and dealing with
|
|
||||||
/// behavioral characteristics of the `buffer(_:)` algorithm.
|
|
||||||
///
|
|
||||||
/// - Parameter createBuffer: A closure that constructs a new `AsyncBuffer` actor to store buffered values.
|
|
||||||
/// - Returns: An asynchronous sequence that buffers elements using the specified `AsyncBuffer`.
|
|
||||||
public func buffer<Buffer: AsyncBuffer>(_ createBuffer: @Sendable @escaping () -> Buffer) -> AsyncBufferSequence<Self, Buffer> where Buffer.Input == Element {
|
|
||||||
AsyncBufferSequence(self, createBuffer: createBuffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that buffers elements using a specific policy to limit the number of
|
|
||||||
/// elements that are buffered.
|
|
||||||
///
|
|
||||||
/// - Parameter policy: A limiting policy behavior on the buffering behavior of the `AsyncBufferSequence`
|
|
||||||
/// - Returns: An asynchronous sequence that buffers elements up to a given limit.
|
|
||||||
public func buffer(policy limit: AsyncLimitBuffer<Element>.Policy) -> AsyncBufferSequence<Self, AsyncLimitBuffer<Element>> {
|
|
||||||
buffer {
|
|
||||||
AsyncLimitBuffer(policy: limit)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An `AsyncSequence` that buffers elements utilizing an `AsyncBuffer`.
|
|
||||||
public struct AsyncBufferSequence<Base: AsyncSequence & Sendable, Buffer: AsyncBuffer> where Base.Element == Buffer.Input {
|
|
||||||
let base: Base
|
|
||||||
let createBuffer: @Sendable () -> Buffer
|
|
||||||
|
|
||||||
init(_ base: Base, createBuffer: @Sendable @escaping () -> Buffer) {
|
|
||||||
self.base = base
|
|
||||||
self.createBuffer = createBuffer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension AsyncBufferSequence: Sendable where Base: Sendable { }
|
|
||||||
|
|
||||||
extension AsyncBufferSequence: AsyncSequence {
|
|
||||||
public typealias Element = Buffer.Output
|
|
||||||
|
|
||||||
/// The iterator for a `AsyncBufferSequence` instance.
|
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
|
||||||
struct Active {
|
|
||||||
var task: Task<Void, Never>?
|
|
||||||
let buffer: Buffer
|
|
||||||
let state: AsyncBufferState<Buffer.Input, Buffer.Output>
|
|
||||||
|
|
||||||
init(_ base: Base, buffer: Buffer, state: AsyncBufferState<Buffer.Input, Buffer.Output>) {
|
|
||||||
self.buffer = buffer
|
|
||||||
self.state = state
|
|
||||||
task = Task {
|
|
||||||
var iter = base.makeAsyncIterator()
|
|
||||||
do {
|
|
||||||
while let item = try await iter.next() {
|
|
||||||
await state.enqueue(item, buffer: buffer)
|
|
||||||
}
|
|
||||||
await state.finish(buffer: buffer)
|
|
||||||
} catch {
|
|
||||||
await state.fail(error, buffer: buffer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func next() async rethrows -> Element? {
|
|
||||||
let result: Result<Element?, Error> = await withTaskCancellationHandler {
|
|
||||||
do {
|
|
||||||
let value = try await state.next(buffer: buffer)
|
|
||||||
return .success(value)
|
|
||||||
} catch {
|
|
||||||
task?.cancel()
|
|
||||||
return .failure(error)
|
|
||||||
}
|
|
||||||
} onCancel: {
|
|
||||||
task?.cancel()
|
|
||||||
}
|
|
||||||
return try result._rethrowGet()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum State {
|
|
||||||
case idle(Base, @Sendable () -> Buffer)
|
|
||||||
case active(Active)
|
|
||||||
}
|
|
||||||
|
|
||||||
var state: State
|
|
||||||
|
|
||||||
init(_ base: Base, createBuffer: @Sendable @escaping () -> Buffer) {
|
|
||||||
state = .idle(base, createBuffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
public mutating func next() async rethrows -> Element? {
|
|
||||||
switch state {
|
|
||||||
case .idle(let base, let createBuffer):
|
|
||||||
let bufferState = AsyncBufferState<Base.Element, Buffer.Output>()
|
|
||||||
let buffer = Active(base, buffer: createBuffer(), state: bufferState)
|
|
||||||
state = .active(buffer)
|
|
||||||
return try await buffer.next()
|
|
||||||
case .active(let buffer):
|
|
||||||
return try await buffer.next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public func makeAsyncIterator() -> Iterator {
|
|
||||||
Iterator(base, createBuffer: createBuffer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -39,10 +39,11 @@
|
|||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public struct AsyncBufferedByteIterator: AsyncIteratorProtocol {
|
public struct AsyncBufferedByteIterator: AsyncIteratorProtocol {
|
||||||
public typealias Element = UInt8
|
public typealias Element = UInt8
|
||||||
@usableFromInline var buffer: _AsyncBytesBuffer
|
@usableFromInline var buffer: _AsyncBytesBuffer
|
||||||
|
|
||||||
/// Creates an asynchronous buffered byte iterator with a specified capacity and read function.
|
/// Creates an asynchronous buffered byte iterator with a specified capacity and read function.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
@@ -55,7 +56,7 @@ public struct AsyncBufferedByteIterator: AsyncIteratorProtocol {
|
|||||||
) {
|
) {
|
||||||
buffer = _AsyncBytesBuffer(capacity: capacity, readFunction: readFunction)
|
buffer = _AsyncBytesBuffer(capacity: capacity, readFunction: readFunction)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a byte out of the buffer if available. When no bytes are available, this will trigger
|
/// Reads a byte out of the buffer if available. When no bytes are available, this will trigger
|
||||||
/// the read function to reload the buffer and then return the next byte from that buffer.
|
/// the read function to reload the buffer and then return the next byte from that buffer.
|
||||||
@inlinable @inline(__always)
|
@inlinable @inline(__always)
|
||||||
@@ -65,14 +66,15 @@ public struct AsyncBufferedByteIterator: AsyncIteratorProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@available(*, unavailable)
|
@available(*, unavailable)
|
||||||
extension AsyncBufferedByteIterator: Sendable { }
|
extension AsyncBufferedByteIterator: Sendable {}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen @usableFromInline
|
@frozen @usableFromInline
|
||||||
internal struct _AsyncBytesBuffer {
|
internal struct _AsyncBytesBuffer {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
final class Storage {
|
final class Storage {
|
||||||
fileprivate let buffer: UnsafeMutableRawBufferPointer
|
fileprivate let buffer: UnsafeMutableRawBufferPointer
|
||||||
|
|
||||||
init(
|
init(
|
||||||
capacity: Int
|
capacity: Int
|
||||||
) {
|
) {
|
||||||
@@ -82,19 +84,19 @@ internal struct _AsyncBytesBuffer {
|
|||||||
alignment: MemoryLayout<AnyObject>.alignment
|
alignment: MemoryLayout<AnyObject>.alignment
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
buffer.deallocate()
|
buffer.deallocate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@usableFromInline internal let storage: Storage
|
@usableFromInline internal let storage: Storage
|
||||||
@usableFromInline internal var nextPointer: UnsafeRawPointer
|
@usableFromInline internal var nextPointer: UnsafeRawPointer
|
||||||
@usableFromInline internal var endPointer: UnsafeRawPointer
|
@usableFromInline internal var endPointer: UnsafeRawPointer
|
||||||
|
|
||||||
internal let readFunction: @Sendable (UnsafeMutableRawBufferPointer) async throws -> Int
|
internal let readFunction: @Sendable (UnsafeMutableRawBufferPointer) async throws -> Int
|
||||||
internal var finished = false
|
internal var finished = false
|
||||||
|
|
||||||
@usableFromInline init(
|
@usableFromInline init(
|
||||||
capacity: Int,
|
capacity: Int,
|
||||||
readFunction: @Sendable @escaping (UnsafeMutableRawBufferPointer) async throws -> Int
|
readFunction: @Sendable @escaping (UnsafeMutableRawBufferPointer) async throws -> Int
|
||||||
@@ -105,7 +107,7 @@ internal struct _AsyncBytesBuffer {
|
|||||||
nextPointer = UnsafeRawPointer(s.buffer.baseAddress!)
|
nextPointer = UnsafeRawPointer(s.buffer.baseAddress!)
|
||||||
endPointer = nextPointer
|
endPointer = nextPointer
|
||||||
}
|
}
|
||||||
|
|
||||||
@inline(never) @usableFromInline
|
@inline(never) @usableFromInline
|
||||||
internal mutating func reloadBufferAndNext() async throws -> UInt8? {
|
internal mutating func reloadBufferAndNext() async throws -> UInt8? {
|
||||||
if finished {
|
if finished {
|
||||||
@@ -128,7 +130,7 @@ internal struct _AsyncBytesBuffer {
|
|||||||
}
|
}
|
||||||
return try await next()
|
return try await next()
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable @inline(__always)
|
@inlinable @inline(__always)
|
||||||
internal mutating func next() async throws -> UInt8? {
|
internal mutating func next() async throws -> UInt8? {
|
||||||
if _fastPath(nextPointer != endPointer) {
|
if _fastPath(nextPointer != endPointer) {
|
||||||
|
|||||||
@@ -17,20 +17,25 @@
|
|||||||
/// - s2: The second asynchronous sequence.
|
/// - s2: The second asynchronous sequence.
|
||||||
/// - Returns: An asynchronous sequence that iterates first over the elements of `s1`, and
|
/// - Returns: An asynchronous sequence that iterates first over the elements of `s1`, and
|
||||||
/// then over the elements of `s2`.
|
/// then over the elements of `s2`.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func chain<Base1: AsyncSequence, Base2: AsyncSequence>(_ s1: Base1, _ s2: Base2) -> AsyncChain2Sequence<Base1, Base2> where Base1.Element == Base2.Element {
|
public func chain<Base1: AsyncSequence, Base2: AsyncSequence>(
|
||||||
|
_ s1: Base1,
|
||||||
|
_ s2: Base2
|
||||||
|
) -> AsyncChain2Sequence<Base1, Base2> where Base1.Element == Base2.Element {
|
||||||
AsyncChain2Sequence(s1, s2)
|
AsyncChain2Sequence(s1, s2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A concatenation of two asynchronous sequences with the same element type.
|
/// A concatenation of two asynchronous sequences with the same element type.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct AsyncChain2Sequence<Base1: AsyncSequence, Base2: AsyncSequence> where Base1.Element == Base2.Element {
|
public struct AsyncChain2Sequence<Base1: AsyncSequence, Base2: AsyncSequence> where Base1.Element == Base2.Element {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base1: Base1
|
let base1: Base1
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base2: Base2
|
let base2: Base2
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(_ base1: Base1, _ base2: Base2) {
|
init(_ base1: Base1, _ base2: Base2) {
|
||||||
self.base1 = base1
|
self.base1 = base1
|
||||||
@@ -38,24 +43,26 @@ public struct AsyncChain2Sequence<Base1: AsyncSequence, Base2: AsyncSequence> wh
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncChain2Sequence: AsyncSequence {
|
extension AsyncChain2Sequence: AsyncSequence {
|
||||||
public typealias Element = Base1.Element
|
public typealias Element = Base1.Element
|
||||||
|
|
||||||
/// The iterator for a `AsyncChain2Sequence` instance.
|
/// The iterator for a `AsyncChain2Sequence` instance.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var base1: Base1.AsyncIterator?
|
var base1: Base1.AsyncIterator?
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var base2: Base2.AsyncIterator?
|
var base2: Base2.AsyncIterator?
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(_ base1: Base1.AsyncIterator, _ base2: Base2.AsyncIterator) {
|
init(_ base1: Base1.AsyncIterator, _ base2: Base2.AsyncIterator) {
|
||||||
self.base1 = base1
|
self.base1 = base1
|
||||||
self.base2 = base2
|
self.base2 = base2
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public mutating func next() async rethrows -> Element? {
|
public mutating func next() async rethrows -> Element? {
|
||||||
do {
|
do {
|
||||||
@@ -72,14 +79,16 @@ extension AsyncChain2Sequence: AsyncSequence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
Iterator(base1.makeAsyncIterator(), base2.makeAsyncIterator())
|
Iterator(base1.makeAsyncIterator(), base2.makeAsyncIterator())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncChain2Sequence: Sendable where Base1: Sendable, Base2: Sendable { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncChain2Sequence: Sendable where Base1: Sendable, Base2: Sendable {}
|
||||||
|
|
||||||
@available(*, unavailable)
|
@available(*, unavailable)
|
||||||
extension AsyncChain2Sequence.Iterator: Sendable { }
|
extension AsyncChain2Sequence.Iterator: Sendable {}
|
||||||
@@ -18,23 +18,30 @@
|
|||||||
/// - s3: The third asynchronous sequence.
|
/// - s3: The third asynchronous sequence.
|
||||||
/// - Returns: An asynchronous sequence that iterates first over the elements of `s1`, and
|
/// - Returns: An asynchronous sequence that iterates first over the elements of `s1`, and
|
||||||
/// then over the elements of `s2`, and then over the elements of `s3`
|
/// then over the elements of `s2`, and then over the elements of `s3`
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func chain<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>(_ s1: Base1, _ s2: Base2, _ s3: Base3) -> AsyncChain3Sequence<Base1, Base2, Base3> {
|
public func chain<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>(
|
||||||
|
_ s1: Base1,
|
||||||
|
_ s2: Base2,
|
||||||
|
_ s3: Base3
|
||||||
|
) -> AsyncChain3Sequence<Base1, Base2, Base3> {
|
||||||
AsyncChain3Sequence(s1, s2, s3)
|
AsyncChain3Sequence(s1, s2, s3)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A concatenation of three asynchronous sequences with the same element type.
|
/// A concatenation of three asynchronous sequences with the same element type.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct AsyncChain3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence> where Base1.Element == Base2.Element, Base1.Element == Base3.Element {
|
public struct AsyncChain3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>
|
||||||
|
where Base1.Element == Base2.Element, Base1.Element == Base3.Element {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base1: Base1
|
let base1: Base1
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base2: Base2
|
let base2: Base2
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base3: Base3
|
let base3: Base3
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(_ base1: Base1, _ base2: Base2, _ base3: Base3) {
|
init(_ base1: Base1, _ base2: Base2, _ base3: Base3) {
|
||||||
self.base1 = base1
|
self.base1 = base1
|
||||||
@@ -43,28 +50,30 @@ public struct AsyncChain3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Ba
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncChain3Sequence: AsyncSequence {
|
extension AsyncChain3Sequence: AsyncSequence {
|
||||||
public typealias Element = Base1.Element
|
public typealias Element = Base1.Element
|
||||||
|
|
||||||
/// The iterator for a `AsyncChain3Sequence` instance.
|
/// The iterator for a `AsyncChain3Sequence` instance.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var base1: Base1.AsyncIterator?
|
var base1: Base1.AsyncIterator?
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var base2: Base2.AsyncIterator?
|
var base2: Base2.AsyncIterator?
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var base3: Base3.AsyncIterator?
|
var base3: Base3.AsyncIterator?
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(_ base1: Base1.AsyncIterator, _ base2: Base2.AsyncIterator, _ base3: Base3.AsyncIterator) {
|
init(_ base1: Base1.AsyncIterator, _ base2: Base2.AsyncIterator, _ base3: Base3.AsyncIterator) {
|
||||||
self.base1 = base1
|
self.base1 = base1
|
||||||
self.base2 = base2
|
self.base2 = base2
|
||||||
self.base3 = base3
|
self.base3 = base3
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public mutating func next() async rethrows -> Element? {
|
public mutating func next() async rethrows -> Element? {
|
||||||
do {
|
do {
|
||||||
@@ -87,11 +96,16 @@ extension AsyncChain3Sequence: AsyncSequence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
Iterator(base1.makeAsyncIterator(), base2.makeAsyncIterator(), base3.makeAsyncIterator())
|
Iterator(base1.makeAsyncIterator(), base2.makeAsyncIterator(), base3.makeAsyncIterator())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncChain3Sequence: Sendable where Base1: Sendable, Base2: Sendable, Base3: Sendable { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncChain3Sequence: Sendable where Base1: Sendable, Base2: Sendable, Base3: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncChain3Sequence.Iterator: Sendable {}
|
||||||
@@ -1,275 +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
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
@preconcurrency @_implementationOnly import OrderedCollections
|
|
||||||
|
|
||||||
/// A channel for sending elements from one task to another with back pressure.
|
|
||||||
///
|
|
||||||
/// The `AsyncChannel` class is intended to be used as a communication type between tasks,
|
|
||||||
/// particularly when one task produces values and another task consumes those values. The back
|
|
||||||
/// pressure applied by `send(_:)` via the suspension/resume ensures that
|
|
||||||
/// the production of values does not exceed the consumption of values from iteration. This method
|
|
||||||
/// suspends after enqueuing the event and is resumed when the next call to `next()`
|
|
||||||
/// on the `Iterator` is made, or when `finish()` is called from another Task.
|
|
||||||
/// As `finish()` induces a terminal state, there is no need for a back pressure management.
|
|
||||||
/// This function does not suspend and will finish all the pending iterations.
|
|
||||||
public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
|
||||||
/// The iterator for a `AsyncChannel` instance.
|
|
||||||
public struct Iterator: AsyncIteratorProtocol, Sendable {
|
|
||||||
let channel: AsyncChannel<Element>
|
|
||||||
var active: Bool = true
|
|
||||||
|
|
||||||
init(_ channel: AsyncChannel<Element>) {
|
|
||||||
self.channel = channel
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Await the next sent element or finish.
|
|
||||||
public mutating func next() async -> Element? {
|
|
||||||
guard active else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
let generation = channel.establish()
|
|
||||||
let nextTokenStatus = ManagedCriticalState<ChannelTokenStatus>(.new)
|
|
||||||
|
|
||||||
let value = await withTaskCancellationHandler {
|
|
||||||
await channel.next(nextTokenStatus, generation)
|
|
||||||
} onCancel: { [channel] in
|
|
||||||
channel.cancelNext(nextTokenStatus, generation)
|
|
||||||
}
|
|
||||||
|
|
||||||
if let value {
|
|
||||||
return value
|
|
||||||
} else {
|
|
||||||
active = false
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
typealias Pending = ChannelToken<UnsafeContinuation<UnsafeContinuation<Element?, Never>?, Never>>
|
|
||||||
typealias Awaiting = ChannelToken<UnsafeContinuation<Element?, Never>>
|
|
||||||
|
|
||||||
struct ChannelToken<Continuation: Sendable>: Hashable, Sendable {
|
|
||||||
var generation: Int
|
|
||||||
var continuation: Continuation?
|
|
||||||
|
|
||||||
init(generation: Int, continuation: Continuation) {
|
|
||||||
self.generation = generation
|
|
||||||
self.continuation = continuation
|
|
||||||
}
|
|
||||||
|
|
||||||
init(placeholder generation: Int) {
|
|
||||||
self.generation = generation
|
|
||||||
self.continuation = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func hash(into hasher: inout Hasher) {
|
|
||||||
hasher.combine(generation)
|
|
||||||
}
|
|
||||||
|
|
||||||
static func == (_ lhs: ChannelToken, _ rhs: ChannelToken) -> Bool {
|
|
||||||
return lhs.generation == rhs.generation
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum ChannelTokenStatus: Equatable {
|
|
||||||
case new
|
|
||||||
case cancelled
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Emission : Sendable {
|
|
||||||
case idle
|
|
||||||
case pending(OrderedSet<Pending>)
|
|
||||||
case awaiting(OrderedSet<Awaiting>)
|
|
||||||
case finished
|
|
||||||
}
|
|
||||||
|
|
||||||
struct State : Sendable {
|
|
||||||
var emission: Emission = .idle
|
|
||||||
var generation = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
let state = ManagedCriticalState(State())
|
|
||||||
|
|
||||||
/// Create a new `AsyncChannel` given an element type.
|
|
||||||
public init(element elementType: Element.Type = Element.self) { }
|
|
||||||
|
|
||||||
func establish() -> Int {
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
defer { state.generation &+= 1 }
|
|
||||||
return state.generation
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func cancelNext(_ nextTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) {
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
let continuation: UnsafeContinuation<Element?, Never>?
|
|
||||||
|
|
||||||
switch state.emission {
|
|
||||||
case .awaiting(var nexts):
|
|
||||||
continuation = nexts.remove(Awaiting(placeholder: generation))?.continuation
|
|
||||||
if nexts.isEmpty {
|
|
||||||
state.emission = .idle
|
|
||||||
} else {
|
|
||||||
state.emission = .awaiting(nexts)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
continuation = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
nextTokenStatus.withCriticalRegion { status in
|
|
||||||
if status == .new {
|
|
||||||
status = .cancelled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
continuation?.resume(returning: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func next(_ nextTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) async -> Element? {
|
|
||||||
return await withUnsafeContinuation { (continuation: UnsafeContinuation<Element?, Never>) in
|
|
||||||
var cancelled = false
|
|
||||||
var terminal = false
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
|
|
||||||
if nextTokenStatus.withCriticalRegion({ $0 }) == .cancelled {
|
|
||||||
cancelled = true
|
|
||||||
}
|
|
||||||
|
|
||||||
switch state.emission {
|
|
||||||
case .idle:
|
|
||||||
state.emission = .awaiting([Awaiting(generation: generation, continuation: continuation)])
|
|
||||||
case .pending(var sends):
|
|
||||||
let send = sends.removeFirst()
|
|
||||||
if sends.count == 0 {
|
|
||||||
state.emission = .idle
|
|
||||||
} else {
|
|
||||||
state.emission = .pending(sends)
|
|
||||||
}
|
|
||||||
send.continuation?.resume(returning: continuation)
|
|
||||||
case .awaiting(var nexts):
|
|
||||||
nexts.updateOrAppend(Awaiting(generation: generation, continuation: continuation))
|
|
||||||
state.emission = .awaiting(nexts)
|
|
||||||
case .finished:
|
|
||||||
terminal = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if cancelled || terminal {
|
|
||||||
continuation.resume(returning: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func cancelSend(_ sendTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) {
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
let continuation: UnsafeContinuation<UnsafeContinuation<Element?, Never>?, Never>?
|
|
||||||
|
|
||||||
switch state.emission {
|
|
||||||
case .pending(var sends):
|
|
||||||
let send = sends.remove(Pending(placeholder: generation))
|
|
||||||
if sends.isEmpty {
|
|
||||||
state.emission = .idle
|
|
||||||
} else {
|
|
||||||
state.emission = .pending(sends)
|
|
||||||
}
|
|
||||||
continuation = send?.continuation
|
|
||||||
default:
|
|
||||||
continuation = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
sendTokenStatus.withCriticalRegion { status in
|
|
||||||
if status == .new {
|
|
||||||
status = .cancelled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
continuation?.resume(returning: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func send(_ sendTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int, _ element: Element) async {
|
|
||||||
let continuation: UnsafeContinuation<Element?, Never>? = await withUnsafeContinuation { continuation in
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
|
|
||||||
if sendTokenStatus.withCriticalRegion({ $0 }) == .cancelled {
|
|
||||||
continuation.resume(returning: nil)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch state.emission {
|
|
||||||
case .idle:
|
|
||||||
state.emission = .pending([Pending(generation: generation, continuation: continuation)])
|
|
||||||
case .pending(var sends):
|
|
||||||
sends.updateOrAppend(Pending(generation: generation, continuation: continuation))
|
|
||||||
state.emission = .pending(sends)
|
|
||||||
case .awaiting(var nexts):
|
|
||||||
let next = nexts.removeFirst().continuation
|
|
||||||
if nexts.count == 0 {
|
|
||||||
state.emission = .idle
|
|
||||||
} else {
|
|
||||||
state.emission = .awaiting(nexts)
|
|
||||||
}
|
|
||||||
continuation.resume(returning: next)
|
|
||||||
case .finished:
|
|
||||||
continuation.resume(returning: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continuation?.resume(returning: element)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send an element to an awaiting iteration. This function will resume when the next call to `next()` is made
|
|
||||||
/// or when a call to `finish()` is made from another Task.
|
|
||||||
/// If the channel is already finished then this returns immediately
|
|
||||||
/// If the task is cancelled, this function will resume. Other sending operations from other tasks will remain active.
|
|
||||||
public func send(_ element: Element) async {
|
|
||||||
let generation = establish()
|
|
||||||
let sendTokenStatus = ManagedCriticalState<ChannelTokenStatus>(.new)
|
|
||||||
|
|
||||||
await withTaskCancellationHandler {
|
|
||||||
await send(sendTokenStatus, generation, element)
|
|
||||||
} onCancel: { [weak self] in
|
|
||||||
self?.cancelSend(sendTokenStatus, generation)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send a finish to all awaiting iterations.
|
|
||||||
/// All subsequent calls to `next(_:)` will resume immediately.
|
|
||||||
public func finish() {
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
|
|
||||||
defer { state.emission = .finished }
|
|
||||||
|
|
||||||
switch state.emission {
|
|
||||||
case .pending(let sends):
|
|
||||||
for send in sends {
|
|
||||||
send.continuation?.resume(returning: nil)
|
|
||||||
}
|
|
||||||
case .awaiting(let nexts):
|
|
||||||
for next in nexts {
|
|
||||||
next.continuation?.resume(returning: nil)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create an `Iterator` for iteration of an `AsyncChannel`
|
|
||||||
public func makeAsyncIterator() -> Iterator {
|
|
||||||
return Iterator(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,17 +9,25 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection`
|
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection`
|
||||||
/// type by testing if elements belong in the same group.
|
/// type by testing if elements belong in the same group.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func chunked<Collected: RangeReplaceableCollection>(into: Collected.Type, by belongInSameGroup: @escaping @Sendable (Element, Element) -> Bool) -> AsyncChunkedByGroupSequence<Self, Collected> where Collected.Element == Element {
|
public func chunked<Collected: RangeReplaceableCollection>(
|
||||||
|
into: Collected.Type,
|
||||||
|
by belongInSameGroup: @escaping @Sendable (Element, Element) -> Bool
|
||||||
|
) -> AsyncChunkedByGroupSequence<Self, Collected> where Collected.Element == Element {
|
||||||
AsyncChunkedByGroupSequence(self, grouping: belongInSameGroup)
|
AsyncChunkedByGroupSequence(self, grouping: belongInSameGroup)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that creates chunks by testing if elements belong in the same group.
|
/// Creates an asynchronous sequence that creates chunks by testing if elements belong in the same group.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func chunked(by belongInSameGroup: @escaping @Sendable (Element, Element) -> Bool) -> AsyncChunkedByGroupSequence<Self, [Element]> {
|
public func chunked(
|
||||||
|
by belongInSameGroup: @escaping @Sendable (Element, Element) -> Bool
|
||||||
|
) -> AsyncChunkedByGroupSequence<Self, [Element]> {
|
||||||
chunked(into: [Element].self, by: belongInSameGroup)
|
chunked(into: [Element].self, by: belongInSameGroup)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,7 +54,9 @@ extension AsyncSequence {
|
|||||||
/// // [10, 20, 30]
|
/// // [10, 20, 30]
|
||||||
/// // [10, 40, 40]
|
/// // [10, 40, 40]
|
||||||
/// // [10, 20]
|
/// // [10, 20]
|
||||||
public struct AsyncChunkedByGroupSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection>: AsyncSequence where Collected.Element == Base.Element {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct AsyncChunkedByGroupSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection>: AsyncSequence
|
||||||
|
where Collected.Element == Base.Element {
|
||||||
public typealias Element = Collected
|
public typealias Element = Collected
|
||||||
|
|
||||||
/// The iterator for a `AsyncChunkedByGroupSequence` instance.
|
/// The iterator for a `AsyncChunkedByGroupSequence` instance.
|
||||||
@@ -76,7 +86,7 @@ public struct AsyncChunkedByGroupSequence<Base: AsyncSequence, Collected: RangeR
|
|||||||
} else {
|
} else {
|
||||||
hangingNext = nil
|
hangingNext = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let first = firstOpt else {
|
guard let first = firstOpt else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -86,23 +96,22 @@ public struct AsyncChunkedByGroupSequence<Base: AsyncSequence, Collected: RangeR
|
|||||||
|
|
||||||
var prev = first
|
var prev = first
|
||||||
while let next = try await base.next() {
|
while let next = try await base.next() {
|
||||||
if grouping(prev, next) {
|
guard grouping(prev, next) else {
|
||||||
result.append(next)
|
|
||||||
prev = next
|
|
||||||
} else {
|
|
||||||
hangingNext = next
|
hangingNext = next
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
result.append(next)
|
||||||
|
prev = next
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base : Base
|
let base: Base
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let grouping : @Sendable (Base.Element, Base.Element) -> Bool
|
let grouping: @Sendable (Base.Element, Base.Element) -> Bool
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(_ base: Base, grouping: @escaping @Sendable (Base.Element, Base.Element) -> Bool) {
|
init(_ base: Base, grouping: @escaping @Sendable (Base.Element, Base.Element) -> Bool) {
|
||||||
@@ -116,5 +125,8 @@ public struct AsyncChunkedByGroupSequence<Base: AsyncSequence, Collected: RangeR
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncChunkedByGroupSequence : Sendable where Base : Sendable, Base.Element : Sendable { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncChunkedByGroupSequence.Iterator : Sendable where Base.AsyncIterator : Sendable, Base.Element : Sendable { }
|
extension AsyncChunkedByGroupSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncChunkedByGroupSequence.Iterator: Sendable {}
|
||||||
@@ -9,22 +9,35 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type on the uniqueness of a given subject.
|
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type on the uniqueness of a given subject.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func chunked<Subject : Equatable, Collected: RangeReplaceableCollection>(into: Collected.Type, on projection: @escaping @Sendable (Element) -> Subject) -> AsyncChunkedOnProjectionSequence<Self, Subject, Collected> {
|
public func chunked<Subject: Equatable, Collected: RangeReplaceableCollection>(
|
||||||
|
into: Collected.Type,
|
||||||
|
on projection: @escaping @Sendable (Element) -> Subject
|
||||||
|
) -> AsyncChunkedOnProjectionSequence<Self, Subject, Collected> {
|
||||||
AsyncChunkedOnProjectionSequence(self, projection: projection)
|
AsyncChunkedOnProjectionSequence(self, projection: projection)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that creates chunks on the uniqueness of a given subject.
|
/// Creates an asynchronous sequence that creates chunks on the uniqueness of a given subject.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func chunked<Subject : Equatable>(on projection: @escaping @Sendable (Element) -> Subject) -> AsyncChunkedOnProjectionSequence<Self, Subject, [Element]> {
|
public func chunked<Subject: Equatable>(
|
||||||
|
on projection: @escaping @Sendable (Element) -> Subject
|
||||||
|
) -> AsyncChunkedOnProjectionSequence<Self, Subject, [Element]> {
|
||||||
chunked(into: [Element].self, on: projection)
|
chunked(into: [Element].self, on: projection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An `AsyncSequence` that chunks on a subject when it differs from the last element.
|
/// An `AsyncSequence` that chunks on a subject when it differs from the last element.
|
||||||
public struct AsyncChunkedOnProjectionSequence<Base: AsyncSequence, Subject: Equatable, Collected: RangeReplaceableCollection>: AsyncSequence where Collected.Element == Base.Element {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct AsyncChunkedOnProjectionSequence<
|
||||||
|
Base: AsyncSequence,
|
||||||
|
Subject: Equatable,
|
||||||
|
Collected: RangeReplaceableCollection
|
||||||
|
>: AsyncSequence where Collected.Element == Base.Element {
|
||||||
public typealias Element = (Subject, Collected)
|
public typealias Element = (Subject, Collected)
|
||||||
|
|
||||||
/// The iterator for a `AsyncChunkedOnProjectionSequence` instance.
|
/// The iterator for a `AsyncChunkedOnProjectionSequence` instance.
|
||||||
@@ -67,22 +80,21 @@ public struct AsyncChunkedOnProjectionSequence<Base: AsyncSequence, Subject: Equ
|
|||||||
|
|
||||||
while let next = try await base.next() {
|
while let next = try await base.next() {
|
||||||
let subj = projection(next)
|
let subj = projection(next)
|
||||||
if subj == first.0 {
|
guard subj == first.0 else {
|
||||||
result.append(next)
|
|
||||||
} else {
|
|
||||||
hangingNext = (subj, next)
|
hangingNext = (subj, next)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
result.append(next)
|
||||||
}
|
}
|
||||||
return (first.0, result)
|
return (first.0, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base : Base
|
let base: Base
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let projection : @Sendable (Base.Element) -> Subject
|
let projection: @Sendable (Base.Element) -> Subject
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(_ base: Base, projection: @escaping @Sendable (Base.Element) -> Subject) {
|
init(_ base: Base, projection: @escaping @Sendable (Base.Element) -> Subject) {
|
||||||
@@ -96,5 +108,8 @@ public struct AsyncChunkedOnProjectionSequence<Base: AsyncSequence, Subject: Equ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncChunkedOnProjectionSequence : Sendable where Base : Sendable, Base.Element : Sendable { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncChunkedOnProjectionSequence.Iterator : Sendable where Base.AsyncIterator : Sendable, Base.Element : Sendable, Subject : Sendable { }
|
extension AsyncChunkedOnProjectionSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncChunkedOnProjectionSequence.Iterator: Sendable {}
|
||||||
@@ -9,54 +9,93 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type of a given count or when a signal `AsyncSequence` produces an element.
|
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type of a given count or when a signal `AsyncSequence` produces an element.
|
||||||
public func chunks<Signal, Collected: RangeReplaceableCollection>(ofCount count: Int, or signal: Signal, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, Signal> where Collected.Element == Element {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public func chunks<Signal, Collected: RangeReplaceableCollection>(
|
||||||
|
ofCount count: Int,
|
||||||
|
or signal: Signal,
|
||||||
|
into: Collected.Type
|
||||||
|
) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, Signal> where Collected.Element == Element {
|
||||||
AsyncChunksOfCountOrSignalSequence(self, count: count, signal: signal)
|
AsyncChunksOfCountOrSignalSequence(self, count: count, signal: signal)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that creates chunks of a given count or when a signal `AsyncSequence` produces an element.
|
/// Creates an asynchronous sequence that creates chunks of a given count or when a signal `AsyncSequence` produces an element.
|
||||||
public func chunks<Signal>(ofCount count: Int, or signal: Signal) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], Signal> {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public func chunks<Signal>(
|
||||||
|
ofCount count: Int,
|
||||||
|
or signal: Signal
|
||||||
|
) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], Signal> {
|
||||||
chunks(ofCount: count, or: signal, into: [Element].self)
|
chunks(ofCount: count, or: signal, into: [Element].self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type when a signal `AsyncSequence` produces an element.
|
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type when a signal `AsyncSequence` produces an element.
|
||||||
public func chunked<Signal, Collected: RangeReplaceableCollection>(by signal: Signal, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, Signal> where Collected.Element == Element {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public func chunked<Signal, Collected: RangeReplaceableCollection>(
|
||||||
|
by signal: Signal,
|
||||||
|
into: Collected.Type
|
||||||
|
) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, Signal> where Collected.Element == Element {
|
||||||
AsyncChunksOfCountOrSignalSequence(self, count: nil, signal: signal)
|
AsyncChunksOfCountOrSignalSequence(self, count: nil, signal: signal)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that creates chunks when a signal `AsyncSequence` produces an element.
|
/// Creates an asynchronous sequence that creates chunks when a signal `AsyncSequence` produces an element.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public func chunked<Signal>(by signal: Signal) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], Signal> {
|
public func chunked<Signal>(by signal: Signal) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], Signal> {
|
||||||
chunked(by: signal, into: [Element].self)
|
chunked(by: signal, into: [Element].self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type of a given count or when an `AsyncTimerSequence` fires.
|
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type of a given count or when an `AsyncTimerSequence` fires.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public func chunks<C: Clock, Collected: RangeReplaceableCollection>(ofCount count: Int, or timer: AsyncTimerSequence<C>, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, AsyncTimerSequence<C>> where Collected.Element == Element {
|
public func chunks<C: Clock, Collected: RangeReplaceableCollection>(
|
||||||
|
ofCount count: Int,
|
||||||
|
or timer: AsyncTimerSequence<C>,
|
||||||
|
into: Collected.Type
|
||||||
|
) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, AsyncTimerSequence<C>> where Collected.Element == Element {
|
||||||
AsyncChunksOfCountOrSignalSequence(self, count: count, signal: timer)
|
AsyncChunksOfCountOrSignalSequence(self, count: count, signal: timer)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that creates chunks of a given count or when an `AsyncTimerSequence` fires.
|
/// Creates an asynchronous sequence that creates chunks of a given count or when an `AsyncTimerSequence` fires.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public func chunks<C: Clock>(ofCount count: Int, or timer: AsyncTimerSequence<C>) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], AsyncTimerSequence<C>> {
|
public func chunks<C: Clock>(
|
||||||
|
ofCount count: Int,
|
||||||
|
or timer: AsyncTimerSequence<C>
|
||||||
|
) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], AsyncTimerSequence<C>> {
|
||||||
chunks(ofCount: count, or: timer, into: [Element].self)
|
chunks(ofCount: count, or: timer, into: [Element].self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type when an `AsyncTimerSequence` fires.
|
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type when an `AsyncTimerSequence` fires.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public func chunked<C: Clock, Collected: RangeReplaceableCollection>(by timer: AsyncTimerSequence<C>, into: Collected.Type) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, AsyncTimerSequence<C>> where Collected.Element == Element {
|
public func chunked<C: Clock, Collected: RangeReplaceableCollection>(
|
||||||
|
by timer: AsyncTimerSequence<C>,
|
||||||
|
into: Collected.Type
|
||||||
|
) -> AsyncChunksOfCountOrSignalSequence<Self, Collected, AsyncTimerSequence<C>> where Collected.Element == Element {
|
||||||
AsyncChunksOfCountOrSignalSequence(self, count: nil, signal: timer)
|
AsyncChunksOfCountOrSignalSequence(self, count: nil, signal: timer)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that creates chunks when an `AsyncTimerSequence` fires.
|
/// Creates an asynchronous sequence that creates chunks when an `AsyncTimerSequence` fires.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public func chunked<C: Clock>(by timer: AsyncTimerSequence<C>) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], AsyncTimerSequence<C>> {
|
public func chunked<C: Clock>(
|
||||||
|
by timer: AsyncTimerSequence<C>
|
||||||
|
) -> AsyncChunksOfCountOrSignalSequence<Self, [Element], AsyncTimerSequence<C>> {
|
||||||
chunked(by: timer, into: [Element].self)
|
chunked(by: timer, into: [Element].self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An `AsyncSequence` that chunks elements into collected `RangeReplaceableCollection` instances by either count or a signal from another `AsyncSequence`.
|
/// An `AsyncSequence` that chunks elements into collected `RangeReplaceableCollection` instances by either count or a signal from another `AsyncSequence`.
|
||||||
public struct AsyncChunksOfCountOrSignalSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection, Signal: AsyncSequence>: AsyncSequence, Sendable where Collected.Element == Base.Element, Base: Sendable, Signal: Sendable, Base.Element: Sendable, Signal.Element: Sendable {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct AsyncChunksOfCountOrSignalSequence<
|
||||||
|
Base: AsyncSequence,
|
||||||
|
Collected: RangeReplaceableCollection,
|
||||||
|
Signal: AsyncSequence
|
||||||
|
>: AsyncSequence, Sendable
|
||||||
|
where
|
||||||
|
Collected.Element == Base.Element,
|
||||||
|
Base: Sendable,
|
||||||
|
Signal: Sendable,
|
||||||
|
Base.Element: Sendable,
|
||||||
|
Signal.Element: Sendable
|
||||||
|
{
|
||||||
|
|
||||||
public typealias Element = Collected
|
public typealias Element = Collected
|
||||||
|
|
||||||
@@ -65,23 +104,23 @@ public struct AsyncChunksOfCountOrSignalSequence<Base: AsyncSequence, Collected:
|
|||||||
case terminal
|
case terminal
|
||||||
case signal
|
case signal
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The iterator for a `AsyncChunksOfCountOrSignalSequence` instance.
|
/// The iterator for a `AsyncChunksOfCountOrSignalSequence` instance.
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
typealias EitherMappedBase = AsyncMapSequence<Base, Either>
|
typealias EitherMappedBase = AsyncMapSequence<Base, Either>
|
||||||
typealias EitherMappedSignal = AsyncMapSequence<Signal, Either>
|
typealias EitherMappedSignal = AsyncMapSequence<Signal, Either>
|
||||||
typealias ChainedBase = AsyncChain2Sequence<EitherMappedBase, AsyncLazySequence<[Either]>>
|
typealias ChainedBase = AsyncChain2Sequence<EitherMappedBase, AsyncSyncSequence<[Either]>>
|
||||||
typealias Merged = AsyncMerge2Sequence<ChainedBase, EitherMappedSignal>
|
typealias Merged = AsyncMerge2Sequence<ChainedBase, EitherMappedSignal>
|
||||||
|
|
||||||
let count: Int?
|
let count: Int?
|
||||||
var iterator: Merged.AsyncIterator
|
var iterator: Merged.AsyncIterator
|
||||||
var terminated = false
|
var terminated = false
|
||||||
|
|
||||||
init(iterator: Merged.AsyncIterator, count: Int?) {
|
init(iterator: Merged.AsyncIterator, count: Int?) {
|
||||||
self.count = count
|
self.count = count
|
||||||
self.iterator = iterator
|
self.iterator = iterator
|
||||||
}
|
}
|
||||||
|
|
||||||
public mutating func next() async rethrows -> Collected? {
|
public mutating func next() async rethrows -> Collected? {
|
||||||
guard !terminated else {
|
guard !terminated else {
|
||||||
return nil
|
return nil
|
||||||
@@ -124,7 +163,16 @@ public struct AsyncChunksOfCountOrSignalSequence<Base: AsyncSequence, Collected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
|
|
||||||
return Iterator(iterator: merge(chain(base.map { Either.element($0) }, [.terminal].async), signal.map { _ in Either.signal }).makeAsyncIterator(), count: count)
|
return Iterator(
|
||||||
|
iterator: merge(
|
||||||
|
chain(base.map { Either.element($0) }, [.terminal].async),
|
||||||
|
signal.map { _ in Either.signal }
|
||||||
|
).makeAsyncIterator(),
|
||||||
|
count: count
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncChunksOfCountOrSignalSequence.Iterator: Sendable {}
|
||||||
@@ -9,14 +9,20 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` of a given count.
|
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` of a given count.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func chunks<Collected: RangeReplaceableCollection>(ofCount count: Int, into: Collected.Type) -> AsyncChunksOfCountSequence<Self, Collected> where Collected.Element == Element {
|
public func chunks<Collected: RangeReplaceableCollection>(
|
||||||
|
ofCount count: Int,
|
||||||
|
into: Collected.Type
|
||||||
|
) -> AsyncChunksOfCountSequence<Self, Collected> where Collected.Element == Element {
|
||||||
AsyncChunksOfCountSequence(self, count: count)
|
AsyncChunksOfCountSequence(self, count: count)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that creates chunks of a given count.
|
/// Creates an asynchronous sequence that creates chunks of a given count.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func chunks(ofCount count: Int) -> AsyncChunksOfCountSequence<Self, [Element]> {
|
public func chunks(ofCount count: Int) -> AsyncChunksOfCountSequence<Self, [Element]> {
|
||||||
chunks(ofCount: count, into: [Element].self)
|
chunks(ofCount: count, into: [Element].self)
|
||||||
@@ -24,7 +30,9 @@ extension AsyncSequence {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// An `AsyncSequence` that chunks elements into `RangeReplaceableCollection` instances of at least a given count.
|
/// An `AsyncSequence` that chunks elements into `RangeReplaceableCollection` instances of at least a given count.
|
||||||
public struct AsyncChunksOfCountSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection>: AsyncSequence where Collected.Element == Base.Element {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct AsyncChunksOfCountSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection>: AsyncSequence
|
||||||
|
where Collected.Element == Base.Element {
|
||||||
public typealias Element = Collected
|
public typealias Element = Collected
|
||||||
|
|
||||||
/// The iterator for a `AsyncChunksOfCountSequence` instance.
|
/// The iterator for a `AsyncChunksOfCountSequence` instance.
|
||||||
@@ -49,6 +57,10 @@ public struct AsyncChunksOfCountSequence<Base: AsyncSequence, Collected: RangeRe
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if count == 1 {
|
||||||
|
return Collected(CollectionOfOne(first))
|
||||||
|
}
|
||||||
|
|
||||||
var result: Collected = .init()
|
var result: Collected = .init()
|
||||||
result.append(first)
|
result.append(first)
|
||||||
|
|
||||||
@@ -63,10 +75,10 @@ public struct AsyncChunksOfCountSequence<Base: AsyncSequence, Collected: RangeRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base : Base
|
let base: Base
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let count : Int
|
let count: Int
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(_ base: Base, count: Int) {
|
init(_ base: Base, count: Int) {
|
||||||
@@ -81,5 +93,7 @@ public struct AsyncChunksOfCountSequence<Base: AsyncSequence, Collected: RangeRe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncChunksOfCountSequence : Sendable where Base : Sendable, Base.Element : Sendable { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncChunksOfCountSequence.Iterator : Sendable where Base.AsyncIterator : Sendable, Base.Element : Sendable { }
|
extension AsyncChunksOfCountSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncChunksOfCountSequence.Iterator: Sendable where Base.AsyncIterator: Sendable, Base.Element: Sendable {}
|
||||||
@@ -9,15 +9,34 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncSequence {
|
||||||
|
/// Returns a new `AsyncSequence` that iterates over every non-nil element from the
|
||||||
|
/// original `AsyncSequence`.
|
||||||
|
///
|
||||||
|
/// Produces the same result as `c.compactMap { $0 }`.
|
||||||
|
///
|
||||||
|
/// - Returns: An `AsyncSequence` where the element is the unwrapped original
|
||||||
|
/// element and iterates over every non-nil element from the original
|
||||||
|
/// `AsyncSequence`.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
@inlinable
|
||||||
|
public func compacted<Unwrapped>() -> AsyncCompactedSequence<Self, Unwrapped>
|
||||||
|
where Element == Unwrapped? {
|
||||||
|
AsyncCompactedSequence(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An `AsyncSequence` that iterates over every non-nil element from the original
|
/// An `AsyncSequence` that iterates over every non-nil element from the original
|
||||||
/// `AsyncSequence`.
|
/// `AsyncSequence`.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct AsyncCompactedSequence<Base: AsyncSequence, Element>: AsyncSequence
|
public struct AsyncCompactedSequence<Base: AsyncSequence, Element>: AsyncSequence
|
||||||
where Base.Element == Element? {
|
where Base.Element == Element? {
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base: Base
|
let base: Base
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
init(_ base: Base) {
|
init(_ base: Base) {
|
||||||
self.base = base
|
self.base = base
|
||||||
@@ -28,12 +47,12 @@ public struct AsyncCompactedSequence<Base: AsyncSequence, Element>: AsyncSequenc
|
|||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var base: Base.AsyncIterator
|
var base: Base.AsyncIterator
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
init(_ base: Base.AsyncIterator) {
|
init(_ base: Base.AsyncIterator) {
|
||||||
self.base = base
|
self.base = base
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public mutating func next() async rethrows -> Element? {
|
public mutating func next() async rethrows -> Element? {
|
||||||
while let wrapped = try await base.next() {
|
while let wrapped = try await base.next() {
|
||||||
@@ -50,22 +69,8 @@ public struct AsyncCompactedSequence<Base: AsyncSequence, Element>: AsyncSequenc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncSequence {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
/// Returns a new `AsyncSequence` that iterates over every non-nil element from the
|
extension AsyncCompactedSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
/// original `AsyncSequence`.
|
|
||||||
///
|
|
||||||
/// Produces the same result as `c.compactMap { $0 }`.
|
|
||||||
///
|
|
||||||
/// - Returns: An `AsyncSequence` where the element is the unwrapped original
|
|
||||||
/// element and iterates over every non-nil element from the original
|
|
||||||
/// `AsyncSequence`.
|
|
||||||
///
|
|
||||||
/// Complexity: O(1)
|
|
||||||
@inlinable
|
|
||||||
public func compacted<Unwrapped>() -> AsyncCompactedSequence<Self, Unwrapped>
|
|
||||||
where Element == Unwrapped? {
|
|
||||||
AsyncCompactedSequence(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension AsyncCompactedSequence: Sendable where Base: Sendable, Base.Element: Sendable { }
|
@available(*, unavailable)
|
||||||
|
extension AsyncCompactedSequence.Iterator: Sendable {}
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
||||||
/// elements of the asynchronous sequence using the given closure.
|
/// elements of the asynchronous sequence using the given closure.
|
||||||
@@ -22,13 +23,17 @@ extension AsyncSequence {
|
|||||||
/// the next element in the receiving asynchronous sequence, which it returns.
|
/// the next element in the receiving asynchronous sequence, which it returns.
|
||||||
/// - Returns: An asynchronous sequence of the initial value followed by the reduced
|
/// - Returns: An asynchronous sequence of the initial value followed by the reduced
|
||||||
/// elements.
|
/// elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func reductions<Result>(_ initial: Result, _ transform: @Sendable @escaping (Result, Element) async -> Result) -> AsyncExclusiveReductionsSequence<Self, Result> {
|
public func reductions<Result>(
|
||||||
|
_ initial: Result,
|
||||||
|
_ transform: @Sendable @escaping (Result, Element) async -> Result
|
||||||
|
) -> AsyncExclusiveReductionsSequence<Self, Result> {
|
||||||
reductions(into: initial) { result, element in
|
reductions(into: initial) { result, element in
|
||||||
result = await transform(result, element)
|
result = await transform(result, element)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
||||||
/// elements of the asynchronous sequence using the given closure.
|
/// elements of the asynchronous sequence using the given closure.
|
||||||
///
|
///
|
||||||
@@ -42,25 +47,30 @@ extension AsyncSequence {
|
|||||||
/// previous result instead of returning a value.
|
/// previous result instead of returning a value.
|
||||||
/// - Returns: An asynchronous sequence of the initial value followed by the reduced
|
/// - Returns: An asynchronous sequence of the initial value followed by the reduced
|
||||||
/// elements.
|
/// elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func reductions<Result>(into initial: Result, _ transform: @Sendable @escaping (inout Result, Element) async -> Void) -> AsyncExclusiveReductionsSequence<Self, Result> {
|
public func reductions<Result>(
|
||||||
|
into initial: Result,
|
||||||
|
_ transform: @Sendable @escaping (inout Result, Element) async -> Void
|
||||||
|
) -> AsyncExclusiveReductionsSequence<Self, Result> {
|
||||||
AsyncExclusiveReductionsSequence(self, initial: initial, transform: transform)
|
AsyncExclusiveReductionsSequence(self, initial: initial, transform: transform)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An asynchronous sequence of applying a transform to the element of an asynchronous sequence and the
|
/// An asynchronous sequence of applying a transform to the element of an asynchronous sequence and the
|
||||||
/// previously transformed result.
|
/// previously transformed result.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct AsyncExclusiveReductionsSequence<Base: AsyncSequence, Element> {
|
public struct AsyncExclusiveReductionsSequence<Base: AsyncSequence, Element> {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base: Base
|
let base: Base
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let initial: Element
|
let initial: Element
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let transform: @Sendable (inout Element, Base.Element) async -> Void
|
let transform: @Sendable (inout Element, Base.Element) async -> Void
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
init(_ base: Base, initial: Element, transform: @Sendable @escaping (inout Element, Base.Element) async -> Void) {
|
init(_ base: Base, initial: Element, transform: @Sendable @escaping (inout Element, Base.Element) async -> Void) {
|
||||||
self.base = base
|
self.base = base
|
||||||
@@ -69,46 +79,55 @@ public struct AsyncExclusiveReductionsSequence<Base: AsyncSequence, Element> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncExclusiveReductionsSequence: AsyncSequence {
|
extension AsyncExclusiveReductionsSequence: AsyncSequence {
|
||||||
/// The iterator for an `AsyncExclusiveReductionsSequence` instance.
|
/// The iterator for an `AsyncExclusiveReductionsSequence` instance.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var iterator: Base.AsyncIterator
|
var iterator: Base.AsyncIterator
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var current: Element?
|
var current: Element?
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let transform: @Sendable (inout Element, Base.Element) async -> Void
|
let transform: @Sendable (inout Element, Base.Element) async -> Void
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
init(_ iterator: Base.AsyncIterator, initial: Element, transform: @Sendable @escaping (inout Element, Base.Element) async -> Void) {
|
init(
|
||||||
|
_ iterator: Base.AsyncIterator,
|
||||||
|
initial: Element,
|
||||||
|
transform: @Sendable @escaping (inout Element, Base.Element) async -> Void
|
||||||
|
) {
|
||||||
self.iterator = iterator
|
self.iterator = iterator
|
||||||
self.current = initial
|
self.current = initial
|
||||||
self.transform = transform
|
self.transform = transform
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public mutating func next() async rethrows -> Element? {
|
public mutating func next() async rethrows -> Element? {
|
||||||
guard let result = current else { return nil }
|
guard var result = current else { return nil }
|
||||||
let value = try await iterator.next()
|
let value = try await iterator.next()
|
||||||
if let value = value {
|
guard let value = value else {
|
||||||
var result = result
|
|
||||||
await transform(&result, value)
|
|
||||||
current = result
|
|
||||||
return result
|
|
||||||
} else {
|
|
||||||
current = nil
|
current = nil
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
await transform(&result, value)
|
||||||
|
current = result
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
Iterator(base.makeAsyncIterator(), initial: initial, transform: transform)
|
Iterator(base.makeAsyncIterator(), initial: initial, transform: transform)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncExclusiveReductionsSequence: Sendable where Base: Sendable, Element: Sendable { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncExclusiveReductionsSequence: Sendable where Base: Sendable, Element: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncExclusiveReductionsSequence.Iterator: Sendable {}
|
||||||
@@ -1,3 +1,15 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
||||||
/// elements of the asynchronous sequence using the given closure.
|
/// elements of the asynchronous sequence using the given closure.
|
||||||
@@ -17,6 +29,7 @@ extension AsyncSequence {
|
|||||||
/// result and the next element in the receiving sequence, and returns
|
/// result and the next element in the receiving sequence, and returns
|
||||||
/// the result.
|
/// the result.
|
||||||
/// - Returns: An asynchronous sequence of the reduced elements.
|
/// - Returns: An asynchronous sequence of the reduced elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func reductions(
|
public func reductions(
|
||||||
_ transform: @Sendable @escaping (Element, Element) async -> Element
|
_ transform: @Sendable @escaping (Element, Element) async -> Element
|
||||||
@@ -27,14 +40,15 @@ extension AsyncSequence {
|
|||||||
|
|
||||||
/// An asynchronous sequence containing the accumulated results of combining the
|
/// An asynchronous sequence containing the accumulated results of combining the
|
||||||
/// elements of the asynchronous sequence using a given closure.
|
/// elements of the asynchronous sequence using a given closure.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct AsyncInclusiveReductionsSequence<Base: AsyncSequence> {
|
public struct AsyncInclusiveReductionsSequence<Base: AsyncSequence> {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base: Base
|
let base: Base
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let transform: @Sendable (Base.Element, Base.Element) async -> Base.Element
|
let transform: @Sendable (Base.Element, Base.Element) async -> Base.Element
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
init(_ base: Base, transform: @Sendable @escaping (Base.Element, Base.Element) async -> Base.Element) {
|
init(_ base: Base, transform: @Sendable @escaping (Base.Element, Base.Element) async -> Base.Element) {
|
||||||
self.base = base
|
self.base = base
|
||||||
@@ -42,10 +56,12 @@ public struct AsyncInclusiveReductionsSequence<Base: AsyncSequence> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncInclusiveReductionsSequence: AsyncSequence {
|
extension AsyncInclusiveReductionsSequence: AsyncSequence {
|
||||||
public typealias Element = Base.Element
|
public typealias Element = Base.Element
|
||||||
|
|
||||||
/// The iterator for an `AsyncInclusiveReductionsSequence` instance.
|
/// The iterator for an `AsyncInclusiveReductionsSequence` instance.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
@@ -58,7 +74,7 @@ extension AsyncInclusiveReductionsSequence: AsyncSequence {
|
|||||||
internal let transform: @Sendable (Base.Element, Base.Element) async -> Base.Element
|
internal let transform: @Sendable (Base.Element, Base.Element) async -> Base.Element
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
internal init(
|
init(
|
||||||
_ iterator: Base.AsyncIterator,
|
_ iterator: Base.AsyncIterator,
|
||||||
transform: @Sendable @escaping (Base.Element, Base.Element) async -> Base.Element
|
transform: @Sendable @escaping (Base.Element, Base.Element) async -> Base.Element
|
||||||
) {
|
) {
|
||||||
@@ -78,10 +94,15 @@ extension AsyncInclusiveReductionsSequence: AsyncSequence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
Iterator(base.makeAsyncIterator(), transform: transform)
|
Iterator(base.makeAsyncIterator(), transform: transform)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncInclusiveReductionsSequence: Sendable where Base: Sendable { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncInclusiveReductionsSequence: Sendable where Base: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncInclusiveReductionsSequence.Iterator: Sendable {}
|
||||||
@@ -1,102 +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
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
extension AsyncSequence {
|
|
||||||
/// Returns an asynchronous sequence containing elements of this asynchronous sequence with
|
|
||||||
/// the given separator inserted in between each element.
|
|
||||||
///
|
|
||||||
/// Any value of the asynchronous sequence's element type can be used as the separator.
|
|
||||||
///
|
|
||||||
/// - Parameter separator: The value to insert in between each of this async
|
|
||||||
/// sequence’s elements.
|
|
||||||
/// - Returns: The interspersed asynchronous sequence of elements.
|
|
||||||
@inlinable
|
|
||||||
public func interspersed(with separator: Element) -> AsyncInterspersedSequence<Self> {
|
|
||||||
AsyncInterspersedSequence(self, separator: separator)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An asynchronous sequence that presents the elements of a base asynchronous sequence of
|
|
||||||
/// elements with a separator between each of those elements.
|
|
||||||
public struct AsyncInterspersedSequence<Base: AsyncSequence> {
|
|
||||||
@usableFromInline
|
|
||||||
internal let base: Base
|
|
||||||
|
|
||||||
@usableFromInline
|
|
||||||
internal let separator: Base.Element
|
|
||||||
|
|
||||||
@usableFromInline
|
|
||||||
internal init(_ base: Base, separator: Base.Element) {
|
|
||||||
self.base = base
|
|
||||||
self.separator = separator
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension AsyncInterspersedSequence: AsyncSequence {
|
|
||||||
public typealias Element = Base.Element
|
|
||||||
|
|
||||||
/// The iterator for an `AsyncInterspersedSequence` asynchronous sequence.
|
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
|
||||||
@usableFromInline
|
|
||||||
internal enum State {
|
|
||||||
case start
|
|
||||||
case element(Result<Base.Element, Error>)
|
|
||||||
case separator
|
|
||||||
}
|
|
||||||
|
|
||||||
@usableFromInline
|
|
||||||
internal var iterator: Base.AsyncIterator
|
|
||||||
|
|
||||||
@usableFromInline
|
|
||||||
internal let separator: Base.Element
|
|
||||||
|
|
||||||
@usableFromInline
|
|
||||||
internal var state = State.start
|
|
||||||
|
|
||||||
@usableFromInline
|
|
||||||
internal init(_ iterator: Base.AsyncIterator, separator: Base.Element) {
|
|
||||||
self.iterator = iterator
|
|
||||||
self.separator = separator
|
|
||||||
}
|
|
||||||
|
|
||||||
public mutating func next() async rethrows -> Base.Element? {
|
|
||||||
// After the start, the state flips between element and separator. Before
|
|
||||||
// returning a separator, a check is made for the next element as a
|
|
||||||
// separator is only returned between two elements. The next element is
|
|
||||||
// stored to allow it to be returned in the next iteration. However, if
|
|
||||||
// the checking the next element throws, the separator is emitted before
|
|
||||||
// rethrowing that error.
|
|
||||||
switch state {
|
|
||||||
case .start:
|
|
||||||
state = .separator
|
|
||||||
return try await iterator.next()
|
|
||||||
case .separator:
|
|
||||||
do {
|
|
||||||
guard let next = try await iterator.next() else { return nil }
|
|
||||||
state = .element(.success(next))
|
|
||||||
} catch {
|
|
||||||
state = .element(.failure(error))
|
|
||||||
}
|
|
||||||
return separator
|
|
||||||
case .element(let result):
|
|
||||||
state = .separator
|
|
||||||
return try result._rethrowGet()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@inlinable
|
|
||||||
public func makeAsyncIterator() -> AsyncInterspersedSequence<Base>.Iterator {
|
|
||||||
Iterator(base.makeAsyncIterator(), separator: separator)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension AsyncInterspersedSequence: Sendable where Base: Sendable, Base.Element: Sendable { }
|
|
||||||
@@ -9,16 +9,22 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence where Element: AsyncSequence {
|
extension AsyncSequence where Element: AsyncSequence {
|
||||||
/// Concatenate an `AsyncSequence` of `AsyncSequence` elements with a separator.
|
/// Concatenate an `AsyncSequence` of `AsyncSequence` elements with a separator.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func joined<Separator: AsyncSequence>(separator: Separator) -> AsyncJoinedBySeparatorSequence<Self, Separator> {
|
public func joined<Separator: AsyncSequence>(
|
||||||
|
separator: Separator
|
||||||
|
) -> AsyncJoinedBySeparatorSequence<Self, Separator> {
|
||||||
return AsyncJoinedBySeparatorSequence(self, separator: separator)
|
return AsyncJoinedBySeparatorSequence(self, separator: separator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An `AsyncSequence` that concatenates `AsyncSequence` elements with a separator.
|
/// An `AsyncSequence` that concatenates `AsyncSequence` elements with a separator.
|
||||||
public struct AsyncJoinedBySeparatorSequence<Base: AsyncSequence, Separator: AsyncSequence>: AsyncSequence where Base.Element: AsyncSequence, Separator.Element == Base.Element.Element {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct AsyncJoinedBySeparatorSequence<Base: AsyncSequence, Separator: AsyncSequence>: AsyncSequence
|
||||||
|
where Base.Element: AsyncSequence, Separator.Element == Base.Element.Element {
|
||||||
public typealias Element = Base.Element.Element
|
public typealias Element = Base.Element.Element
|
||||||
public typealias AsyncIterator = Iterator
|
public typealias AsyncIterator = Iterator
|
||||||
|
|
||||||
@@ -36,31 +42,31 @@ public struct AsyncJoinedBySeparatorSequence<Base: AsyncSequence, Separator: Asy
|
|||||||
@usableFromInline
|
@usableFromInline
|
||||||
func startSeparator() -> SeparatorState {
|
func startSeparator() -> SeparatorState {
|
||||||
switch self {
|
switch self {
|
||||||
case .initial(let separatorSequence):
|
case .initial(let separatorSequence):
|
||||||
return .partialAsync(separatorSequence.makeAsyncIterator(), [])
|
return .partialAsync(separatorSequence.makeAsyncIterator(), [])
|
||||||
case .cached(let array):
|
case .cached(let array):
|
||||||
return .partialCached(array.makeIterator(), array)
|
return .partialCached(array.makeIterator(), array)
|
||||||
default:
|
default:
|
||||||
fatalError("Invalid separator sequence state")
|
fatalError("Invalid separator sequence state")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
func next() async rethrows -> (Element?, SeparatorState) {
|
func next() async rethrows -> (Element?, SeparatorState) {
|
||||||
switch self {
|
switch self {
|
||||||
case .partialAsync(var separatorIterator, var cache):
|
case .partialAsync(var separatorIterator, var cache):
|
||||||
guard let next = try await separatorIterator.next() else {
|
guard let next = try await separatorIterator.next() else {
|
||||||
return (nil, .cached(cache))
|
return (nil, .cached(cache))
|
||||||
}
|
}
|
||||||
cache.append(next)
|
cache.append(next)
|
||||||
return (next, .partialAsync(separatorIterator, cache))
|
return (next, .partialAsync(separatorIterator, cache))
|
||||||
case .partialCached(var cacheIterator, let cache):
|
case .partialCached(var cacheIterator, let cache):
|
||||||
guard let next = cacheIterator.next() else {
|
guard let next = cacheIterator.next() else {
|
||||||
return (nil, .cached(cache))
|
return (nil, .cached(cache))
|
||||||
}
|
}
|
||||||
return (next, .partialCached(cacheIterator, cache))
|
return (next, .partialCached(cacheIterator, cache))
|
||||||
default:
|
default:
|
||||||
fatalError("Invalid separator sequence state")
|
fatalError("Invalid separator sequence state")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,37 +89,37 @@ public struct AsyncJoinedBySeparatorSequence<Base: AsyncSequence, Separator: Asy
|
|||||||
public mutating func next() async rethrows -> Base.Element.Element? {
|
public mutating func next() async rethrows -> Base.Element.Element? {
|
||||||
do {
|
do {
|
||||||
switch state {
|
switch state {
|
||||||
case .terminal:
|
case .terminal:
|
||||||
|
return nil
|
||||||
|
case .initial(var outerIterator, let separator):
|
||||||
|
guard let innerSequence = try await outerIterator.next() else {
|
||||||
|
state = .terminal
|
||||||
return nil
|
return nil
|
||||||
case .initial(var outerIterator, let separator):
|
}
|
||||||
guard let innerSequence = try await outerIterator.next() else {
|
let innerIterator = innerSequence.makeAsyncIterator()
|
||||||
state = .terminal
|
state = .sequence(outerIterator, innerIterator, .initial(separator))
|
||||||
return nil
|
return try await next()
|
||||||
}
|
case .sequence(var outerIterator, var innerIterator, let separatorState):
|
||||||
let innerIterator = innerSequence.makeAsyncIterator()
|
if let item = try await innerIterator.next() {
|
||||||
state = .sequence(outerIterator, innerIterator, .initial(separator))
|
state = .sequence(outerIterator, innerIterator, separatorState)
|
||||||
return try await next()
|
|
||||||
case .sequence(var outerIterator, var innerIterator, let separatorState):
|
|
||||||
if let item = try await innerIterator.next() {
|
|
||||||
state = .sequence(outerIterator, innerIterator, separatorState)
|
|
||||||
return item
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let nextInner = try await outerIterator.next() else {
|
|
||||||
state = .terminal
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
state = .separator(outerIterator, separatorState.startSeparator(), nextInner)
|
|
||||||
return try await next()
|
|
||||||
case .separator(let iterator, let separatorState, let nextBase):
|
|
||||||
let (itemOpt, newSepState) = try await separatorState.next()
|
|
||||||
guard let item = itemOpt else {
|
|
||||||
state = .sequence(iterator, nextBase.makeAsyncIterator(), newSepState)
|
|
||||||
return try await next()
|
|
||||||
}
|
|
||||||
state = .separator(iterator, newSepState, nextBase)
|
|
||||||
return item
|
return item
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let nextInner = try await outerIterator.next() else {
|
||||||
|
state = .terminal
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
state = .separator(outerIterator, separatorState.startSeparator(), nextInner)
|
||||||
|
return try await next()
|
||||||
|
case .separator(let iterator, let separatorState, let nextBase):
|
||||||
|
let (itemOpt, newSepState) = try await separatorState.next()
|
||||||
|
guard let item = itemOpt else {
|
||||||
|
state = .sequence(iterator, nextBase.makeAsyncIterator(), newSepState)
|
||||||
|
return try await next()
|
||||||
|
}
|
||||||
|
state = .separator(iterator, newSepState, nextBase)
|
||||||
|
return item
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
state = .terminal
|
state = .terminal
|
||||||
@@ -140,5 +146,9 @@ public struct AsyncJoinedBySeparatorSequence<Base: AsyncSequence, Separator: Asy
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncJoinedBySeparatorSequence: Sendable
|
extension AsyncJoinedBySeparatorSequence: Sendable
|
||||||
where Base: Sendable, Base.Element: Sendable, Base.Element.Element: Sendable, Separator: Sendable { }
|
where Base: Sendable, Base.Element: Sendable, Base.Element.Element: Sendable, Separator: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncJoinedBySeparatorSequence.Iterator: Sendable {}
|
||||||
@@ -9,8 +9,10 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence where Element: AsyncSequence {
|
extension AsyncSequence where Element: AsyncSequence {
|
||||||
/// Concatenate an `AsyncSequence` of `AsyncSequence` elements
|
/// Concatenate an `AsyncSequence` of `AsyncSequence` elements
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func joined() -> AsyncJoinedSequence<Self> {
|
public func joined() -> AsyncJoinedSequence<Self> {
|
||||||
return AsyncJoinedSequence(self)
|
return AsyncJoinedSequence(self)
|
||||||
@@ -18,6 +20,7 @@ extension AsyncSequence where Element: AsyncSequence {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// An `AsyncSequence` that concatenates`AsyncSequence` elements
|
/// An `AsyncSequence` that concatenates`AsyncSequence` elements
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct AsyncJoinedSequence<Base: AsyncSequence>: AsyncSequence where Base.Element: AsyncSequence {
|
public struct AsyncJoinedSequence<Base: AsyncSequence>: AsyncSequence where Base.Element: AsyncSequence {
|
||||||
public typealias Element = Base.Element.Element
|
public typealias Element = Base.Element.Element
|
||||||
@@ -32,42 +35,42 @@ public struct AsyncJoinedSequence<Base: AsyncSequence>: AsyncSequence where Base
|
|||||||
case sequence(Base.AsyncIterator, Base.Element.AsyncIterator)
|
case sequence(Base.AsyncIterator, Base.Element.AsyncIterator)
|
||||||
case terminal
|
case terminal
|
||||||
}
|
}
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var state: State
|
var state: State
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
init(_ iterator: Base.AsyncIterator) {
|
init(_ iterator: Base.AsyncIterator) {
|
||||||
state = .initial(iterator)
|
state = .initial(iterator)
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public mutating func next() async rethrows -> Base.Element.Element? {
|
public mutating func next() async rethrows -> Base.Element.Element? {
|
||||||
do {
|
do {
|
||||||
switch state {
|
switch state {
|
||||||
case .terminal:
|
case .terminal:
|
||||||
|
return nil
|
||||||
|
case .initial(var outerIterator):
|
||||||
|
guard let innerSequence = try await outerIterator.next() else {
|
||||||
|
state = .terminal
|
||||||
return nil
|
return nil
|
||||||
case .initial(var outerIterator):
|
}
|
||||||
guard let innerSequence = try await outerIterator.next() else {
|
let innerIterator = innerSequence.makeAsyncIterator()
|
||||||
state = .terminal
|
state = .sequence(outerIterator, innerIterator)
|
||||||
return nil
|
return try await next()
|
||||||
}
|
case .sequence(var outerIterator, var innerIterator):
|
||||||
let innerIterator = innerSequence.makeAsyncIterator()
|
if let item = try await innerIterator.next() {
|
||||||
state = .sequence(outerIterator, innerIterator)
|
state = .sequence(outerIterator, innerIterator)
|
||||||
return try await next()
|
return item
|
||||||
case .sequence(var outerIterator, var innerIterator):
|
}
|
||||||
if let item = try await innerIterator.next() {
|
|
||||||
state = .sequence(outerIterator, innerIterator)
|
|
||||||
return item
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let nextInner = try await outerIterator.next() else {
|
|
||||||
state = .terminal
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
state = .sequence(outerIterator, nextInner.makeAsyncIterator())
|
guard let nextInner = try await outerIterator.next() else {
|
||||||
return try await next()
|
state = .terminal
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
state = .sequence(outerIterator, nextInner.makeAsyncIterator())
|
||||||
|
return try await next()
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
state = .terminal
|
state = .terminal
|
||||||
@@ -75,20 +78,24 @@ public struct AsyncJoinedSequence<Base: AsyncSequence>: AsyncSequence where Base
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base: Base
|
let base: Base
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(_ base: Base) {
|
init(_ base: Base) {
|
||||||
self.base = base
|
self.base = base
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
return Iterator(base.makeAsyncIterator())
|
return Iterator(base.makeAsyncIterator())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncJoinedSequence: Sendable
|
extension AsyncJoinedSequence: Sendable
|
||||||
where Base: Sendable, Base.Element: Sendable, Base.Element.Element: Sendable { }
|
where Base: Sendable, Base.Element: Sendable, Base.Element.Element: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncJoinedSequence.Iterator: Sendable {}
|
||||||
@@ -9,8 +9,10 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence where Element: Equatable {
|
extension AsyncSequence where Element: Equatable {
|
||||||
/// Creates an asynchronous sequence that omits repeated elements.
|
/// Creates an asynchronous sequence that omits repeated elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public func removeDuplicates() -> AsyncRemoveDuplicatesSequence<Self> {
|
public func removeDuplicates() -> AsyncRemoveDuplicatesSequence<Self> {
|
||||||
AsyncRemoveDuplicatesSequence(self) { lhs, rhs in
|
AsyncRemoveDuplicatesSequence(self) { lhs, rhs in
|
||||||
lhs == rhs
|
lhs == rhs
|
||||||
@@ -18,19 +20,27 @@ extension AsyncSequence where Element: Equatable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Creates an asynchronous sequence that omits repeated elements by testing them with a predicate.
|
/// Creates an asynchronous sequence that omits repeated elements by testing them with a predicate.
|
||||||
public func removeDuplicates(by predicate: @escaping @Sendable (Element, Element) async -> Bool) -> AsyncRemoveDuplicatesSequence<Self> {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public func removeDuplicates(
|
||||||
|
by predicate: @escaping @Sendable (Element, Element) async -> Bool
|
||||||
|
) -> AsyncRemoveDuplicatesSequence<Self> {
|
||||||
return AsyncRemoveDuplicatesSequence(self, predicate: predicate)
|
return AsyncRemoveDuplicatesSequence(self, predicate: predicate)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that omits repeated elements by testing them with an error-throwing predicate.
|
/// Creates an asynchronous sequence that omits repeated elements by testing them with an error-throwing predicate.
|
||||||
public func removeDuplicates(by predicate: @escaping @Sendable (Element, Element) async throws -> Bool) -> AsyncThrowingRemoveDuplicatesSequence<Self> {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public func removeDuplicates(
|
||||||
|
by predicate: @escaping @Sendable (Element, Element) async throws -> Bool
|
||||||
|
) -> AsyncThrowingRemoveDuplicatesSequence<Self> {
|
||||||
return AsyncThrowingRemoveDuplicatesSequence(self, predicate: predicate)
|
return AsyncThrowingRemoveDuplicatesSequence(self, predicate: predicate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An asynchronous sequence that omits repeated elements by testing them with a predicate.
|
/// An asynchronous sequence that omits repeated elements by testing them with a predicate.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public struct AsyncRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncSequence {
|
public struct AsyncRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncSequence {
|
||||||
public typealias Element = Base.Element
|
public typealias Element = Base.Element
|
||||||
|
|
||||||
@@ -73,7 +83,7 @@ public struct AsyncRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncSequence
|
|||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let predicate: @Sendable (Element, Element) async -> Bool
|
let predicate: @Sendable (Element, Element) async -> Bool
|
||||||
|
|
||||||
init(_ base: Base, predicate: @escaping @Sendable (Element, Element) async -> Bool) {
|
init(_ base: Base, predicate: @escaping @Sendable (Element, Element) async -> Bool) {
|
||||||
self.base = base
|
self.base = base
|
||||||
self.predicate = predicate
|
self.predicate = predicate
|
||||||
@@ -86,9 +96,10 @@ public struct AsyncRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncSequence
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// An asynchronous sequence that omits repeated elements by testing them with an error-throwing predicate.
|
/// An asynchronous sequence that omits repeated elements by testing them with an error-throwing predicate.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public struct AsyncThrowingRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncSequence {
|
public struct AsyncThrowingRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncSequence {
|
||||||
public typealias Element = Base.Element
|
public typealias Element = Base.Element
|
||||||
|
|
||||||
/// The iterator for an `AsyncThrowingRemoveDuplicatesSequence` instance.
|
/// The iterator for an `AsyncThrowingRemoveDuplicatesSequence` instance.
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
|
||||||
@@ -128,7 +139,7 @@ public struct AsyncThrowingRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncS
|
|||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let predicate: @Sendable (Element, Element) async throws -> Bool
|
let predicate: @Sendable (Element, Element) async throws -> Bool
|
||||||
|
|
||||||
init(_ base: Base, predicate: @escaping @Sendable (Element, Element) async throws -> Bool) {
|
init(_ base: Base, predicate: @escaping @Sendable (Element, Element) async throws -> Bool) {
|
||||||
self.base = base
|
self.base = base
|
||||||
self.predicate = predicate
|
self.predicate = predicate
|
||||||
@@ -140,6 +151,13 @@ public struct AsyncThrowingRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncRemoveDuplicatesSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncThrowingRemoveDuplicatesSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
|
|
||||||
extension AsyncRemoveDuplicatesSequence: Sendable where Base: Sendable, Base.Element: Sendable { }
|
@available(*, unavailable)
|
||||||
extension AsyncThrowingRemoveDuplicatesSequence: Sendable where Base: Sendable, Base.Element: Sendable { }
|
extension AsyncRemoveDuplicatesSequence.Iterator: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncThrowingRemoveDuplicatesSequence.Iterator: Sendable {}
|
||||||
+19
-13
@@ -9,13 +9,15 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension Sequence {
|
extension Sequence {
|
||||||
/// An asynchronous sequence containing the same elements as this sequence,
|
/// An asynchronous sequence containing the same elements as this sequence,
|
||||||
/// but on which operations, such as `map` and `filter`, are
|
/// but on which operations, such as `map` and `filter`, are
|
||||||
/// implemented asynchronously.
|
/// implemented asynchronously.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public var async: AsyncLazySequence<Self> {
|
public var async: AsyncSyncSequence<Self> {
|
||||||
AsyncLazySequence(self)
|
AsyncSyncSequence(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,43 +29,47 @@ extension Sequence {
|
|||||||
///
|
///
|
||||||
/// This functions similarly to `LazySequence` by accessing elements sequentially
|
/// This functions similarly to `LazySequence` by accessing elements sequentially
|
||||||
/// in the iterator's `next()` method.
|
/// in the iterator's `next()` method.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct AsyncLazySequence<Base: Sequence>: AsyncSequence {
|
public struct AsyncSyncSequence<Base: Sequence>: AsyncSequence {
|
||||||
public typealias Element = Base.Element
|
public typealias Element = Base.Element
|
||||||
|
|
||||||
@frozen
|
@frozen
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var iterator: Base.Iterator?
|
var iterator: Base.Iterator?
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(_ iterator: Base.Iterator) {
|
init(_ iterator: Base.Iterator) {
|
||||||
self.iterator = iterator
|
self.iterator = iterator
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public mutating func next() async -> Base.Element? {
|
public mutating func next() async -> Base.Element? {
|
||||||
if !Task.isCancelled, let value = iterator?.next() {
|
guard !Task.isCancelled, let value = iterator?.next() else {
|
||||||
return value
|
|
||||||
} else {
|
|
||||||
iterator = nil
|
iterator = nil
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base: Base
|
let base: Base
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(_ base: Base) {
|
init(_ base: Base) {
|
||||||
self.base = base
|
self.base = base
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
Iterator(base.makeIterator())
|
Iterator(base.makeIterator())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncLazySequence: Sendable where Base: Sendable { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncSyncSequence: Sendable where Base: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncSyncSequence.Iterator: Sendable {}
|
||||||
@@ -9,47 +9,66 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
|
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public func throttle<C: Clock, Reduced>(for interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> AsyncThrottleSequence<Self, C, Reduced> {
|
public func _throttle<C: Clock, Reduced>(
|
||||||
AsyncThrottleSequence(self, interval: interval, clock: clock, reducing: reducing)
|
for interval: C.Instant.Duration,
|
||||||
|
clock: C,
|
||||||
|
reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced
|
||||||
|
) -> _AsyncThrottleSequence<Self, C, Reduced> {
|
||||||
|
_AsyncThrottleSequence(self, interval: interval, clock: clock, reducing: reducing)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
|
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public func throttle<Reduced>(for interval: Duration, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> AsyncThrottleSequence<Self, ContinuousClock, Reduced> {
|
public func _throttle<Reduced>(
|
||||||
throttle(for: interval, clock: .continuous, reducing: reducing)
|
for interval: Duration,
|
||||||
|
reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced
|
||||||
|
) -> _AsyncThrottleSequence<Self, ContinuousClock, Reduced> {
|
||||||
|
_throttle(for: interval, clock: .continuous, reducing: reducing)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
|
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public func throttle<C: Clock>(for interval: C.Instant.Duration, clock: C, latest: Bool = true) -> AsyncThrottleSequence<Self, C, Element> {
|
public func _throttle<C: Clock>(
|
||||||
throttle(for: interval, clock: clock) { previous, element in
|
for interval: C.Instant.Duration,
|
||||||
if latest {
|
clock: C,
|
||||||
return element
|
latest: Bool = true
|
||||||
} else {
|
) -> _AsyncThrottleSequence<Self, C, Element> {
|
||||||
|
_throttle(for: interval, clock: clock) { previous, element in
|
||||||
|
guard latest else {
|
||||||
return previous ?? element
|
return previous ?? element
|
||||||
}
|
}
|
||||||
|
return element
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
|
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public func throttle(for interval: Duration, latest: Bool = true) -> AsyncThrottleSequence<Self, ContinuousClock, Element> {
|
public func _throttle(
|
||||||
throttle(for: interval, clock: .continuous, latest: latest)
|
for interval: Duration,
|
||||||
|
latest: Bool = true
|
||||||
|
) -> _AsyncThrottleSequence<Self, ContinuousClock, Element> {
|
||||||
|
_throttle(for: interval, clock: .continuous, latest: latest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A rate-limited `AsyncSequence` by emitting values at most every specified interval.
|
/// A rate-limited `AsyncSequence` by emitting values at most every specified interval.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public struct AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
|
public struct _AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
|
||||||
let base: Base
|
let base: Base
|
||||||
let interval: C.Instant.Duration
|
let interval: C.Instant.Duration
|
||||||
let clock: C
|
let clock: C
|
||||||
let reducing: @Sendable (Reduced?, Base.Element) async -> Reduced
|
let reducing: @Sendable (Reduced?, Base.Element) async -> Reduced
|
||||||
|
|
||||||
init(_ base: Base, interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Base.Element) async -> Reduced) {
|
init(
|
||||||
|
_ base: Base,
|
||||||
|
interval: C.Instant.Duration,
|
||||||
|
clock: C,
|
||||||
|
reducing: @Sendable @escaping (Reduced?, Base.Element) async -> Reduced
|
||||||
|
) {
|
||||||
self.base = base
|
self.base = base
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.clock = clock
|
self.clock = clock
|
||||||
@@ -58,9 +77,9 @@ public struct AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
extension AsyncThrottleSequence: AsyncSequence {
|
extension _AsyncThrottleSequence: AsyncSequence {
|
||||||
public typealias Element = Reduced
|
public typealias Element = Reduced
|
||||||
|
|
||||||
/// The iterator for an `AsyncThrottleSequence` instance.
|
/// The iterator for an `AsyncThrottleSequence` instance.
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
var base: Base.AsyncIterator
|
var base: Base.AsyncIterator
|
||||||
@@ -68,20 +87,34 @@ extension AsyncThrottleSequence: AsyncSequence {
|
|||||||
let interval: C.Instant.Duration
|
let interval: C.Instant.Duration
|
||||||
let clock: C
|
let clock: C
|
||||||
let reducing: @Sendable (Reduced?, Base.Element) async -> Reduced
|
let reducing: @Sendable (Reduced?, Base.Element) async -> Reduced
|
||||||
|
|
||||||
init(_ base: Base.AsyncIterator, interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Base.Element) async -> Reduced) {
|
init(
|
||||||
|
_ base: Base.AsyncIterator,
|
||||||
|
interval: C.Instant.Duration,
|
||||||
|
clock: C,
|
||||||
|
reducing: @Sendable @escaping (Reduced?, Base.Element) async -> Reduced
|
||||||
|
) {
|
||||||
self.base = base
|
self.base = base
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.clock = clock
|
self.clock = clock
|
||||||
self.reducing = reducing
|
self.reducing = reducing
|
||||||
}
|
}
|
||||||
|
|
||||||
public mutating func next() async rethrows -> Reduced? {
|
public mutating func next() async rethrows -> Reduced? {
|
||||||
var reduced: Reduced?
|
var reduced: Reduced?
|
||||||
let start = last ?? clock.now
|
let start = last ?? clock.now
|
||||||
repeat {
|
repeat {
|
||||||
guard let element = try await base.next() else {
|
guard let element = try await base.next() else {
|
||||||
return nil
|
if reduced != nil, let last {
|
||||||
|
// ensure the rate of elements never exceeds the given interval
|
||||||
|
let amount = interval - last.duration(to: clock.now)
|
||||||
|
if amount > .zero {
|
||||||
|
try? await clock.sleep(until: clock.now.advanced(by: amount), tolerance: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// the last value is unable to have any subsequent
|
||||||
|
// values so always return the last reduction
|
||||||
|
return reduced
|
||||||
}
|
}
|
||||||
let reduction = await reducing(reduced, element)
|
let reduction = await reducing(reduced, element)
|
||||||
let now = clock.now
|
let now = clock.now
|
||||||
@@ -94,11 +127,14 @@ extension AsyncThrottleSequence: AsyncSequence {
|
|||||||
} while true
|
} while true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
Iterator(base.makeAsyncIterator(), interval: interval, clock: clock, reducing: reducing)
|
Iterator(base.makeAsyncIterator(), interval: interval, clock: clock, reducing: reducing)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
extension AsyncThrottleSequence: Sendable where Base: Sendable, Element: Sendable { }
|
extension _AsyncThrottleSequence: Sendable where Base: Sendable, Element: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension _AsyncThrottleSequence.Iterator: Sendable {}
|
||||||
@@ -1,320 +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
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
@preconcurrency @_implementationOnly import OrderedCollections
|
|
||||||
|
|
||||||
/// An error-throwing channel for sending elements from on task to another with back pressure.
|
|
||||||
///
|
|
||||||
/// The `AsyncThrowingChannel` class is intended to be used as a communication types between tasks,
|
|
||||||
/// particularly when one task produces values and another task consumes those values. The back
|
|
||||||
/// pressure applied by `send(_:)` via suspension/resume ensures that the production of values does
|
|
||||||
/// not exceed the consumption of values from iteration. This method suspends after enqueuing the event
|
|
||||||
/// and is resumed when the next call to `next()` on the `Iterator` is made, or when `finish()`/`fail(_:)` is called
|
|
||||||
/// from another Task. As `finish()` and `fail(_:)` induce a terminal state, there is no need for a back pressure management.
|
|
||||||
/// Those functions do not suspend and will finish all the pending iterations.
|
|
||||||
public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: AsyncSequence, Sendable {
|
|
||||||
/// The iterator for an `AsyncThrowingChannel` instance.
|
|
||||||
public struct Iterator: AsyncIteratorProtocol, Sendable {
|
|
||||||
let channel: AsyncThrowingChannel<Element, Failure>
|
|
||||||
var active: Bool = true
|
|
||||||
|
|
||||||
init(_ channel: AsyncThrowingChannel<Element, Failure>) {
|
|
||||||
self.channel = channel
|
|
||||||
}
|
|
||||||
|
|
||||||
public mutating func next() async throws -> Element? {
|
|
||||||
guard active else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
let generation = channel.establish()
|
|
||||||
let nextTokenStatus = ManagedCriticalState<ChannelTokenStatus>(.new)
|
|
||||||
|
|
||||||
do {
|
|
||||||
let value = try await withTaskCancellationHandler {
|
|
||||||
try await channel.next(nextTokenStatus, generation)
|
|
||||||
} onCancel: { [channel] in
|
|
||||||
channel.cancelNext(nextTokenStatus, generation)
|
|
||||||
}
|
|
||||||
|
|
||||||
if let value = value {
|
|
||||||
return value
|
|
||||||
} else {
|
|
||||||
active = false
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
active = false
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
typealias Pending = ChannelToken<UnsafeContinuation<UnsafeContinuation<Element?, Error>?, Never>>
|
|
||||||
typealias Awaiting = ChannelToken<UnsafeContinuation<Element?, Error>>
|
|
||||||
|
|
||||||
struct ChannelToken<Continuation: Sendable>: Hashable, Sendable {
|
|
||||||
var generation: Int
|
|
||||||
var continuation: Continuation?
|
|
||||||
|
|
||||||
init(generation: Int, continuation: Continuation) {
|
|
||||||
self.generation = generation
|
|
||||||
self.continuation = continuation
|
|
||||||
}
|
|
||||||
|
|
||||||
init(placeholder generation: Int) {
|
|
||||||
self.generation = generation
|
|
||||||
self.continuation = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func hash(into hasher: inout Hasher) {
|
|
||||||
hasher.combine(generation)
|
|
||||||
}
|
|
||||||
|
|
||||||
static func == (_ lhs: ChannelToken, _ rhs: ChannelToken) -> Bool {
|
|
||||||
return lhs.generation == rhs.generation
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
enum ChannelTokenStatus: Equatable {
|
|
||||||
case new
|
|
||||||
case cancelled
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Termination {
|
|
||||||
case finished
|
|
||||||
case failed(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Emission: Sendable {
|
|
||||||
case idle
|
|
||||||
case pending(OrderedSet<Pending>)
|
|
||||||
case awaiting(OrderedSet<Awaiting>)
|
|
||||||
case terminated(Termination)
|
|
||||||
}
|
|
||||||
|
|
||||||
struct State : Sendable {
|
|
||||||
var emission: Emission = .idle
|
|
||||||
var generation = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
let state = ManagedCriticalState(State())
|
|
||||||
|
|
||||||
public init(_ elementType: Element.Type = Element.self) { }
|
|
||||||
|
|
||||||
func establish() -> Int {
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
defer { state.generation &+= 1 }
|
|
||||||
return state.generation
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func cancelNext(_ nextTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) {
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
let continuation: UnsafeContinuation<Element?, Error>?
|
|
||||||
|
|
||||||
switch state.emission {
|
|
||||||
case .awaiting(var nexts):
|
|
||||||
continuation = nexts.remove(Awaiting(placeholder: generation))?.continuation
|
|
||||||
if nexts.isEmpty {
|
|
||||||
state.emission = .idle
|
|
||||||
} else {
|
|
||||||
state.emission = .awaiting(nexts)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
continuation = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
nextTokenStatus.withCriticalRegion { status in
|
|
||||||
if status == .new {
|
|
||||||
status = .cancelled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
continuation?.resume(returning: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func next(_ nextTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) async throws -> Element? {
|
|
||||||
return try await withUnsafeThrowingContinuation { (continuation: UnsafeContinuation<Element?, Error>) in
|
|
||||||
var cancelled = false
|
|
||||||
var potentialTermination: Termination?
|
|
||||||
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
|
|
||||||
if nextTokenStatus.withCriticalRegion({ $0 }) == .cancelled {
|
|
||||||
cancelled = true
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch state.emission {
|
|
||||||
case .idle:
|
|
||||||
state.emission = .awaiting([Awaiting(generation: generation, continuation: continuation)])
|
|
||||||
case .pending(var sends):
|
|
||||||
let send = sends.removeFirst()
|
|
||||||
if sends.count == 0 {
|
|
||||||
state.emission = .idle
|
|
||||||
} else {
|
|
||||||
state.emission = .pending(sends)
|
|
||||||
}
|
|
||||||
send.continuation?.resume(returning: continuation)
|
|
||||||
case .awaiting(var nexts):
|
|
||||||
nexts.updateOrAppend(Awaiting(generation: generation, continuation: continuation))
|
|
||||||
state.emission = .awaiting(nexts)
|
|
||||||
case .terminated(let termination):
|
|
||||||
potentialTermination = termination
|
|
||||||
state.emission = .terminated(.finished)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if cancelled {
|
|
||||||
continuation.resume(returning: nil)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch potentialTermination {
|
|
||||||
case .none:
|
|
||||||
return
|
|
||||||
case .failed(let error):
|
|
||||||
continuation.resume(throwing: error)
|
|
||||||
return
|
|
||||||
case .finished:
|
|
||||||
continuation.resume(returning: nil)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func cancelSend(_ sendTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) {
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
let continuation: UnsafeContinuation<UnsafeContinuation<Element?, Error>?, Never>?
|
|
||||||
|
|
||||||
switch state.emission {
|
|
||||||
case .pending(var sends):
|
|
||||||
let send = sends.remove(Pending(placeholder: generation))
|
|
||||||
if sends.isEmpty {
|
|
||||||
state.emission = .idle
|
|
||||||
} else {
|
|
||||||
state.emission = .pending(sends)
|
|
||||||
}
|
|
||||||
continuation = send?.continuation
|
|
||||||
default:
|
|
||||||
continuation = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
sendTokenStatus.withCriticalRegion { status in
|
|
||||||
if status == .new {
|
|
||||||
status = .cancelled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
continuation?.resume(returning: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func send(_ sendTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int, _ element: Element) async {
|
|
||||||
let continuation: UnsafeContinuation<Element?, Error>? = await withUnsafeContinuation { continuation in
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
|
|
||||||
if sendTokenStatus.withCriticalRegion({ $0 }) == .cancelled {
|
|
||||||
continuation.resume(returning: nil)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch state.emission {
|
|
||||||
case .idle:
|
|
||||||
state.emission = .pending([Pending(generation: generation, continuation: continuation)])
|
|
||||||
case .pending(var sends):
|
|
||||||
sends.updateOrAppend(Pending(generation: generation, continuation: continuation))
|
|
||||||
state.emission = .pending(sends)
|
|
||||||
case .awaiting(var nexts):
|
|
||||||
let next = nexts.removeFirst().continuation
|
|
||||||
if nexts.count == 0 {
|
|
||||||
state.emission = .idle
|
|
||||||
} else {
|
|
||||||
state.emission = .awaiting(nexts)
|
|
||||||
}
|
|
||||||
continuation.resume(returning: next)
|
|
||||||
case .terminated:
|
|
||||||
continuation.resume(returning: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continuation?.resume(returning: element)
|
|
||||||
}
|
|
||||||
|
|
||||||
func terminateAll(error: Failure? = nil) {
|
|
||||||
state.withCriticalRegion { state in
|
|
||||||
|
|
||||||
let nextState: Emission
|
|
||||||
if let error = error {
|
|
||||||
nextState = .terminated(.failed(error))
|
|
||||||
} else {
|
|
||||||
nextState = .terminated(.finished)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch state.emission {
|
|
||||||
case .idle:
|
|
||||||
state.emission = nextState
|
|
||||||
case .pending(let sends):
|
|
||||||
state.emission = nextState
|
|
||||||
for send in sends {
|
|
||||||
send.continuation?.resume(returning: nil)
|
|
||||||
}
|
|
||||||
case .awaiting(let nexts):
|
|
||||||
state.emission = nextState
|
|
||||||
if let error = error {
|
|
||||||
for next in nexts {
|
|
||||||
next.continuation?.resume(throwing: error)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for next in nexts {
|
|
||||||
next.continuation?.resume(returning: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case .terminated:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send an element to an awaiting iteration. This function will resume when the next call to `next()` is made
|
|
||||||
/// or when a call to `finish()`/`fail(_:)` is made from another Task.
|
|
||||||
/// If the channel is already finished then this returns immediately
|
|
||||||
/// If the task is cancelled, this function will resume. Other sending operations from other tasks will remain active.
|
|
||||||
public func send(_ element: Element) async {
|
|
||||||
let generation = establish()
|
|
||||||
let sendTokenStatus = ManagedCriticalState<ChannelTokenStatus>(.new)
|
|
||||||
|
|
||||||
await withTaskCancellationHandler {
|
|
||||||
await send(sendTokenStatus, generation, element)
|
|
||||||
} onCancel: { [weak self] in
|
|
||||||
self?.cancelSend(sendTokenStatus, generation)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send an error to all awaiting iterations.
|
|
||||||
/// All subsequent calls to `next(_:)` will resume immediately.
|
|
||||||
public func fail(_ error: Error) where Failure == Error {
|
|
||||||
terminateAll(error: error)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send a finish to all awaiting iterations.
|
|
||||||
/// All subsequent calls to `next(_:)` will resume immediately.
|
|
||||||
public func finish() {
|
|
||||||
terminateAll()
|
|
||||||
}
|
|
||||||
|
|
||||||
public func makeAsyncIterator() -> Iterator {
|
|
||||||
return Iterator(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
||||||
/// elements of the asynchronous sequence using the given error-throwing closure.
|
/// elements of the asynchronous sequence using the given error-throwing closure.
|
||||||
@@ -23,13 +24,17 @@ extension AsyncSequence {
|
|||||||
/// the result. If the closure throws an error, the sequence throws.
|
/// the result. If the closure throws an error, the sequence throws.
|
||||||
/// - Returns: An asynchronous sequence of the initial value followed by the reduced
|
/// - Returns: An asynchronous sequence of the initial value followed by the reduced
|
||||||
/// elements.
|
/// elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func reductions<Result>(_ initial: Result, _ transform: @Sendable @escaping (Result, Element) async throws -> Result) -> AsyncThrowingExclusiveReductionsSequence<Self, Result> {
|
public func reductions<Result>(
|
||||||
|
_ initial: Result,
|
||||||
|
_ transform: @Sendable @escaping (Result, Element) async throws -> Result
|
||||||
|
) -> AsyncThrowingExclusiveReductionsSequence<Self, Result> {
|
||||||
reductions(into: initial) { result, element in
|
reductions(into: initial) { result, element in
|
||||||
result = try await transform(result, element)
|
result = try await transform(result, element)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
||||||
/// elements of the asynchronous sequence using the given error-throwing closure.
|
/// elements of the asynchronous sequence using the given error-throwing closure.
|
||||||
///
|
///
|
||||||
@@ -44,78 +49,96 @@ extension AsyncSequence {
|
|||||||
/// error, the sequence throws.
|
/// error, the sequence throws.
|
||||||
/// - Returns: An asynchronous sequence of the initial value followed by the reduced
|
/// - Returns: An asynchronous sequence of the initial value followed by the reduced
|
||||||
/// elements.
|
/// elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func reductions<Result>(into initial: Result, _ transform: @Sendable @escaping (inout Result, Element) async throws -> Void) -> AsyncThrowingExclusiveReductionsSequence<Self, Result> {
|
public func reductions<Result>(
|
||||||
|
into initial: Result,
|
||||||
|
_ transform: @Sendable @escaping (inout Result, Element) async throws -> Void
|
||||||
|
) -> AsyncThrowingExclusiveReductionsSequence<Self, Result> {
|
||||||
AsyncThrowingExclusiveReductionsSequence(self, initial: initial, transform: transform)
|
AsyncThrowingExclusiveReductionsSequence(self, initial: initial, transform: transform)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An asynchronous sequence of applying an error-throwing transform to the element of
|
/// An asynchronous sequence of applying an error-throwing transform to the element of
|
||||||
/// an asynchronous sequence and the previously transformed result.
|
/// an asynchronous sequence and the previously transformed result.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct AsyncThrowingExclusiveReductionsSequence<Base: AsyncSequence, Element> {
|
public struct AsyncThrowingExclusiveReductionsSequence<Base: AsyncSequence, Element> {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base: Base
|
let base: Base
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let initial: Element
|
let initial: Element
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let transform: @Sendable (inout Element, Base.Element) async throws -> Void
|
let transform: @Sendable (inout Element, Base.Element) async throws -> Void
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
init(_ base: Base, initial: Element, transform: @Sendable @escaping (inout Element, Base.Element) async throws -> Void) {
|
init(
|
||||||
|
_ base: Base,
|
||||||
|
initial: Element,
|
||||||
|
transform: @Sendable @escaping (inout Element, Base.Element) async throws -> Void
|
||||||
|
) {
|
||||||
self.base = base
|
self.base = base
|
||||||
self.initial = initial
|
self.initial = initial
|
||||||
self.transform = transform
|
self.transform = transform
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncThrowingExclusiveReductionsSequence: AsyncSequence {
|
extension AsyncThrowingExclusiveReductionsSequence: AsyncSequence {
|
||||||
/// The iterator for an `AsyncThrowingExclusiveReductionsSequence` instance.
|
/// The iterator for an `AsyncThrowingExclusiveReductionsSequence` instance.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var iterator: Base.AsyncIterator
|
var iterator: Base.AsyncIterator
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
var current: Element?
|
var current: Element?
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let transform: @Sendable (inout Element, Base.Element) async throws -> Void
|
let transform: @Sendable (inout Element, Base.Element) async throws -> Void
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
init(_ iterator: Base.AsyncIterator, initial: Element, transform: @Sendable @escaping (inout Element, Base.Element) async throws -> Void) {
|
init(
|
||||||
|
_ iterator: Base.AsyncIterator,
|
||||||
|
initial: Element,
|
||||||
|
transform: @Sendable @escaping (inout Element, Base.Element) async throws -> Void
|
||||||
|
) {
|
||||||
self.iterator = iterator
|
self.iterator = iterator
|
||||||
self.current = initial
|
self.current = initial
|
||||||
self.transform = transform
|
self.transform = transform
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public mutating func next() async throws -> Element? {
|
public mutating func next() async throws -> Element? {
|
||||||
guard let result = current else { return nil }
|
guard var result = current else { return nil }
|
||||||
let value = try await iterator.next()
|
let value = try await iterator.next()
|
||||||
if let value = value {
|
guard let value = value else {
|
||||||
var result = result
|
|
||||||
do {
|
|
||||||
try await transform(&result, value)
|
|
||||||
current = result
|
|
||||||
return result
|
|
||||||
} catch {
|
|
||||||
current = nil
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
current = nil
|
current = nil
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
do {
|
||||||
|
try await transform(&result, value)
|
||||||
|
current = result
|
||||||
|
return result
|
||||||
|
} catch {
|
||||||
|
current = nil
|
||||||
|
throw error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
Iterator(base.makeAsyncIterator(), initial: initial, transform: transform)
|
Iterator(base.makeAsyncIterator(), initial: initial, transform: transform)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncThrowingExclusiveReductionsSequence: Sendable where Base: Sendable, Element: Sendable { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncThrowingExclusiveReductionsSequence: Sendable where Base: Sendable, Element: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncThrowingExclusiveReductionsSequence.Iterator: Sendable {}
|
||||||
@@ -1,3 +1,15 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
/// Returns an asynchronous sequence containing the accumulated results of combining the
|
||||||
/// elements of the asynchronous sequence using the given error-throwing closure.
|
/// elements of the asynchronous sequence using the given error-throwing closure.
|
||||||
@@ -16,6 +28,7 @@ extension AsyncSequence {
|
|||||||
/// result and the next element in the receiving sequence. If the closure
|
/// result and the next element in the receiving sequence. If the closure
|
||||||
/// throws an error, the sequence throws.
|
/// throws an error, the sequence throws.
|
||||||
/// - Returns: An asynchronous sequence of the reduced elements.
|
/// - Returns: An asynchronous sequence of the reduced elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func reductions(
|
public func reductions(
|
||||||
_ transform: @Sendable @escaping (Element, Element) async throws -> Element
|
_ transform: @Sendable @escaping (Element, Element) async throws -> Element
|
||||||
@@ -26,14 +39,15 @@ extension AsyncSequence {
|
|||||||
|
|
||||||
/// An asynchronous sequence containing the accumulated results of combining the
|
/// An asynchronous sequence containing the accumulated results of combining the
|
||||||
/// elements of the asynchronous sequence using a given error-throwing closure.
|
/// elements of the asynchronous sequence using a given error-throwing closure.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct AsyncThrowingInclusiveReductionsSequence<Base: AsyncSequence> {
|
public struct AsyncThrowingInclusiveReductionsSequence<Base: AsyncSequence> {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let base: Base
|
let base: Base
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
let transform: @Sendable (Base.Element, Base.Element) async throws -> Base.Element
|
let transform: @Sendable (Base.Element, Base.Element) async throws -> Base.Element
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
init(_ base: Base, transform: @Sendable @escaping (Base.Element, Base.Element) async throws -> Base.Element) {
|
init(_ base: Base, transform: @Sendable @escaping (Base.Element, Base.Element) async throws -> Base.Element) {
|
||||||
self.base = base
|
self.base = base
|
||||||
@@ -41,10 +55,12 @@ public struct AsyncThrowingInclusiveReductionsSequence<Base: AsyncSequence> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncThrowingInclusiveReductionsSequence: AsyncSequence {
|
extension AsyncThrowingInclusiveReductionsSequence: AsyncSequence {
|
||||||
public typealias Element = Base.Element
|
public typealias Element = Base.Element
|
||||||
|
|
||||||
/// The iterator for an `AsyncThrowingInclusiveReductionsSequence` instance.
|
/// The iterator for an `AsyncThrowingInclusiveReductionsSequence` instance.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@frozen
|
@frozen
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
@@ -57,7 +73,7 @@ extension AsyncThrowingInclusiveReductionsSequence: AsyncSequence {
|
|||||||
internal let transform: @Sendable (Base.Element, Base.Element) async throws -> Base.Element
|
internal let transform: @Sendable (Base.Element, Base.Element) async throws -> Base.Element
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
internal init(
|
init(
|
||||||
_ iterator: Base.AsyncIterator,
|
_ iterator: Base.AsyncIterator,
|
||||||
transform: @Sendable @escaping (Base.Element, Base.Element) async throws -> Base.Element
|
transform: @Sendable @escaping (Base.Element, Base.Element) async throws -> Base.Element
|
||||||
) {
|
) {
|
||||||
@@ -82,10 +98,15 @@ extension AsyncThrowingInclusiveReductionsSequence: AsyncSequence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
Iterator(base.makeAsyncIterator(), transform: transform)
|
Iterator(base.makeAsyncIterator(), transform: transform)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncThrowingInclusiveReductionsSequence: Sendable where Base: Sendable { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncThrowingInclusiveReductionsSequence: Sendable where Base: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncThrowingInclusiveReductionsSequence.Iterator: Sendable {}
|
||||||
@@ -13,59 +13,49 @@
|
|||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public struct AsyncTimerSequence<C: Clock>: AsyncSequence {
|
public struct AsyncTimerSequence<C: Clock>: AsyncSequence {
|
||||||
public typealias Element = C.Instant
|
public typealias Element = C.Instant
|
||||||
|
|
||||||
/// The iterator for an `AsyncTimerSequence` instance.
|
/// The iterator for an `AsyncTimerSequence` instance.
|
||||||
public struct Iterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
var clock: C?
|
var clock: C?
|
||||||
let interval: C.Instant.Duration
|
let interval: C.Instant.Duration
|
||||||
let tolerance: C.Instant.Duration?
|
let tolerance: C.Instant.Duration?
|
||||||
var last: C.Instant?
|
var last: C.Instant?
|
||||||
|
|
||||||
init(interval: C.Instant.Duration, tolerance: C.Instant.Duration?, clock: C) {
|
init(interval: C.Instant.Duration, tolerance: C.Instant.Duration?, clock: C) {
|
||||||
self.clock = clock
|
self.clock = clock
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.tolerance = tolerance
|
self.tolerance = tolerance
|
||||||
}
|
}
|
||||||
|
|
||||||
func nextDeadline(_ clock: C) -> C.Instant {
|
|
||||||
let now = clock.now
|
|
||||||
let last = self.last ?? now
|
|
||||||
let next = last.advanced(by: interval)
|
|
||||||
if next < now {
|
|
||||||
return last.advanced(by: interval * Int(((next.duration(to: now)) / interval).rounded(.up)))
|
|
||||||
} else {
|
|
||||||
return next
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public mutating func next() async -> C.Instant? {
|
public mutating func next() async -> C.Instant? {
|
||||||
guard let clock = clock else {
|
guard let clock = self.clock else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
let next = nextDeadline(clock)
|
|
||||||
|
let next = (self.last ?? clock.now).advanced(by: self.interval)
|
||||||
do {
|
do {
|
||||||
try await clock.sleep(until: next, tolerance: tolerance)
|
try await clock.sleep(until: next, tolerance: self.tolerance)
|
||||||
} catch {
|
} catch {
|
||||||
self.clock = nil
|
self.clock = nil
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
let now = clock.now
|
let now = clock.now
|
||||||
last = next
|
self.last = next
|
||||||
return now
|
return now
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let clock: C
|
let clock: C
|
||||||
let interval: C.Instant.Duration
|
let interval: C.Instant.Duration
|
||||||
let tolerance: C.Instant.Duration?
|
let tolerance: C.Instant.Duration?
|
||||||
|
|
||||||
/// Create an `AsyncTimerSequence` with a given repeating interval.
|
/// Create an `AsyncTimerSequence` with a given repeating interval.
|
||||||
public init(interval: C.Instant.Duration, tolerance: C.Instant.Duration? = nil, clock: C) {
|
public init(interval: C.Instant.Duration, tolerance: C.Instant.Duration? = nil, clock: C) {
|
||||||
self.clock = clock
|
self.clock = clock
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.tolerance = tolerance
|
self.tolerance = tolerance
|
||||||
}
|
}
|
||||||
|
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
Iterator(interval: interval, tolerance: tolerance, clock: clock)
|
Iterator(interval: interval, tolerance: tolerance, clock: clock)
|
||||||
}
|
}
|
||||||
@@ -74,7 +64,11 @@ public struct AsyncTimerSequence<C: Clock>: AsyncSequence {
|
|||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
extension AsyncTimerSequence {
|
extension AsyncTimerSequence {
|
||||||
/// Create an `AsyncTimerSequence` with a given repeating interval.
|
/// Create an `AsyncTimerSequence` with a given repeating interval.
|
||||||
public static func repeating(every interval: C.Instant.Duration, tolerance: C.Instant.Duration? = nil, clock: C) -> AsyncTimerSequence<C> {
|
public static func repeating(
|
||||||
|
every interval: C.Instant.Duration,
|
||||||
|
tolerance: C.Instant.Duration? = nil,
|
||||||
|
clock: C
|
||||||
|
) -> AsyncTimerSequence<C> {
|
||||||
return AsyncTimerSequence(interval: interval, tolerance: tolerance, clock: clock)
|
return AsyncTimerSequence(interval: interval, tolerance: tolerance, clock: clock)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,10 +76,16 @@ extension AsyncTimerSequence {
|
|||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
extension AsyncTimerSequence where C == SuspendingClock {
|
extension AsyncTimerSequence where C == SuspendingClock {
|
||||||
/// Create an `AsyncTimerSequence` with a given repeating interval.
|
/// Create an `AsyncTimerSequence` with a given repeating interval.
|
||||||
public static func repeating(every interval: Duration, tolerance: Duration? = nil) -> AsyncTimerSequence<SuspendingClock> {
|
public static func repeating(
|
||||||
|
every interval: Duration,
|
||||||
|
tolerance: Duration? = nil
|
||||||
|
) -> AsyncTimerSequence<SuspendingClock> {
|
||||||
return AsyncTimerSequence(interval: interval, tolerance: tolerance, clock: SuspendingClock())
|
return AsyncTimerSequence(interval: interval, tolerance: tolerance, clock: SuspendingClock())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
extension AsyncTimerSequence: Sendable { }
|
extension AsyncTimerSequence: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncTimerSequence.Iterator: Sendable {}
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncSequence where Self: Sendable {
|
||||||
|
/// Creates an asynchronous sequence that buffers elements.
|
||||||
|
///
|
||||||
|
/// The buffering behaviour is dictated by the policy:
|
||||||
|
/// - bounded: will buffer elements until the limit is reached. Then it will suspend the upstream async sequence.
|
||||||
|
/// - unbounded: will buffer elements without limit.
|
||||||
|
/// - bufferingNewest: will buffer elements until the limit is reached. Then it will discard the oldest elements.
|
||||||
|
/// - bufferingOldest: will buffer elements until the limit is reached. Then it will discard the newest elements.
|
||||||
|
///
|
||||||
|
/// - Parameter policy: A policy that drives the behaviour of the ``AsyncBufferSequence``
|
||||||
|
/// - Returns: An asynchronous sequence that buffers elements up to a given limit.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public func buffer(
|
||||||
|
policy: AsyncBufferSequencePolicy
|
||||||
|
) -> AsyncBufferSequence<Self> {
|
||||||
|
AsyncBufferSequence<Self>(base: self, policy: policy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A policy dictating the buffering behaviour of an ``AsyncBufferSequence``
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct AsyncBufferSequencePolicy: Sendable {
|
||||||
|
enum _Policy {
|
||||||
|
case bounded(Int)
|
||||||
|
case unbounded
|
||||||
|
case bufferingNewest(Int)
|
||||||
|
case bufferingOldest(Int)
|
||||||
|
}
|
||||||
|
|
||||||
|
let policy: _Policy
|
||||||
|
|
||||||
|
/// A policy for buffering elements until the limit is reached.
|
||||||
|
/// Then consumption of the upstream `AsyncSequence` will be paused until elements are consumed from the buffer.
|
||||||
|
/// If the limit is zero then no buffering policy is applied.
|
||||||
|
public static func bounded(_ limit: Int) -> Self {
|
||||||
|
precondition(limit >= 0, "The limit should be positive or equal to 0.")
|
||||||
|
return Self(policy: .bounded(limit))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A policy for buffering elements without limit.
|
||||||
|
public static var unbounded: Self {
|
||||||
|
return Self(policy: .unbounded)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A policy for buffering elements until the limit is reached.
|
||||||
|
/// After the limit is reached and a new element is produced by the upstream, the oldest buffered element will be discarded.
|
||||||
|
/// If the limit is zero then no buffering policy is applied.
|
||||||
|
public static func bufferingLatest(_ limit: Int) -> Self {
|
||||||
|
precondition(limit >= 0, "The limit should be positive or equal to 0.")
|
||||||
|
return Self(policy: .bufferingNewest(limit))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A policy for buffering elements until the limit is reached.
|
||||||
|
/// After the limit is reached and a new element is produced by the upstream, the latest buffered element will be discarded.
|
||||||
|
/// If the limit is zero then no buffering policy is applied.
|
||||||
|
public static func bufferingOldest(_ limit: Int) -> Self {
|
||||||
|
precondition(limit >= 0, "The limit should be positive or equal to 0.")
|
||||||
|
return Self(policy: .bufferingOldest(limit))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An `AsyncSequence` that buffers elements in regard to a policy.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct AsyncBufferSequence<Base: AsyncSequence & Sendable>: AsyncSequence {
|
||||||
|
enum StorageType {
|
||||||
|
case transparent(Base.AsyncIterator)
|
||||||
|
case bounded(storage: BoundedBufferStorage<Base>)
|
||||||
|
case unbounded(storage: UnboundedBufferStorage<Base>)
|
||||||
|
}
|
||||||
|
|
||||||
|
public typealias Element = Base.Element
|
||||||
|
public typealias AsyncIterator = Iterator
|
||||||
|
|
||||||
|
let base: Base
|
||||||
|
let policy: AsyncBufferSequencePolicy
|
||||||
|
|
||||||
|
init(
|
||||||
|
base: Base,
|
||||||
|
policy: AsyncBufferSequencePolicy
|
||||||
|
) {
|
||||||
|
self.base = base
|
||||||
|
self.policy = policy
|
||||||
|
}
|
||||||
|
|
||||||
|
public func makeAsyncIterator() -> Iterator {
|
||||||
|
let storageType: StorageType
|
||||||
|
switch self.policy.policy {
|
||||||
|
case .bounded(...0), .bufferingNewest(...0), .bufferingOldest(...0):
|
||||||
|
storageType = .transparent(self.base.makeAsyncIterator())
|
||||||
|
case .bounded(let limit):
|
||||||
|
storageType = .bounded(storage: BoundedBufferStorage(base: self.base, limit: limit))
|
||||||
|
case .unbounded:
|
||||||
|
storageType = .unbounded(storage: UnboundedBufferStorage(base: self.base, policy: .unlimited))
|
||||||
|
case .bufferingNewest(let limit):
|
||||||
|
storageType = .unbounded(storage: UnboundedBufferStorage(base: self.base, policy: .bufferingNewest(limit)))
|
||||||
|
case .bufferingOldest(let limit):
|
||||||
|
storageType = .unbounded(storage: UnboundedBufferStorage(base: self.base, policy: .bufferingOldest(limit)))
|
||||||
|
}
|
||||||
|
return Iterator(storageType: storageType)
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
var storageType: StorageType
|
||||||
|
|
||||||
|
public mutating func next() async rethrows -> Element? {
|
||||||
|
switch self.storageType {
|
||||||
|
case .transparent(var iterator):
|
||||||
|
let element = try await iterator.next()
|
||||||
|
self.storageType = .transparent(iterator)
|
||||||
|
return element
|
||||||
|
case .bounded(let storage):
|
||||||
|
return try await storage.next()?._rethrowGet()
|
||||||
|
case .unbounded(let storage):
|
||||||
|
return try await storage.next()?._rethrowGet()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncBufferSequence: Sendable where Base: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncBufferSequence.Iterator: Sendable {}
|
||||||
@@ -0,0 +1,329 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
import DequeModule
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
struct BoundedBufferStateMachine<Base: AsyncSequence> {
|
||||||
|
typealias Element = Base.Element
|
||||||
|
typealias SuspendedProducer = UnsafeContinuation<Void, Never>
|
||||||
|
typealias SuspendedConsumer = UnsafeContinuation<Result<Base.Element, Error>?, Never>
|
||||||
|
|
||||||
|
// We are using UnsafeTransfer here since we have to get the elements from the task
|
||||||
|
// into the consumer task. This is a transfer but we cannot prove this to the compiler at this point
|
||||||
|
// since next is not marked as transferring the return value.
|
||||||
|
fileprivate enum State {
|
||||||
|
case initial(base: Base)
|
||||||
|
case buffering(
|
||||||
|
task: Task<Void, Never>,
|
||||||
|
buffer: Deque<Result<UnsafeTransfer<Element>, Error>>,
|
||||||
|
suspendedProducer: SuspendedProducer?,
|
||||||
|
suspendedConsumer: SuspendedConsumer?
|
||||||
|
)
|
||||||
|
case modifying
|
||||||
|
case finished(buffer: Deque<Result<UnsafeTransfer<Element>, Error>>)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var state: State
|
||||||
|
private let limit: Int
|
||||||
|
|
||||||
|
init(base: Base, limit: Int) {
|
||||||
|
self.state = .initial(base: base)
|
||||||
|
self.limit = limit
|
||||||
|
}
|
||||||
|
|
||||||
|
var task: Task<Void, Never>? {
|
||||||
|
switch self.state {
|
||||||
|
case .buffering(let task, _, _, _):
|
||||||
|
return task
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func taskStarted(task: Task<Void, Never>) {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
self.state = .buffering(task: task, buffer: [], suspendedProducer: nil, suspendedConsumer: nil)
|
||||||
|
|
||||||
|
case .buffering:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func shouldSuspendProducer() -> Bool {
|
||||||
|
switch state {
|
||||||
|
case .initial:
|
||||||
|
preconditionFailure("Invalid state. The task should already be started.")
|
||||||
|
|
||||||
|
case .buffering(_, let buffer, .none, .none):
|
||||||
|
// we are either idle or the buffer is already in use (no awaiting consumer)
|
||||||
|
// if there are free slots, we should directly request the next element
|
||||||
|
return buffer.count >= self.limit
|
||||||
|
|
||||||
|
case .buffering(_, _, .none, .some):
|
||||||
|
// we have an awaiting consumer, we should not suspended the producer, we should
|
||||||
|
// directly request the next element
|
||||||
|
return false
|
||||||
|
|
||||||
|
case .buffering(_, _, .some, _):
|
||||||
|
preconditionFailure("Invalid state. There is already a suspended producer.")
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ProducerSuspendedAction {
|
||||||
|
case none
|
||||||
|
case resumeProducer
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func producerSuspended(continuation: SuspendedProducer) -> ProducerSuspendedAction {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
preconditionFailure("Invalid state. The task should already be started.")
|
||||||
|
|
||||||
|
case .buffering(let task, let buffer, .none, .none):
|
||||||
|
// we are either idle or the buffer is already in use (no awaiting consumer)
|
||||||
|
// if the buffer is available we resume the producer so it can we can request the next element
|
||||||
|
// otherwise we confirm the suspension
|
||||||
|
guard buffer.count < limit else {
|
||||||
|
self.state = .buffering(
|
||||||
|
task: task,
|
||||||
|
buffer: buffer,
|
||||||
|
suspendedProducer: continuation,
|
||||||
|
suspendedConsumer: nil
|
||||||
|
)
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
return .resumeProducer
|
||||||
|
|
||||||
|
case .buffering(_, let buffer, .none, .some):
|
||||||
|
// we have an awaiting consumer, we can resume the producer so the next element can be requested
|
||||||
|
precondition(
|
||||||
|
buffer.isEmpty,
|
||||||
|
"Invalid state. The buffer should be empty as we have an awaiting consumer already."
|
||||||
|
)
|
||||||
|
return .resumeProducer
|
||||||
|
|
||||||
|
case .buffering(_, _, .some, _):
|
||||||
|
preconditionFailure("Invalid state. There is already a suspended producer.")
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
return .resumeProducer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ElementProducedAction {
|
||||||
|
case none
|
||||||
|
case resumeConsumer(continuation: SuspendedConsumer, result: Result<Element, Error>)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func elementProduced(element: Element) -> ElementProducedAction {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
preconditionFailure("Invalid state. The task should already be started.")
|
||||||
|
|
||||||
|
case .buffering(let task, var buffer, .none, .none):
|
||||||
|
// we are either idle or the buffer is already in use (no awaiting consumer)
|
||||||
|
// we have to stack the new element or suspend the producer if the buffer is full
|
||||||
|
precondition(
|
||||||
|
buffer.count < limit,
|
||||||
|
"Invalid state. The buffer should be available for stacking a new element."
|
||||||
|
)
|
||||||
|
self.state = .modifying
|
||||||
|
buffer.append(.success(.init(element)))
|
||||||
|
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .buffering(let task, let buffer, .none, .some(let suspendedConsumer)):
|
||||||
|
// we have an awaiting consumer, we can resume it with the element and exit
|
||||||
|
precondition(buffer.isEmpty, "Invalid state. The buffer should be empty.")
|
||||||
|
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
|
||||||
|
return .resumeConsumer(continuation: suspendedConsumer, result: .success(element))
|
||||||
|
|
||||||
|
case .buffering(_, _, .some, _):
|
||||||
|
preconditionFailure("Invalid state. There should not be a suspended producer.")
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum FinishAction {
|
||||||
|
case none
|
||||||
|
case resumeConsumer(
|
||||||
|
continuation: UnsafeContinuation<Result<Base.Element, Error>?, Never>?
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func finish(error: Error?) -> FinishAction {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
preconditionFailure("Invalid state. The task should already be started.")
|
||||||
|
|
||||||
|
case .buffering(_, var buffer, .none, .none):
|
||||||
|
// we are either idle or the buffer is already in use (no awaiting consumer)
|
||||||
|
// if we have an error we stack it in the buffer so it can be consumed later
|
||||||
|
if let error {
|
||||||
|
buffer.append(.failure(error))
|
||||||
|
}
|
||||||
|
self.state = .finished(buffer: buffer)
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .buffering(_, let buffer, .none, .some(let suspendedConsumer)):
|
||||||
|
// we have an awaiting consumer, we can resume it
|
||||||
|
precondition(buffer.isEmpty, "Invalid state. The buffer should be empty.")
|
||||||
|
self.state = .finished(buffer: [])
|
||||||
|
return .resumeConsumer(continuation: suspendedConsumer)
|
||||||
|
|
||||||
|
case .buffering(_, _, .some, _):
|
||||||
|
preconditionFailure("Invalid state. There should not be a suspended producer.")
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum NextAction {
|
||||||
|
case startTask(base: Base)
|
||||||
|
case suspend
|
||||||
|
case returnResult(producerContinuation: UnsafeContinuation<Void, Never>?, result: Result<Element, Error>?)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func next() -> NextAction {
|
||||||
|
switch state {
|
||||||
|
case .initial(let base):
|
||||||
|
return .startTask(base: base)
|
||||||
|
|
||||||
|
case .buffering(_, let buffer, .none, .none) where buffer.isEmpty:
|
||||||
|
// we are idle, we must suspend the consumer
|
||||||
|
return .suspend
|
||||||
|
|
||||||
|
case .buffering(let task, var buffer, let suspendedProducer, .none):
|
||||||
|
// we have values in the buffer, we unstack the oldest one and resume a potential suspended producer
|
||||||
|
self.state = .modifying
|
||||||
|
let result = buffer.popFirst()!
|
||||||
|
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
|
||||||
|
return .returnResult(producerContinuation: suspendedProducer, result: result.map { $0.wrapped })
|
||||||
|
|
||||||
|
case .buffering(_, _, _, .some):
|
||||||
|
preconditionFailure("Invalid states. There is already a suspended consumer.")
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished(let buffer) where buffer.isEmpty:
|
||||||
|
return .returnResult(producerContinuation: nil, result: nil)
|
||||||
|
|
||||||
|
case .finished(var buffer):
|
||||||
|
self.state = .modifying
|
||||||
|
let result = buffer.popFirst()!
|
||||||
|
self.state = .finished(buffer: buffer)
|
||||||
|
return .returnResult(producerContinuation: nil, result: result.map { $0.wrapped })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum NextSuspendedAction {
|
||||||
|
case none
|
||||||
|
case returnResult(producerContinuation: UnsafeContinuation<Void, Never>?, result: Result<Element, Error>?)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func nextSuspended(continuation: SuspendedConsumer) -> NextSuspendedAction {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
preconditionFailure("Invalid state. The task should already be started.")
|
||||||
|
|
||||||
|
case .buffering(let task, let buffer, .none, .none) where buffer.isEmpty:
|
||||||
|
// we are idle, we confirm the suspension of the consumer
|
||||||
|
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: continuation)
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .buffering(let task, var buffer, let suspendedProducer, .none):
|
||||||
|
// we have values in the buffer, we unstack the oldest one and resume a potential suspended producer
|
||||||
|
self.state = .modifying
|
||||||
|
let result = buffer.popFirst()!
|
||||||
|
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
|
||||||
|
return .returnResult(producerContinuation: suspendedProducer, result: result.map { $0.wrapped })
|
||||||
|
|
||||||
|
case .buffering(_, _, _, .some):
|
||||||
|
preconditionFailure("Invalid states. There is already a suspended consumer.")
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished(let buffer) where buffer.isEmpty:
|
||||||
|
return .returnResult(producerContinuation: nil, result: nil)
|
||||||
|
|
||||||
|
case .finished(var buffer):
|
||||||
|
self.state = .modifying
|
||||||
|
let result = buffer.popFirst()!
|
||||||
|
self.state = .finished(buffer: buffer)
|
||||||
|
return .returnResult(producerContinuation: nil, result: result.map { $0.wrapped })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum InterruptedAction {
|
||||||
|
case none
|
||||||
|
case resumeProducerAndConsumer(
|
||||||
|
task: Task<Void, Never>,
|
||||||
|
producerContinuation: UnsafeContinuation<Void, Never>?,
|
||||||
|
consumerContinuation: UnsafeContinuation<Result<Base.Element, Error>?, Never>?
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func interrupted() -> InterruptedAction {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
self.state = .finished(buffer: [])
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .buffering(let task, _, let suspendedProducer, let suspendedConsumer):
|
||||||
|
self.state = .finished(buffer: [])
|
||||||
|
return .resumeProducerAndConsumer(
|
||||||
|
task: task,
|
||||||
|
producerContinuation: suspendedProducer,
|
||||||
|
consumerContinuation: suspendedConsumer
|
||||||
|
)
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
self.state = .finished(buffer: [])
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension BoundedBufferStateMachine: Sendable where Base: Sendable {}
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension BoundedBufferStateMachine.State: Sendable where Base: Sendable {}
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
final class BoundedBufferStorage<Base: AsyncSequence>: Sendable where Base: Sendable {
|
||||||
|
private let stateMachine: ManagedCriticalState<BoundedBufferStateMachine<Base>>
|
||||||
|
|
||||||
|
init(base: Base, limit: Int) {
|
||||||
|
self.stateMachine = ManagedCriticalState(BoundedBufferStateMachine(base: base, limit: limit))
|
||||||
|
}
|
||||||
|
|
||||||
|
func next() async -> Result<Base.Element, Error>? {
|
||||||
|
return await withTaskCancellationHandler {
|
||||||
|
let action: BoundedBufferStateMachine<Base>.NextAction? = self.stateMachine.withCriticalRegion {
|
||||||
|
stateMachine in
|
||||||
|
let action = stateMachine.next()
|
||||||
|
switch action {
|
||||||
|
case .startTask(let base):
|
||||||
|
self.startTask(stateMachine: &stateMachine, base: base)
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case .suspend:
|
||||||
|
return action
|
||||||
|
case .returnResult:
|
||||||
|
return action
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
case .suspend:
|
||||||
|
break
|
||||||
|
|
||||||
|
case .returnResult(let producerContinuation, let result):
|
||||||
|
producerContinuation?.resume()
|
||||||
|
return result
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
return await withUnsafeContinuation {
|
||||||
|
(continuation: UnsafeContinuation<Result<Base.Element, Error>?, Never>) in
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.nextSuspended(continuation: continuation)
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .returnResult(let producerContinuation, let result):
|
||||||
|
producerContinuation?.resume()
|
||||||
|
continuation.resume(returning: result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} onCancel: {
|
||||||
|
self.interrupted()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func startTask(
|
||||||
|
stateMachine: inout BoundedBufferStateMachine<Base>,
|
||||||
|
base: Base
|
||||||
|
) {
|
||||||
|
let task = Task {
|
||||||
|
do {
|
||||||
|
var iterator = base.makeAsyncIterator()
|
||||||
|
|
||||||
|
loop: while true {
|
||||||
|
let shouldSuspend = self.stateMachine.withCriticalRegion { stateMachine -> Bool in
|
||||||
|
return stateMachine.shouldSuspendProducer()
|
||||||
|
}
|
||||||
|
|
||||||
|
if shouldSuspend {
|
||||||
|
await withUnsafeContinuation { (continuation: UnsafeContinuation<Void, Never>) in
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.producerSuspended(continuation: continuation)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeProducer:
|
||||||
|
continuation.resume()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let element = try await iterator.next() else {
|
||||||
|
// the upstream is finished
|
||||||
|
break loop
|
||||||
|
}
|
||||||
|
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.elementProduced(element: element)
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeConsumer(let continuation, let result):
|
||||||
|
continuation.resume(returning: result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.finish(error: nil)
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeConsumer(let continuation):
|
||||||
|
continuation?.resume(returning: nil)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.finish(error: error)
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeConsumer(let continuation):
|
||||||
|
continuation?.resume(returning: .failure(error))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stateMachine.taskStarted(task: task)
|
||||||
|
}
|
||||||
|
|
||||||
|
func interrupted() {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.interrupted()
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeProducerAndConsumer(let task, let producerContinuation, let consumerContinuation):
|
||||||
|
task.cancel()
|
||||||
|
producerContinuation?.resume()
|
||||||
|
consumerContinuation?.resume(returning: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
self.interrupted()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,259 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
import DequeModule
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
struct UnboundedBufferStateMachine<Base: AsyncSequence> {
|
||||||
|
typealias Element = Base.Element
|
||||||
|
typealias SuspendedConsumer = UnsafeContinuation<Result<Element, Error>?, Never>
|
||||||
|
|
||||||
|
enum Policy {
|
||||||
|
case unlimited
|
||||||
|
case bufferingNewest(Int)
|
||||||
|
case bufferingOldest(Int)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We are using UnsafeTransfer here since we have to get the elements from the task
|
||||||
|
// into the consumer task. This is a transfer but we cannot prove this to the compiler at this point
|
||||||
|
// since next is not marked as transferring the return value.
|
||||||
|
fileprivate enum State {
|
||||||
|
case initial(base: Base)
|
||||||
|
case buffering(
|
||||||
|
task: Task<Void, Never>,
|
||||||
|
buffer: Deque<Result<UnsafeTransfer<Element>, Error>>,
|
||||||
|
suspendedConsumer: SuspendedConsumer?
|
||||||
|
)
|
||||||
|
case modifying
|
||||||
|
case finished(buffer: Deque<Result<UnsafeTransfer<Element>, Error>>)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var state: State
|
||||||
|
private let policy: Policy
|
||||||
|
|
||||||
|
init(base: Base, policy: Policy) {
|
||||||
|
self.state = .initial(base: base)
|
||||||
|
self.policy = policy
|
||||||
|
}
|
||||||
|
|
||||||
|
var task: Task<Void, Never>? {
|
||||||
|
switch self.state {
|
||||||
|
case .buffering(let task, _, _):
|
||||||
|
return task
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func taskStarted(task: Task<Void, Never>) {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
self.state = .buffering(task: task, buffer: [], suspendedConsumer: nil)
|
||||||
|
|
||||||
|
case .buffering:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ElementProducedAction {
|
||||||
|
case none
|
||||||
|
case resumeConsumer(
|
||||||
|
continuation: SuspendedConsumer,
|
||||||
|
result: Result<Element, Error>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func elementProduced(element: Element) -> ElementProducedAction {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
preconditionFailure("Invalid state. The task should already by started.")
|
||||||
|
|
||||||
|
case .buffering(let task, var buffer, .none):
|
||||||
|
// we are either idle or the buffer is already in use (no awaiting consumer)
|
||||||
|
// we have to apply the policy when stacking the new element
|
||||||
|
self.state = .modifying
|
||||||
|
switch self.policy {
|
||||||
|
case .unlimited:
|
||||||
|
buffer.append(.success(.init(element)))
|
||||||
|
case .bufferingNewest(let limit):
|
||||||
|
if buffer.count >= limit {
|
||||||
|
_ = buffer.popFirst()
|
||||||
|
}
|
||||||
|
buffer.append(.success(.init(element)))
|
||||||
|
case .bufferingOldest(let limit):
|
||||||
|
if buffer.count < limit {
|
||||||
|
buffer.append(.success(.init(element)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .buffering(let task, let buffer, .some(let suspendedConsumer)):
|
||||||
|
// we have an awaiting consumer, we can resume it with the element
|
||||||
|
precondition(buffer.isEmpty, "Invalid state. The buffer should be empty.")
|
||||||
|
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
|
||||||
|
return .resumeConsumer(
|
||||||
|
continuation: suspendedConsumer,
|
||||||
|
result: .success(element)
|
||||||
|
)
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum FinishAction {
|
||||||
|
case none
|
||||||
|
case resumeConsumer(continuation: SuspendedConsumer?)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func finish(error: Error?) -> FinishAction {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
preconditionFailure("Invalid state. The task should already by started.")
|
||||||
|
|
||||||
|
case .buffering(_, var buffer, .none):
|
||||||
|
// we are either idle or the buffer is already in use (no awaiting consumer)
|
||||||
|
// if we have an error we stack it in the buffer so it can be consumed later
|
||||||
|
if let error {
|
||||||
|
buffer.append(.failure(error))
|
||||||
|
}
|
||||||
|
self.state = .finished(buffer: buffer)
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .buffering(_, let buffer, let suspendedConsumer):
|
||||||
|
// we have an awaiting consumer, we can resume it with nil or the error
|
||||||
|
precondition(buffer.isEmpty, "Invalid state. The buffer should be empty.")
|
||||||
|
self.state = .finished(buffer: [])
|
||||||
|
return .resumeConsumer(continuation: suspendedConsumer)
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum NextAction {
|
||||||
|
case startTask(base: Base)
|
||||||
|
case suspend
|
||||||
|
case returnResult(Result<Element, Error>?)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func next() -> NextAction {
|
||||||
|
switch self.state {
|
||||||
|
case .initial(let base):
|
||||||
|
return .startTask(base: base)
|
||||||
|
|
||||||
|
case .buffering(_, let buffer, let suspendedConsumer) where buffer.isEmpty:
|
||||||
|
// we are idle, we have to suspend the consumer
|
||||||
|
precondition(suspendedConsumer == nil, "Invalid states. There is already a suspended consumer.")
|
||||||
|
return .suspend
|
||||||
|
|
||||||
|
case .buffering(let task, var buffer, let suspendedConsumer):
|
||||||
|
// the buffer is already in use, we can unstack a value and directly resume the consumer
|
||||||
|
precondition(suspendedConsumer == nil, "Invalid states. There is already a suspended consumer.")
|
||||||
|
self.state = .modifying
|
||||||
|
let result = buffer.popFirst()!
|
||||||
|
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
|
||||||
|
return .returnResult(result.map { $0.wrapped })
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished(let buffer) where buffer.isEmpty:
|
||||||
|
return .returnResult(nil)
|
||||||
|
|
||||||
|
case .finished(var buffer):
|
||||||
|
self.state = .modifying
|
||||||
|
let result = buffer.popFirst()!
|
||||||
|
self.state = .finished(buffer: buffer)
|
||||||
|
return .returnResult(result.map { $0.wrapped })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum NextSuspendedAction {
|
||||||
|
case none
|
||||||
|
case resumeConsumer(Result<Element, Error>?)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func nextSuspended(continuation: SuspendedConsumer) -> NextSuspendedAction {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
preconditionFailure("Invalid state. The task should already by started.")
|
||||||
|
|
||||||
|
case .buffering(let task, let buffer, let suspendedConsumer) where buffer.isEmpty:
|
||||||
|
// we are idle, we confirm the suspension of the consumer
|
||||||
|
precondition(suspendedConsumer == nil, "Invalid states. There is already a suspended consumer.")
|
||||||
|
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: continuation)
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .buffering(let task, var buffer, let suspendedConsumer):
|
||||||
|
// the buffer is already in use, we can unstack a value and directly resume the consumer
|
||||||
|
precondition(suspendedConsumer == nil, "Invalid states. There is already a suspended consumer.")
|
||||||
|
self.state = .modifying
|
||||||
|
let result = buffer.popFirst()!
|
||||||
|
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
|
||||||
|
return .resumeConsumer(result.map { $0.wrapped })
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished(let buffer) where buffer.isEmpty:
|
||||||
|
return .resumeConsumer(nil)
|
||||||
|
|
||||||
|
case .finished(var buffer):
|
||||||
|
self.state = .modifying
|
||||||
|
let result = buffer.popFirst()!
|
||||||
|
self.state = .finished(buffer: buffer)
|
||||||
|
return .resumeConsumer(result.map { $0.wrapped })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum InterruptedAction {
|
||||||
|
case none
|
||||||
|
case resumeConsumer(task: Task<Void, Never>, continuation: SuspendedConsumer?)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func interrupted() -> InterruptedAction {
|
||||||
|
switch self.state {
|
||||||
|
case .initial:
|
||||||
|
state = .finished(buffer: [])
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .buffering(let task, _, let suspendedConsumer):
|
||||||
|
self.state = .finished(buffer: [])
|
||||||
|
return .resumeConsumer(task: task, continuation: suspendedConsumer)
|
||||||
|
|
||||||
|
case .modifying:
|
||||||
|
preconditionFailure("Invalid state.")
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
self.state = .finished(buffer: [])
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension UnboundedBufferStateMachine: Sendable where Base: Sendable {}
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension UnboundedBufferStateMachine.State: Sendable where Base: Sendable {}
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
final class UnboundedBufferStorage<Base: AsyncSequence>: Sendable where Base: Sendable {
|
||||||
|
private let stateMachine: ManagedCriticalState<UnboundedBufferStateMachine<Base>>
|
||||||
|
|
||||||
|
init(base: Base, policy: UnboundedBufferStateMachine<Base>.Policy) {
|
||||||
|
self.stateMachine = ManagedCriticalState(UnboundedBufferStateMachine<Base>(base: base, policy: policy))
|
||||||
|
}
|
||||||
|
|
||||||
|
func next() async -> Result<Base.Element, Error>? {
|
||||||
|
return await withTaskCancellationHandler {
|
||||||
|
|
||||||
|
let action: UnboundedBufferStateMachine<Base>.NextAction? = self.stateMachine.withCriticalRegion {
|
||||||
|
stateMachine in
|
||||||
|
let action = stateMachine.next()
|
||||||
|
switch action {
|
||||||
|
case .startTask(let base):
|
||||||
|
self.startTask(stateMachine: &stateMachine, base: base)
|
||||||
|
return nil
|
||||||
|
case .suspend:
|
||||||
|
return action
|
||||||
|
case .returnResult:
|
||||||
|
return action
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
case .suspend:
|
||||||
|
break
|
||||||
|
case .returnResult(let result):
|
||||||
|
return result
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
return await withUnsafeContinuation {
|
||||||
|
(continuation: UnsafeContinuation<Result<Base.Element, Error>?, Never>) in
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.nextSuspended(continuation: continuation)
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeConsumer(let result):
|
||||||
|
continuation.resume(returning: result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} onCancel: {
|
||||||
|
self.interrupted()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func startTask(
|
||||||
|
stateMachine: inout UnboundedBufferStateMachine<Base>,
|
||||||
|
base: Base
|
||||||
|
) {
|
||||||
|
let task = Task {
|
||||||
|
do {
|
||||||
|
for try await element in base {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.elementProduced(element: element)
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeConsumer(let continuation, let result):
|
||||||
|
continuation.resume(returning: result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.finish(error: nil)
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeConsumer(let continuation):
|
||||||
|
continuation?.resume(returning: nil)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.finish(error: error)
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeConsumer(let continuation):
|
||||||
|
continuation?.resume(returning: .failure(error))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stateMachine.taskStarted(task: task)
|
||||||
|
}
|
||||||
|
|
||||||
|
func interrupted() {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.interrupted()
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeConsumer(let task, let continuation):
|
||||||
|
task.cancel()
|
||||||
|
continuation?.resume(returning: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
self.interrupted()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
/// A channel for sending elements from one task to another with back pressure.
|
||||||
|
///
|
||||||
|
/// The `AsyncChannel` class is intended to be used as a communication type between tasks,
|
||||||
|
/// particularly when one task produces values and another task consumes those values. The back
|
||||||
|
/// pressure applied by `send(_:)` via the suspension/resume ensures that
|
||||||
|
/// the production of values does not exceed the consumption of values from iteration. This method
|
||||||
|
/// suspends after enqueuing the event and is resumed when the next call to `next()`
|
||||||
|
/// on the `Iterator` is made, or when `finish()` is called from another Task.
|
||||||
|
/// As `finish()` induces a terminal state, there is no more need for a back pressure management.
|
||||||
|
/// This function does not suspend and will finish all the pending iterations.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||||
|
public typealias Element = Element
|
||||||
|
public typealias AsyncIterator = Iterator
|
||||||
|
|
||||||
|
let storage: ChannelStorage<Element, Never>
|
||||||
|
|
||||||
|
public init() {
|
||||||
|
self.storage = ChannelStorage()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends an element to an awaiting iteration. This function will resume when the next call to `next()` is made
|
||||||
|
/// or when a call to `finish()` is made from another task.
|
||||||
|
/// If the channel is already finished then this returns immediately.
|
||||||
|
/// If the task is cancelled, this function will resume without sending the element.
|
||||||
|
/// Other sending operations from other tasks will remain active.
|
||||||
|
public func send(_ element: Element) async {
|
||||||
|
await self.storage.send(element: element)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Immediately resumes all the suspended operations.
|
||||||
|
/// All subsequent calls to `next(_:)` will resume immediately.
|
||||||
|
public func finish() {
|
||||||
|
self.storage.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
public func makeAsyncIterator() -> Iterator {
|
||||||
|
Iterator(storage: self.storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
let storage: ChannelStorage<Element, Never>
|
||||||
|
|
||||||
|
public mutating func next() async -> Element? {
|
||||||
|
// Although the storage can throw, its usage in the context of an `AsyncChannel` guarantees it cannot.
|
||||||
|
// There is no public way of sending a failure to it.
|
||||||
|
try! await self.storage.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncChannel.Iterator: Sendable {}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
/// An error-throwing channel for sending elements from on task to another with back pressure.
|
||||||
|
///
|
||||||
|
/// The `AsyncThrowingChannel` class is intended to be used as a communication types between tasks,
|
||||||
|
/// particularly when one task produces values and another task consumes those values. The back
|
||||||
|
/// pressure applied by `send(_:)` via suspension/resume ensures that the production of values does
|
||||||
|
/// not exceed the consumption of values from iteration. This method suspends after enqueuing the event
|
||||||
|
/// and is resumed when the next call to `next()` on the `Iterator` is made, or when `finish()`/`fail(_:)` is called
|
||||||
|
/// from another Task. As `finish()` and `fail(_:)` induce a terminal state, there is no more need for a back pressure management.
|
||||||
|
/// Those functions do not suspend and will finish all the pending iterations.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: AsyncSequence, Sendable {
|
||||||
|
public typealias Element = Element
|
||||||
|
public typealias AsyncIterator = Iterator
|
||||||
|
|
||||||
|
let storage: ChannelStorage<Element, Failure>
|
||||||
|
|
||||||
|
public init() {
|
||||||
|
self.storage = ChannelStorage()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends an element to an awaiting iteration. This function will resume when the next call to `next()` is made
|
||||||
|
/// or when a call to `finish()` or `fail` is made from another task.
|
||||||
|
/// If the channel is already finished then this returns immediately.
|
||||||
|
/// If the task is cancelled, this function will resume without sending the element.
|
||||||
|
/// Other sending operations from other tasks will remain active.
|
||||||
|
public func send(_ element: Element) async {
|
||||||
|
await self.storage.send(element: element)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends an error to all awaiting iterations.
|
||||||
|
/// All subsequent calls to `next(_:)` will resume immediately.
|
||||||
|
public func fail(_ error: Error) where Failure == Error {
|
||||||
|
self.storage.finish(error: error)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Immediately resumes all the suspended operations.
|
||||||
|
/// All subsequent calls to `next(_:)` will resume immediately.
|
||||||
|
public func finish() {
|
||||||
|
self.storage.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
public func makeAsyncIterator() -> Iterator {
|
||||||
|
Iterator(storage: self.storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
let storage: ChannelStorage<Element, Failure>
|
||||||
|
|
||||||
|
public mutating func next() async throws -> Element? {
|
||||||
|
try await self.storage.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncThrowingChannel.Iterator: Sendable {}
|
||||||
@@ -0,0 +1,348 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
import OrderedCollections
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
struct ChannelStateMachine<Element: Sendable, Failure: Error>: Sendable {
|
||||||
|
private struct SuspendedProducer: Hashable, Sendable {
|
||||||
|
let id: UInt64
|
||||||
|
let continuation: UnsafeContinuation<Void, Never>?
|
||||||
|
let element: Element?
|
||||||
|
|
||||||
|
func hash(into hasher: inout Hasher) {
|
||||||
|
hasher.combine(self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func == (_ lhs: SuspendedProducer, _ rhs: SuspendedProducer) -> Bool {
|
||||||
|
return lhs.id == rhs.id
|
||||||
|
}
|
||||||
|
|
||||||
|
static func placeHolder(id: UInt64) -> SuspendedProducer {
|
||||||
|
SuspendedProducer(id: id, continuation: nil, element: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private struct SuspendedConsumer: Hashable, Sendable {
|
||||||
|
let id: UInt64
|
||||||
|
let continuation: UnsafeContinuation<Element?, any Error>?
|
||||||
|
|
||||||
|
func hash(into hasher: inout Hasher) {
|
||||||
|
hasher.combine(self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func == (_ lhs: SuspendedConsumer, _ rhs: SuspendedConsumer) -> Bool {
|
||||||
|
return lhs.id == rhs.id
|
||||||
|
}
|
||||||
|
|
||||||
|
static func placeHolder(id: UInt64) -> SuspendedConsumer {
|
||||||
|
SuspendedConsumer(id: id, continuation: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum Termination {
|
||||||
|
case finished
|
||||||
|
case failed(Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum State: Sendable {
|
||||||
|
case channeling(
|
||||||
|
suspendedProducers: OrderedSet<SuspendedProducer>,
|
||||||
|
cancelledProducers: Set<SuspendedProducer>,
|
||||||
|
suspendedConsumers: OrderedSet<SuspendedConsumer>,
|
||||||
|
cancelledConsumers: Set<SuspendedConsumer>
|
||||||
|
)
|
||||||
|
case terminated(Termination)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var state: State = .channeling(
|
||||||
|
suspendedProducers: [],
|
||||||
|
cancelledProducers: [],
|
||||||
|
suspendedConsumers: [],
|
||||||
|
cancelledConsumers: []
|
||||||
|
)
|
||||||
|
|
||||||
|
enum SendAction {
|
||||||
|
case resumeConsumer(continuation: UnsafeContinuation<Element?, any Error>?)
|
||||||
|
case suspend
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func send() -> SendAction {
|
||||||
|
switch self.state {
|
||||||
|
case .channeling(_, _, let suspendedConsumers, _) where suspendedConsumers.isEmpty:
|
||||||
|
// we are idle or waiting for consumers, we have to suspend the producer
|
||||||
|
return .suspend
|
||||||
|
|
||||||
|
case .channeling(let suspendedProducers, let cancelledProducers, var suspendedConsumers, let cancelledConsumers):
|
||||||
|
// we are waiting for producers, we can resume the first available consumer
|
||||||
|
let suspendedConsumer = suspendedConsumers.removeFirst()
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .resumeConsumer(continuation: suspendedConsumer.continuation)
|
||||||
|
|
||||||
|
case .terminated:
|
||||||
|
return .resumeConsumer(continuation: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum SendSuspendedAction {
|
||||||
|
case resumeProducer
|
||||||
|
case resumeProducerAndConsumer(continuation: UnsafeContinuation<Element?, any Error>?)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func sendSuspended(
|
||||||
|
continuation: UnsafeContinuation<Void, Never>,
|
||||||
|
element: Element,
|
||||||
|
producerID: UInt64
|
||||||
|
) -> SendSuspendedAction? {
|
||||||
|
switch self.state {
|
||||||
|
case .channeling(var suspendedProducers, var cancelledProducers, var suspendedConsumers, let cancelledConsumers):
|
||||||
|
let suspendedProducer = SuspendedProducer(id: producerID, continuation: continuation, element: element)
|
||||||
|
if let _ = cancelledProducers.remove(suspendedProducer) {
|
||||||
|
// the producer was already cancelled, we resume it
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .resumeProducer
|
||||||
|
}
|
||||||
|
|
||||||
|
guard suspendedConsumers.isEmpty else {
|
||||||
|
// we are waiting for producers
|
||||||
|
// we resume the first consumer
|
||||||
|
let suspendedConsumer = suspendedConsumers.removeFirst()
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .resumeProducerAndConsumer(continuation: suspendedConsumer.continuation)
|
||||||
|
}
|
||||||
|
// we are idle or waiting for consumers
|
||||||
|
// we stack the incoming producer in a suspended state
|
||||||
|
suspendedProducers.append(suspendedProducer)
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .terminated:
|
||||||
|
return .resumeProducer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum SendCancelledAction {
|
||||||
|
case none
|
||||||
|
case resumeProducer(continuation: UnsafeContinuation<Void, Never>?)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func sendCancelled(producerID: UInt64) -> SendCancelledAction {
|
||||||
|
switch self.state {
|
||||||
|
case .channeling(var suspendedProducers, var cancelledProducers, let suspendedConsumers, let cancelledConsumers):
|
||||||
|
// the cancelled producer might be part of the waiting list
|
||||||
|
let placeHolder = SuspendedProducer.placeHolder(id: producerID)
|
||||||
|
|
||||||
|
if let removed = suspendedProducers.remove(placeHolder) {
|
||||||
|
// the producer was cancelled after being added to the suspended ones, we resume it
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .resumeProducer(continuation: removed.continuation)
|
||||||
|
}
|
||||||
|
|
||||||
|
// the producer was cancelled before being added to the suspended ones
|
||||||
|
cancelledProducers.update(with: placeHolder)
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .terminated:
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum FinishAction {
|
||||||
|
case none
|
||||||
|
case resumeProducersAndConsumers(
|
||||||
|
producerSontinuations: [UnsafeContinuation<Void, Never>?],
|
||||||
|
consumerContinuations: [UnsafeContinuation<Element?, any Error>?]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func finish(error: Failure?) -> FinishAction {
|
||||||
|
switch self.state {
|
||||||
|
case .channeling(let suspendedProducers, _, let suspendedConsumers, _):
|
||||||
|
// no matter if we are idle, waiting for producers or waiting for consumers, we resume every thing that is suspended
|
||||||
|
if let error {
|
||||||
|
if suspendedConsumers.isEmpty {
|
||||||
|
self.state = .terminated(.failed(error))
|
||||||
|
} else {
|
||||||
|
self.state = .terminated(.finished)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.state = .terminated(.finished)
|
||||||
|
}
|
||||||
|
return .resumeProducersAndConsumers(
|
||||||
|
producerSontinuations: suspendedProducers.map { $0.continuation },
|
||||||
|
consumerContinuations: suspendedConsumers.map { $0.continuation }
|
||||||
|
)
|
||||||
|
|
||||||
|
case .terminated:
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum NextAction {
|
||||||
|
case resumeProducer(continuation: UnsafeContinuation<Void, Never>?, result: Result<Element?, Error>)
|
||||||
|
case suspend
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func next() -> NextAction {
|
||||||
|
switch self.state {
|
||||||
|
case .channeling(let suspendedProducers, _, _, _) where suspendedProducers.isEmpty:
|
||||||
|
// we are idle or waiting for producers, we must suspend
|
||||||
|
return .suspend
|
||||||
|
|
||||||
|
case .channeling(var suspendedProducers, let cancelledProducers, let suspendedConsumers, let cancelledConsumers):
|
||||||
|
// we are waiting for consumers, we can resume the first awaiting producer
|
||||||
|
let suspendedProducer = suspendedProducers.removeFirst()
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .resumeProducer(
|
||||||
|
continuation: suspendedProducer.continuation,
|
||||||
|
result: .success(suspendedProducer.element)
|
||||||
|
)
|
||||||
|
|
||||||
|
case .terminated(.failed(let error)):
|
||||||
|
self.state = .terminated(.finished)
|
||||||
|
return .resumeProducer(continuation: nil, result: .failure(error))
|
||||||
|
|
||||||
|
case .terminated:
|
||||||
|
return .resumeProducer(continuation: nil, result: .success(nil))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum NextSuspendedAction {
|
||||||
|
case resumeConsumer(element: Element?)
|
||||||
|
case resumeConsumerWithError(error: Error)
|
||||||
|
case resumeProducerAndConsumer(continuation: UnsafeContinuation<Void, Never>?, element: Element?)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func nextSuspended(
|
||||||
|
continuation: UnsafeContinuation<Element?, any Error>,
|
||||||
|
consumerID: UInt64
|
||||||
|
) -> NextSuspendedAction? {
|
||||||
|
switch self.state {
|
||||||
|
case .channeling(var suspendedProducers, let cancelledProducers, var suspendedConsumers, var cancelledConsumers):
|
||||||
|
let suspendedConsumer = SuspendedConsumer(id: consumerID, continuation: continuation)
|
||||||
|
if let _ = cancelledConsumers.remove(suspendedConsumer) {
|
||||||
|
// the consumer was already cancelled, we resume it
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .resumeConsumer(element: nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
guard suspendedProducers.isEmpty else {
|
||||||
|
// we are waiting for consumers
|
||||||
|
// we resume the first producer
|
||||||
|
let suspendedProducer = suspendedProducers.removeFirst()
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .resumeProducerAndConsumer(
|
||||||
|
continuation: suspendedProducer.continuation,
|
||||||
|
element: suspendedProducer.element
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// we are idle or waiting for producers
|
||||||
|
// we stack the incoming consumer in a suspended state
|
||||||
|
suspendedConsumers.append(suspendedConsumer)
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .terminated(.finished):
|
||||||
|
return .resumeConsumer(element: nil)
|
||||||
|
|
||||||
|
case .terminated(.failed(let error)):
|
||||||
|
self.state = .terminated(.finished)
|
||||||
|
return .resumeConsumerWithError(error: error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum NextCancelledAction {
|
||||||
|
case none
|
||||||
|
case resumeConsumer(continuation: UnsafeContinuation<Element?, any Error>?)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func nextCancelled(consumerID: UInt64) -> NextCancelledAction {
|
||||||
|
switch self.state {
|
||||||
|
case .channeling(let suspendedProducers, let cancelledProducers, var suspendedConsumers, var cancelledConsumers):
|
||||||
|
// the cancelled consumer might be part of the suspended ones
|
||||||
|
let placeHolder = SuspendedConsumer.placeHolder(id: consumerID)
|
||||||
|
|
||||||
|
if let removed = suspendedConsumers.remove(placeHolder) {
|
||||||
|
// the consumer was cancelled after being added to the suspended ones, we resume it
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .resumeConsumer(continuation: removed.continuation)
|
||||||
|
}
|
||||||
|
|
||||||
|
// the consumer was cancelled before being added to the suspended ones
|
||||||
|
cancelledConsumers.update(with: placeHolder)
|
||||||
|
self.state = .channeling(
|
||||||
|
suspendedProducers: suspendedProducers,
|
||||||
|
cancelledProducers: cancelledProducers,
|
||||||
|
suspendedConsumers: suspendedConsumers,
|
||||||
|
cancelledConsumers: cancelledConsumers
|
||||||
|
)
|
||||||
|
return .none
|
||||||
|
|
||||||
|
case .terminated:
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
struct ChannelStorage<Element: Sendable, Failure: Error>: Sendable {
|
||||||
|
private let stateMachine: ManagedCriticalState<ChannelStateMachine<Element, Failure>>
|
||||||
|
private let ids = ManagedCriticalState<UInt64>(0)
|
||||||
|
|
||||||
|
init() {
|
||||||
|
self.stateMachine = ManagedCriticalState(ChannelStateMachine())
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateId() -> UInt64 {
|
||||||
|
self.ids.withCriticalRegion { ids in
|
||||||
|
defer { ids &+= 1 }
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func send(element: Element) async {
|
||||||
|
// check if a suspension is needed
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.send()
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .suspend:
|
||||||
|
break
|
||||||
|
|
||||||
|
case .resumeConsumer(let continuation):
|
||||||
|
continuation?.resume(returning: element)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let producerID = self.generateId()
|
||||||
|
|
||||||
|
await withTaskCancellationHandler {
|
||||||
|
// a suspension is needed
|
||||||
|
await withUnsafeContinuation { (continuation: UnsafeContinuation<Void, Never>) in
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.sendSuspended(continuation: continuation, element: element, producerID: producerID)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeProducer:
|
||||||
|
continuation.resume()
|
||||||
|
case .resumeProducerAndConsumer(let consumerContinuation):
|
||||||
|
continuation.resume()
|
||||||
|
consumerContinuation?.resume(returning: element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} onCancel: {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.sendCancelled(producerID: producerID)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeProducer(let continuation):
|
||||||
|
continuation?.resume()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func finish(error: Failure? = nil) {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.finish(error: error)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeProducersAndConsumers(let producerContinuations, let consumerContinuations):
|
||||||
|
producerContinuations.forEach { $0?.resume() }
|
||||||
|
if let error {
|
||||||
|
consumerContinuations.forEach { $0?.resume(throwing: error) }
|
||||||
|
} else {
|
||||||
|
consumerContinuations.forEach { $0?.resume(returning: nil) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func next() async throws -> Element? {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .suspend:
|
||||||
|
break
|
||||||
|
|
||||||
|
case .resumeProducer(let producerContinuation, let result):
|
||||||
|
producerContinuation?.resume()
|
||||||
|
return try result._rethrowGet()
|
||||||
|
}
|
||||||
|
|
||||||
|
let consumerID = self.generateId()
|
||||||
|
|
||||||
|
return try await withTaskCancellationHandler {
|
||||||
|
try await withUnsafeThrowingContinuation { (continuation: UnsafeContinuation<Element?, any Error>) in
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.nextSuspended(
|
||||||
|
continuation: continuation,
|
||||||
|
consumerID: consumerID
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeConsumer(let element):
|
||||||
|
continuation.resume(returning: element)
|
||||||
|
case .resumeConsumerWithError(let error):
|
||||||
|
continuation.resume(throwing: error)
|
||||||
|
case .resumeProducerAndConsumer(let producerContinuation, let element):
|
||||||
|
producerContinuation?.resume()
|
||||||
|
continuation.resume(returning: element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} onCancel: {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.nextCancelled(consumerID: consumerID)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
case .resumeConsumer(let continuation):
|
||||||
|
continuation?.resume(returning: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,33 +20,39 @@
|
|||||||
/// Throws:
|
/// Throws:
|
||||||
/// ``combineLatest(_:_:)`` throws when one of the bases throws. If one of the bases threw any buffered and not yet consumed
|
/// ``combineLatest(_:_:)`` throws when one of the bases throws. If one of the bases threw any buffered and not yet consumed
|
||||||
/// values will be dropped.
|
/// values will be dropped.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public func combineLatest<
|
public func combineLatest<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence
|
Base2: AsyncSequence
|
||||||
>(_ base1: Base1, _ base2: Base2) -> AsyncCombineLatest2Sequence<Base1, Base2> where
|
>(_ base1: Base1, _ base2: Base2) -> AsyncCombineLatest2Sequence<Base1, Base2>
|
||||||
|
where
|
||||||
Base1: Sendable,
|
Base1: Sendable,
|
||||||
Base1.Element: Sendable,
|
Base1.Element: Sendable,
|
||||||
Base2: Sendable,
|
Base2: Sendable,
|
||||||
Base2.Element: Sendable {
|
Base2.Element: Sendable
|
||||||
|
{
|
||||||
AsyncCombineLatest2Sequence(base1, base2)
|
AsyncCombineLatest2Sequence(base1, base2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An `AsyncSequence` that combines the latest values produced from two asynchronous sequences into an asynchronous sequence of tuples.
|
/// An `AsyncSequence` that combines the latest values produced from two asynchronous sequences into an asynchronous sequence of tuples.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public struct AsyncCombineLatest2Sequence<
|
public struct AsyncCombineLatest2Sequence<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence
|
Base2: AsyncSequence
|
||||||
>: AsyncSequence, Sendable where
|
>: AsyncSequence, Sendable
|
||||||
|
where
|
||||||
Base1: Sendable,
|
Base1: Sendable,
|
||||||
Base1.Element: Sendable,
|
Base1.Element: Sendable,
|
||||||
Base2: Sendable,
|
Base2: Sendable,
|
||||||
Base2.Element: Sendable {
|
Base2.Element: Sendable
|
||||||
|
{
|
||||||
public typealias Element = (Base1.Element, Base2.Element)
|
public typealias Element = (Base1.Element, Base2.Element)
|
||||||
public typealias AsyncIterator = Iterator
|
public typealias AsyncIterator = Iterator
|
||||||
|
|
||||||
let base1: Base1
|
let base1: Base1
|
||||||
let base2: Base2
|
let base2: Base2
|
||||||
|
|
||||||
public init(_ base1: Base1, _ base2: Base2) {
|
init(_ base1: Base1, _ base2: Base2) {
|
||||||
self.base1 = base1
|
self.base1 = base1
|
||||||
self.base2 = base2
|
self.base2 = base2
|
||||||
}
|
}
|
||||||
@@ -87,3 +93,6 @@ public struct AsyncCombineLatest2Sequence<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncCombineLatest2Sequence.Iterator: Sendable {}
|
||||||
@@ -20,32 +20,38 @@
|
|||||||
/// Throws:
|
/// Throws:
|
||||||
/// ``combineLatest(_:_:_:)`` throws when one of the bases throws. If one of the bases threw any buffered and not yet consumed
|
/// ``combineLatest(_:_:_:)`` throws when one of the bases throws. If one of the bases threw any buffered and not yet consumed
|
||||||
/// values will be dropped.
|
/// values will be dropped.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public func combineLatest<
|
public func combineLatest<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence,
|
Base2: AsyncSequence,
|
||||||
Base3: AsyncSequence
|
Base3: AsyncSequence
|
||||||
>(_ base1: Base1, _ base2: Base2, _ base3: Base3) -> AsyncCombineLatest3Sequence<Base1, Base2, Base3> where
|
>(_ base1: Base1, _ base2: Base2, _ base3: Base3) -> AsyncCombineLatest3Sequence<Base1, Base2, Base3>
|
||||||
|
where
|
||||||
Base1: Sendable,
|
Base1: Sendable,
|
||||||
Base1.Element: Sendable,
|
Base1.Element: Sendable,
|
||||||
Base2: Sendable,
|
Base2: Sendable,
|
||||||
Base2.Element: Sendable,
|
Base2.Element: Sendable,
|
||||||
Base3: Sendable,
|
Base3: Sendable,
|
||||||
Base3.Element: Sendable {
|
Base3.Element: Sendable
|
||||||
|
{
|
||||||
AsyncCombineLatest3Sequence(base1, base2, base3)
|
AsyncCombineLatest3Sequence(base1, base2, base3)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An `AsyncSequence` that combines the latest values produced from three asynchronous sequences into an asynchronous sequence of tuples.
|
/// An `AsyncSequence` that combines the latest values produced from three asynchronous sequences into an asynchronous sequence of tuples.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public struct AsyncCombineLatest3Sequence<
|
public struct AsyncCombineLatest3Sequence<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence,
|
Base2: AsyncSequence,
|
||||||
Base3: AsyncSequence
|
Base3: AsyncSequence
|
||||||
>: AsyncSequence, Sendable where
|
>: AsyncSequence, Sendable
|
||||||
|
where
|
||||||
Base1: Sendable,
|
Base1: Sendable,
|
||||||
Base1.Element: Sendable,
|
Base1.Element: Sendable,
|
||||||
Base2: Sendable,
|
Base2: Sendable,
|
||||||
Base2.Element: Sendable,
|
Base2.Element: Sendable,
|
||||||
Base3: Sendable,
|
Base3: Sendable,
|
||||||
Base3.Element: Sendable {
|
Base3.Element: Sendable
|
||||||
|
{
|
||||||
public typealias Element = (Base1.Element, Base2.Element, Base3.Element)
|
public typealias Element = (Base1.Element, Base2.Element, Base3.Element)
|
||||||
public typealias AsyncIterator = Iterator
|
public typealias AsyncIterator = Iterator
|
||||||
|
|
||||||
@@ -60,7 +66,8 @@ public struct AsyncCombineLatest3Sequence<
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func makeAsyncIterator() -> AsyncIterator {
|
public func makeAsyncIterator() -> AsyncIterator {
|
||||||
Iterator(storage: .init(self.base1, self.base2, self.base3)
|
Iterator(
|
||||||
|
storage: .init(self.base1, self.base2, self.base3)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,3 +104,6 @@ public struct AsyncCombineLatest3Sequence<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncCombineLatest3Sequence.Iterator: Sendable {}
|
||||||
@@ -9,25 +9,32 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
@_implementationOnly import DequeModule
|
import DequeModule
|
||||||
|
|
||||||
/// State machine for combine latest
|
/// State machine for combine latest
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
struct CombineLatestStateMachine<
|
struct CombineLatestStateMachine<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence,
|
Base2: AsyncSequence,
|
||||||
Base3: AsyncSequence
|
Base3: AsyncSequence
|
||||||
>: Sendable where
|
>: Sendable
|
||||||
|
where
|
||||||
Base1: Sendable,
|
Base1: Sendable,
|
||||||
Base2: Sendable,
|
Base2: Sendable,
|
||||||
Base3: Sendable,
|
Base3: Sendable,
|
||||||
Base1.Element: Sendable,
|
Base1.Element: Sendable,
|
||||||
Base2.Element: Sendable,
|
Base2.Element: Sendable,
|
||||||
Base3.Element: Sendable {
|
Base3.Element: Sendable
|
||||||
typealias DownstreamContinuation = UnsafeContinuation<Result<(
|
{
|
||||||
Base1.Element,
|
typealias DownstreamContinuation = UnsafeContinuation<
|
||||||
Base2.Element,
|
Result<
|
||||||
Base3.Element?
|
(
|
||||||
)?, Error>, Never>
|
Base1.Element,
|
||||||
|
Base2.Element,
|
||||||
|
Base3.Element?
|
||||||
|
)?, Error
|
||||||
|
>, Never
|
||||||
|
>
|
||||||
|
|
||||||
private enum State: Sendable {
|
private enum State: Sendable {
|
||||||
/// Small wrapper for the state of an upstream sequence.
|
/// Small wrapper for the state of an upstream sequence.
|
||||||
@@ -115,7 +122,9 @@ struct CombineLatestStateMachine<
|
|||||||
|
|
||||||
case .combining:
|
case .combining:
|
||||||
// An iterator was deinitialized while we have a suspended continuation.
|
// 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, _):
|
case .waitingForDemand(let task, let upstreams, _):
|
||||||
// The iterator was dropped which signals that the consumer is finished.
|
// The iterator was dropped which signals that the consumer is finished.
|
||||||
@@ -124,7 +133,8 @@ struct CombineLatestStateMachine<
|
|||||||
|
|
||||||
return .cancelTaskAndUpstreamContinuations(
|
return .cancelTaskAndUpstreamContinuations(
|
||||||
task: task,
|
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 .upstreamThrew, .upstreamsFinished:
|
case .upstreamThrew, .upstreamsFinished:
|
||||||
@@ -180,7 +190,10 @@ struct CombineLatestStateMachine<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func childTaskSuspended(baseIndex: Int, continuation: UnsafeContinuation<Void, Error>) -> ChildTaskSuspendedAction? {
|
mutating func childTaskSuspended(
|
||||||
|
baseIndex: Int,
|
||||||
|
continuation: UnsafeContinuation<Void, Error>
|
||||||
|
) -> ChildTaskSuspendedAction? {
|
||||||
switch self.state {
|
switch self.state {
|
||||||
case .initial:
|
case .initial:
|
||||||
// Child tasks are only created after we transitioned to `zipping`
|
// Child tasks are only created after we transitioned to `zipping`
|
||||||
@@ -203,7 +216,9 @@ struct CombineLatestStateMachine<
|
|||||||
upstreams.2.continuation = continuation
|
upstreams.2.continuation = continuation
|
||||||
|
|
||||||
default:
|
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(
|
self.state = .waitingForDemand(
|
||||||
@@ -283,7 +298,10 @@ struct CombineLatestStateMachine<
|
|||||||
return .none
|
return .none
|
||||||
|
|
||||||
case .combining(let task, var upstreams, let downstreamContinuation, let buffer):
|
case .combining(let task, var upstreams, let downstreamContinuation, let buffer):
|
||||||
precondition(buffer.isEmpty, "Internal inconsistency current state \(self.state) and the buffer is not empty")
|
precondition(
|
||||||
|
buffer.isEmpty,
|
||||||
|
"Internal inconsistency current state \(self.state) and the buffer is not empty"
|
||||||
|
)
|
||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
|
|
||||||
switch result {
|
switch result {
|
||||||
@@ -302,8 +320,9 @@ struct CombineLatestStateMachine<
|
|||||||
|
|
||||||
// Implementing this for the two arities without variadic generics is a bit awkward sadly.
|
// Implementing this for the two arities without variadic generics is a bit awkward sadly.
|
||||||
if let first = upstreams.0.element,
|
if let first = upstreams.0.element,
|
||||||
let second = upstreams.1.element,
|
let second = upstreams.1.element,
|
||||||
let third = upstreams.2.element {
|
let third = upstreams.2.element
|
||||||
|
{
|
||||||
// We got an element from each upstream so we can resume the downstream now
|
// We got an element from each upstream so we can resume the downstream now
|
||||||
self.state = .waitingForDemand(
|
self.state = .waitingForDemand(
|
||||||
task: task,
|
task: task,
|
||||||
@@ -317,8 +336,9 @@ struct CombineLatestStateMachine<
|
|||||||
)
|
)
|
||||||
|
|
||||||
} else if let first = upstreams.0.element,
|
} else if let first = upstreams.0.element,
|
||||||
let second = upstreams.1.element,
|
let second = upstreams.1.element,
|
||||||
self.numberOfUpstreamSequences == 2 {
|
self.numberOfUpstreamSequences == 2
|
||||||
|
{
|
||||||
// We got an element from each upstream so we can resume the downstream now
|
// We got an element from each upstream so we can resume the downstream now
|
||||||
self.state = .waitingForDemand(
|
self.state = .waitingForDemand(
|
||||||
task: task,
|
task: task,
|
||||||
@@ -335,9 +355,21 @@ struct CombineLatestStateMachine<
|
|||||||
self.state = .combining(
|
self.state = .combining(
|
||||||
task: task,
|
task: task,
|
||||||
upstreams: (
|
upstreams: (
|
||||||
.init(continuation: upstreams.0.continuation, element: upstreams.0.element, isFinished: upstreams.0.isFinished),
|
.init(
|
||||||
.init(continuation: upstreams.1.continuation, element: upstreams.1.element, isFinished: upstreams.1.isFinished),
|
continuation: upstreams.0.continuation,
|
||||||
.init(continuation: upstreams.2.continuation, element: upstreams.2.element, isFinished: upstreams.2.isFinished)
|
element: upstreams.0.element,
|
||||||
|
isFinished: upstreams.0.isFinished
|
||||||
|
),
|
||||||
|
.init(
|
||||||
|
continuation: upstreams.1.continuation,
|
||||||
|
element: upstreams.1.element,
|
||||||
|
isFinished: upstreams.1.isFinished
|
||||||
|
),
|
||||||
|
.init(
|
||||||
|
continuation: upstreams.2.continuation,
|
||||||
|
element: upstreams.2.element,
|
||||||
|
isFinished: upstreams.2.isFinished
|
||||||
|
)
|
||||||
),
|
),
|
||||||
downstreamContinuation: downstreamContinuation,
|
downstreamContinuation: downstreamContinuation,
|
||||||
buffer: buffer
|
buffer: buffer
|
||||||
@@ -397,7 +429,9 @@ struct CombineLatestStateMachine<
|
|||||||
upstreams.2.isFinished = true
|
upstreams.2.isFinished = true
|
||||||
|
|
||||||
default:
|
default:
|
||||||
preconditionFailure("Internal inconsistency current state \(self.state) and received upstreamFinished() with base index \(baseIndex)")
|
preconditionFailure(
|
||||||
|
"Internal inconsistency current state \(self.state) and received upstreamFinished() with base index \(baseIndex)"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if upstreams.0.isFinished && upstreams.1.isFinished && upstreams.2.isFinished {
|
if upstreams.0.isFinished && upstreams.1.isFinished && upstreams.2.isFinished {
|
||||||
@@ -410,7 +444,9 @@ struct CombineLatestStateMachine<
|
|||||||
|
|
||||||
return .cancelTaskAndUpstreamContinuations(
|
return .cancelTaskAndUpstreamContinuations(
|
||||||
task: task,
|
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 }
|
||||||
)
|
)
|
||||||
} else if upstreams.0.isFinished && upstreams.1.isFinished && self.numberOfUpstreamSequences == 2 {
|
} else if upstreams.0.isFinished && upstreams.1.isFinished && self.numberOfUpstreamSequences == 2 {
|
||||||
// All upstreams finished we can transition to either finished or upstreamsFinished now
|
// All upstreams finished we can transition to either finished or upstreamsFinished now
|
||||||
@@ -422,7 +458,9 @@ struct CombineLatestStateMachine<
|
|||||||
|
|
||||||
return .cancelTaskAndUpstreamContinuations(
|
return .cancelTaskAndUpstreamContinuations(
|
||||||
task: task,
|
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 }
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
self.state = .waitingForDemand(
|
self.state = .waitingForDemand(
|
||||||
@@ -455,7 +493,9 @@ struct CombineLatestStateMachine<
|
|||||||
emptyUpstreamFinished = upstreams.2.element == nil
|
emptyUpstreamFinished = upstreams.2.element == nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
preconditionFailure("Internal inconsistency current state \(self.state) and received upstreamFinished() with base index \(baseIndex)")
|
preconditionFailure(
|
||||||
|
"Internal inconsistency current state \(self.state) and received upstreamFinished() with base index \(baseIndex)"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implementing this for the two arities without variadic generics is a bit awkward sadly.
|
// Implementing this for the two arities without variadic generics is a bit awkward sadly.
|
||||||
@@ -466,7 +506,9 @@ struct CombineLatestStateMachine<
|
|||||||
return .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
return .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
downstreamContinuation: downstreamContinuation,
|
downstreamContinuation: downstreamContinuation,
|
||||||
task: task,
|
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 }
|
||||||
)
|
)
|
||||||
|
|
||||||
} else if upstreams.0.isFinished && upstreams.1.isFinished && upstreams.2.isFinished {
|
} else if upstreams.0.isFinished && upstreams.1.isFinished && upstreams.2.isFinished {
|
||||||
@@ -476,7 +518,9 @@ struct CombineLatestStateMachine<
|
|||||||
return .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
return .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
downstreamContinuation: downstreamContinuation,
|
downstreamContinuation: downstreamContinuation,
|
||||||
task: task,
|
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 }
|
||||||
)
|
)
|
||||||
|
|
||||||
} else if upstreams.0.isFinished && upstreams.1.isFinished && self.numberOfUpstreamSequences == 2 {
|
} else if upstreams.0.isFinished && upstreams.1.isFinished && self.numberOfUpstreamSequences == 2 {
|
||||||
@@ -486,7 +530,9 @@ struct CombineLatestStateMachine<
|
|||||||
return .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
return .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
downstreamContinuation: downstreamContinuation,
|
downstreamContinuation: downstreamContinuation,
|
||||||
task: task,
|
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 }
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
self.state = .combining(
|
self.state = .combining(
|
||||||
@@ -530,7 +576,8 @@ struct CombineLatestStateMachine<
|
|||||||
preconditionFailure("Internal inconsistency current state \(self.state) and received upstreamThrew()")
|
preconditionFailure("Internal inconsistency current state \(self.state) and received upstreamThrew()")
|
||||||
|
|
||||||
case .upstreamsFinished:
|
case .upstreamsFinished:
|
||||||
preconditionFailure("Internal inconsistency current state \(self.state) and received upstreamThrew()")
|
// We need to tolerate multiple upstreams failing
|
||||||
|
return .none
|
||||||
|
|
||||||
case .waitingForDemand(let task, let upstreams, _):
|
case .waitingForDemand(let task, let upstreams, _):
|
||||||
// An upstream threw. We can cancel everything now and transition to finished.
|
// An upstream threw. We can cancel everything now and transition to finished.
|
||||||
@@ -541,7 +588,8 @@ struct CombineLatestStateMachine<
|
|||||||
|
|
||||||
return .cancelTaskAndUpstreamContinuations(
|
return .cancelTaskAndUpstreamContinuations(
|
||||||
task: task,
|
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 .combining(let task, let upstreams, let downstreamContinuation, _):
|
case .combining(let task, let upstreams, let downstreamContinuation, _):
|
||||||
@@ -554,7 +602,8 @@ struct CombineLatestStateMachine<
|
|||||||
downstreamContinuation: downstreamContinuation,
|
downstreamContinuation: downstreamContinuation,
|
||||||
error: error,
|
error: error,
|
||||||
task: task,
|
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 .upstreamThrew, .finished:
|
case .upstreamThrew, .finished:
|
||||||
@@ -585,7 +634,9 @@ struct CombineLatestStateMachine<
|
|||||||
mutating func cancelled() -> CancelledAction? {
|
mutating func cancelled() -> CancelledAction? {
|
||||||
switch self.state {
|
switch self.state {
|
||||||
case .initial:
|
case .initial:
|
||||||
preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")
|
state = .finished
|
||||||
|
|
||||||
|
return .none
|
||||||
|
|
||||||
case .waitingForDemand(let task, let upstreams, _):
|
case .waitingForDemand(let task, let upstreams, _):
|
||||||
// The downstream task got cancelled so we need to cancel our upstream Task
|
// The downstream task got cancelled so we need to cancel our upstream Task
|
||||||
@@ -594,7 +645,8 @@ struct CombineLatestStateMachine<
|
|||||||
|
|
||||||
return .cancelTaskAndUpstreamContinuations(
|
return .cancelTaskAndUpstreamContinuations(
|
||||||
task: task,
|
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 .combining(let task, let upstreams, let downstreamContinuation, _):
|
case .combining(let task, let upstreams, let downstreamContinuation, _):
|
||||||
@@ -605,7 +657,8 @@ struct CombineLatestStateMachine<
|
|||||||
return .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
return .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
downstreamContinuation: downstreamContinuation,
|
downstreamContinuation: downstreamContinuation,
|
||||||
task: task,
|
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 .upstreamsFinished:
|
case .upstreamsFinished:
|
||||||
@@ -658,19 +711,10 @@ struct CombineLatestStateMachine<
|
|||||||
// If not we have to transition to combining and need to resume all upstream continuations now
|
// If not we have to transition to combining and need to resume all upstream continuations now
|
||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
|
|
||||||
if let element = buffer.popFirst() {
|
guard let element = buffer.popFirst() else {
|
||||||
self.state = .waitingForDemand(
|
let upstreamContinuations = [
|
||||||
task: task,
|
upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation,
|
||||||
upstreams: upstreams,
|
].compactMap { $0 }
|
||||||
buffer: buffer
|
|
||||||
)
|
|
||||||
|
|
||||||
return .resumeContinuation(
|
|
||||||
downstreamContinuation: continuation,
|
|
||||||
result: .success(element)
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
let upstreamContinuations = [upstreams.0.continuation, upstreams.1.continuation, upstreams.2.continuation].compactMap { $0 }
|
|
||||||
upstreams.0.continuation = nil
|
upstreams.0.continuation = nil
|
||||||
upstreams.1.continuation = nil
|
upstreams.1.continuation = nil
|
||||||
upstreams.2.continuation = nil
|
upstreams.2.continuation = nil
|
||||||
@@ -686,22 +730,31 @@ struct CombineLatestStateMachine<
|
|||||||
upstreamContinuation: upstreamContinuations
|
upstreamContinuation: upstreamContinuations
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
self.state = .waitingForDemand(
|
||||||
|
task: task,
|
||||||
|
upstreams: upstreams,
|
||||||
|
buffer: buffer
|
||||||
|
)
|
||||||
|
|
||||||
|
return .resumeContinuation(
|
||||||
|
downstreamContinuation: continuation,
|
||||||
|
result: .success(element)
|
||||||
|
)
|
||||||
|
|
||||||
case .upstreamsFinished(var buffer):
|
case .upstreamsFinished(var buffer):
|
||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
|
|
||||||
if let element = buffer.popFirst() {
|
guard let element = buffer.popFirst() else {
|
||||||
self.state = .upstreamsFinished(buffer: buffer)
|
|
||||||
|
|
||||||
return .resumeContinuation(
|
|
||||||
downstreamContinuation: continuation,
|
|
||||||
result: .success(element)
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
self.state = .finished
|
self.state = .finished
|
||||||
|
|
||||||
return .resumeDownstreamContinuationWithNil(continuation)
|
return .resumeDownstreamContinuationWithNil(continuation)
|
||||||
}
|
}
|
||||||
|
self.state = .upstreamsFinished(buffer: buffer)
|
||||||
|
|
||||||
|
return .resumeContinuation(
|
||||||
|
downstreamContinuation: continuation,
|
||||||
|
result: .success(element)
|
||||||
|
)
|
||||||
|
|
||||||
case .upstreamThrew(let error):
|
case .upstreamThrew(let error):
|
||||||
// One of the upstreams threw and we have to return this error now.
|
// One of the upstreams threw and we have to return this error now.
|
||||||
|
|||||||
@@ -9,17 +9,20 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
final class CombineLatestStorage<
|
final class CombineLatestStorage<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence,
|
Base2: AsyncSequence,
|
||||||
Base3: AsyncSequence
|
Base3: AsyncSequence
|
||||||
>: Sendable where
|
>: Sendable
|
||||||
|
where
|
||||||
Base1: Sendable,
|
Base1: Sendable,
|
||||||
Base1.Element: Sendable,
|
Base1.Element: Sendable,
|
||||||
Base2: Sendable,
|
Base2: Sendable,
|
||||||
Base2.Element: Sendable,
|
Base2.Element: Sendable,
|
||||||
Base3: Sendable,
|
Base3: Sendable,
|
||||||
Base3.Element: Sendable {
|
Base3.Element: Sendable
|
||||||
|
{
|
||||||
typealias StateMachine = CombineLatestStateMachine<Base1, Base2, Base3>
|
typealias StateMachine = CombineLatestStateMachine<Base1, Base2, Base3>
|
||||||
|
|
||||||
private let stateMachine: ManagedCriticalState<StateMachine>
|
private let stateMachine: ManagedCriticalState<StateMachine>
|
||||||
@@ -48,7 +51,7 @@ final class CombineLatestStorage<
|
|||||||
func next() async rethrows -> (Base1.Element, Base2.Element, Base3.Element?)? {
|
func next() async rethrows -> (Base1.Element, Base2.Element, Base3.Element?)? {
|
||||||
try await withTaskCancellationHandler {
|
try await withTaskCancellationHandler {
|
||||||
let result = await withUnsafeContinuation { continuation in
|
let result = await withUnsafeContinuation { continuation in
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action: StateMachine.NextAction? = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.next(for: continuation)
|
let action = stateMachine.next(for: continuation)
|
||||||
switch action {
|
switch action {
|
||||||
case .startTask(let base1, let base2, let base3):
|
case .startTask(let base1, let base2, let base3):
|
||||||
@@ -58,47 +61,67 @@ final class CombineLatestStorage<
|
|||||||
base1: base1,
|
base1: base1,
|
||||||
base2: base2,
|
base2: base2,
|
||||||
base3: base3,
|
base3: base3,
|
||||||
downStreamContinuation: continuation
|
downstreamContinuation: continuation
|
||||||
)
|
)
|
||||||
|
return nil
|
||||||
|
|
||||||
case .resumeContinuation(let downstreamContinuation, let result):
|
case .resumeContinuation:
|
||||||
downstreamContinuation.resume(returning: result)
|
return action
|
||||||
|
|
||||||
case .resumeUpstreamContinuations(let upstreamContinuations):
|
case .resumeUpstreamContinuations:
|
||||||
// bases can be iterated over for 1 iteration so their next value can be retrieved
|
return action
|
||||||
upstreamContinuations.forEach { $0.resume() }
|
|
||||||
|
|
||||||
case .resumeDownstreamContinuationWithNil(let continuation):
|
case .resumeDownstreamContinuationWithNil:
|
||||||
// the async sequence is already finished, immediately resuming
|
return action
|
||||||
continuation.resume(returning: .success(nil))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
case .resumeContinuation(let downstreamContinuation, let result):
|
||||||
|
downstreamContinuation.resume(returning: result)
|
||||||
|
|
||||||
|
case .resumeUpstreamContinuations(let upstreamContinuations):
|
||||||
|
// bases can be iterated over for 1 iteration so their next value can be retrieved
|
||||||
|
upstreamContinuations.forEach { $0.resume() }
|
||||||
|
|
||||||
|
case .resumeDownstreamContinuationWithNil(let continuation):
|
||||||
|
// the async sequence is already finished, immediately resuming
|
||||||
|
continuation.resume(returning: .success(nil))
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return try result._rethrowGet()
|
return try result._rethrowGet()
|
||||||
|
|
||||||
} onCancel: {
|
} onCancel: {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.cancelled()
|
stateMachine.cancelled()
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
case .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
let downstreamContinuation,
|
let downstreamContinuation,
|
||||||
let task,
|
let task,
|
||||||
let upstreamContinuations
|
let upstreamContinuations
|
||||||
):
|
):
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .success(nil))
|
downstreamContinuation.resume(returning: .success(nil))
|
||||||
|
|
||||||
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,7 +131,7 @@ final class CombineLatestStorage<
|
|||||||
base1: Base1,
|
base1: Base1,
|
||||||
base2: Base2,
|
base2: Base2,
|
||||||
base3: Base3?,
|
base3: Base3?,
|
||||||
downStreamContinuation: StateMachine.DownstreamContinuation
|
downstreamContinuation: StateMachine.DownstreamContinuation
|
||||||
) {
|
) {
|
||||||
// This creates a new `Task` that is iterating the upstream
|
// This creates a new `Task` that is iterating the upstream
|
||||||
// sequences. We must store it to cancel it at the right times.
|
// sequences. We must store it to cancel it at the right times.
|
||||||
@@ -124,33 +147,33 @@ final class CombineLatestStorage<
|
|||||||
// element from upstream. This continuation is only resumed
|
// element from upstream. This continuation is only resumed
|
||||||
// if the downstream consumer called `next` to signal his demand.
|
// if the downstream consumer called `next` to signal his demand.
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
try await withUnsafeThrowingContinuation { continuation in
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.childTaskSuspended(baseIndex: 0, continuation: continuation)
|
stateMachine.childTaskSuspended(baseIndex: 0, continuation: continuation)
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let upstreamContinuation):
|
case .resumeContinuation(let upstreamContinuation):
|
||||||
upstreamContinuation.resume()
|
upstreamContinuation.resume()
|
||||||
|
|
||||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||||
upstreamContinuation.resume(throwing: error)
|
upstreamContinuation.resume(throwing: error)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let element1 = try await base1Iterator.next() {
|
if let element1 = try await base1Iterator.next() {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.elementProduced((element1, nil, nil))
|
stateMachine.elementProduced((element1, nil, nil))
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let downstreamContinuation, let result):
|
case .resumeContinuation(let downstreamContinuation, let result):
|
||||||
downstreamContinuation.resume(returning: result)
|
downstreamContinuation.resume(returning: result)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
@@ -191,33 +214,33 @@ final class CombineLatestStorage<
|
|||||||
// element from upstream. This continuation is only resumed
|
// element from upstream. This continuation is only resumed
|
||||||
// if the downstream consumer called `next` to signal his demand.
|
// if the downstream consumer called `next` to signal his demand.
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
try await withUnsafeThrowingContinuation { continuation in
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.childTaskSuspended(baseIndex: 1, continuation: continuation)
|
stateMachine.childTaskSuspended(baseIndex: 1, continuation: continuation)
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let upstreamContinuation):
|
case .resumeContinuation(let upstreamContinuation):
|
||||||
upstreamContinuation.resume()
|
upstreamContinuation.resume()
|
||||||
|
|
||||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||||
upstreamContinuation.resume(throwing: error)
|
upstreamContinuation.resume(throwing: error)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let element2 = try await base1Iterator.next() {
|
if let element2 = try await base1Iterator.next() {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.elementProduced((nil, element2, nil))
|
stateMachine.elementProduced((nil, element2, nil))
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let downstreamContinuation, let result):
|
case .resumeContinuation(let downstreamContinuation, let result):
|
||||||
downstreamContinuation.resume(returning: result)
|
downstreamContinuation.resume(returning: result)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
@@ -259,33 +282,33 @@ final class CombineLatestStorage<
|
|||||||
// element from upstream. This continuation is only resumed
|
// element from upstream. This continuation is only resumed
|
||||||
// if the downstream consumer called `next` to signal his demand.
|
// if the downstream consumer called `next` to signal his demand.
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
try await withUnsafeThrowingContinuation { continuation in
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.childTaskSuspended(baseIndex: 2, continuation: continuation)
|
stateMachine.childTaskSuspended(baseIndex: 2, continuation: continuation)
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let upstreamContinuation):
|
case .resumeContinuation(let upstreamContinuation):
|
||||||
upstreamContinuation.resume()
|
upstreamContinuation.resume()
|
||||||
|
|
||||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||||
upstreamContinuation.resume(throwing: error)
|
upstreamContinuation.resume(throwing: error)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let element3 = try await base1Iterator.next() {
|
if let element3 = try await base1Iterator.next() {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.elementProduced((nil, nil, element3))
|
stateMachine.elementProduced((nil, nil, element3))
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let downstreamContinuation, let result):
|
case .resumeContinuation(let downstreamContinuation, let result):
|
||||||
downstreamContinuation.resume(returning: result)
|
downstreamContinuation.resume(returning: result)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
@@ -319,18 +342,19 @@ final class CombineLatestStorage<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
while !group.isEmpty {
|
||||||
try await group.waitForAll()
|
do {
|
||||||
} catch {
|
try await group.next()
|
||||||
// One of the upstream sequences threw an error
|
} catch {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
// One of the upstream sequences threw an error
|
||||||
let action = stateMachine.upstreamThrew(error)
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.upstreamThrew(error)
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
|
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
|
||||||
let downstreamContinuation,
|
let downstreamContinuation,
|
||||||
let error,
|
let error,
|
||||||
@@ -339,19 +363,17 @@ final class CombineLatestStorage<
|
|||||||
):
|
):
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .failure(error))
|
downstreamContinuation.resume(returning: .failure(error))
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
group.cancelAll()
|
group.cancelAll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stateMachine.taskIsStarted(task: task, downstreamContinuation: downStreamContinuation)
|
stateMachine.taskIsStarted(task: task, downstreamContinuation: downstreamContinuation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,91 +9,102 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequence {
|
extension AsyncSequence {
|
||||||
/// Creates an asynchronous sequence that emits the latest element after a given quiescence period
|
/// Creates an asynchronous sequence that emits the latest element after a given quiescence period
|
||||||
/// has elapsed by using a specified Clock.
|
/// has elapsed by using a specified Clock.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public func debounce<C: Clock>(for interval: C.Instant.Duration, tolerance: C.Instant.Duration? = nil, clock: C) -> AsyncDebounceSequence<Self, C> where Self: Sendable {
|
public func debounce<C: Clock>(
|
||||||
AsyncDebounceSequence(self, interval: interval, tolerance: tolerance, clock: clock)
|
for interval: C.Instant.Duration,
|
||||||
}
|
tolerance: C.Instant.Duration? = nil,
|
||||||
|
clock: C
|
||||||
|
) -> AsyncDebounceSequence<Self, C> where Self: Sendable, Self.Element: Sendable {
|
||||||
|
AsyncDebounceSequence(self, interval: interval, tolerance: tolerance, clock: clock)
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates an asynchronous sequence that emits the latest element after a given quiescence period
|
/// Creates an asynchronous sequence that emits the latest element after a given quiescence period
|
||||||
/// has elapsed.
|
/// has elapsed.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public func debounce(for interval: Duration, tolerance: Duration? = nil) -> AsyncDebounceSequence<Self, ContinuousClock> where Self: Sendable {
|
public func debounce(
|
||||||
self.debounce(for: interval, tolerance: tolerance, clock: .continuous)
|
for interval: Duration,
|
||||||
}
|
tolerance: Duration? = nil
|
||||||
|
) -> AsyncDebounceSequence<Self, ContinuousClock> where Self: Sendable, Self.Element: Sendable {
|
||||||
|
self.debounce(for: interval, tolerance: tolerance, clock: .continuous)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An `AsyncSequence` that emits the latest element after a given quiescence period
|
/// An `AsyncSequence` that emits the latest element after a given quiescence period
|
||||||
/// has elapsed.
|
/// has elapsed.
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public struct AsyncDebounceSequence<Base: AsyncSequence, C: Clock>: Sendable where Base: Sendable {
|
public struct AsyncDebounceSequence<Base: AsyncSequence & Sendable, C: Clock>: Sendable where Base.Element: Sendable {
|
||||||
private let base: Base
|
private let base: Base
|
||||||
private let clock: C
|
private let clock: C
|
||||||
private let interval: C.Instant.Duration
|
private let interval: C.Instant.Duration
|
||||||
private let tolerance: C.Instant.Duration?
|
private let tolerance: C.Instant.Duration?
|
||||||
|
|
||||||
/// Initializes a new ``AsyncDebounceSequence``.
|
/// Initializes a new ``AsyncDebounceSequence``.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - base: The base sequence.
|
/// - base: The base sequence.
|
||||||
/// - interval: The interval to debounce.
|
/// - interval: The interval to debounce.
|
||||||
/// - tolerance: The tolerance of the clock.
|
/// - tolerance: The tolerance of the clock.
|
||||||
/// - clock: The clock.
|
/// - clock: The clock.
|
||||||
public init(_ base: Base, interval: C.Instant.Duration, tolerance: C.Instant.Duration?, clock: C) {
|
init(_ base: Base, interval: C.Instant.Duration, tolerance: C.Instant.Duration?, clock: C) {
|
||||||
self.base = base
|
self.base = base
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.tolerance = tolerance
|
self.tolerance = tolerance
|
||||||
self.clock = clock
|
self.clock = clock
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
extension AsyncDebounceSequence: AsyncSequence {
|
extension AsyncDebounceSequence: AsyncSequence {
|
||||||
public typealias Element = Base.Element
|
public typealias Element = Base.Element
|
||||||
|
|
||||||
public func makeAsyncIterator() -> AsyncIterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
let storage = DebounceStorage(
|
let storage = DebounceStorage(
|
||||||
base: self.base,
|
base: self.base,
|
||||||
interval: self.interval,
|
interval: self.interval,
|
||||||
tolerance: self.tolerance,
|
tolerance: self.tolerance,
|
||||||
clock: self.clock
|
clock: self.clock
|
||||||
)
|
)
|
||||||
return AsyncIterator(storage: storage)
|
return Iterator(storage: storage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
extension AsyncDebounceSequence {
|
extension AsyncDebounceSequence {
|
||||||
public struct AsyncIterator: AsyncIteratorProtocol {
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
/// This class is needed to hook the deinit to observe once all references to the ``AsyncIterator`` are dropped.
|
/// This class is needed to hook the deinit to observe once all references to the ``AsyncIterator`` are dropped.
|
||||||
///
|
///
|
||||||
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
|
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
|
||||||
final class InternalClass: Sendable {
|
final class InternalClass: Sendable {
|
||||||
private let storage: DebounceStorage<Base, C>
|
private let storage: DebounceStorage<Base, C>
|
||||||
|
|
||||||
fileprivate init(storage: DebounceStorage<Base, C>) {
|
fileprivate init(storage: DebounceStorage<Base, C>) {
|
||||||
self.storage = storage
|
self.storage = storage
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
self.storage.iteratorDeinitialized()
|
self.storage.iteratorDeinitialized()
|
||||||
}
|
}
|
||||||
|
|
||||||
func next() async rethrows -> Element? {
|
func next() async rethrows -> Element? {
|
||||||
try await self.storage.next()
|
try await self.storage.next()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let internalClass: InternalClass
|
|
||||||
|
|
||||||
fileprivate init(storage: DebounceStorage<Base, C>) {
|
|
||||||
self.internalClass = InternalClass(storage: storage)
|
|
||||||
}
|
|
||||||
|
|
||||||
public mutating func next() async rethrows -> Element? {
|
|
||||||
try await self.internalClass.next()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let internalClass: InternalClass
|
||||||
|
|
||||||
|
fileprivate init(storage: DebounceStorage<Base, C>) {
|
||||||
|
self.internalClass = InternalClass(storage: storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
public mutating func next() async rethrows -> Element? {
|
||||||
|
try await self.internalClass.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncDebounceSequence.Iterator: Sendable {}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -10,287 +10,318 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
final class DebounceStorage<Base: AsyncSequence, C: Clock>: @unchecked Sendable where Base: Sendable {
|
final class DebounceStorage<Base: AsyncSequence & Sendable, C: Clock>: Sendable where Base.Element: Sendable {
|
||||||
typealias Element = Base.Element
|
typealias Element = Base.Element
|
||||||
|
|
||||||
/// The state machine protected with a lock.
|
/// The state machine protected with a lock.
|
||||||
private let stateMachine: ManagedCriticalState<DebounceStateMachine<Base, C>>
|
private let stateMachine: ManagedCriticalState<DebounceStateMachine<Base, C>>
|
||||||
/// The interval to debounce.
|
/// The interval to debounce.
|
||||||
private let interval: C.Instant.Duration
|
private let interval: C.Instant.Duration
|
||||||
/// The tolerance for the clock.
|
/// The tolerance for the clock.
|
||||||
private let tolerance: C.Instant.Duration?
|
private let tolerance: C.Instant.Duration?
|
||||||
/// The clock.
|
/// The clock.
|
||||||
private let clock: C
|
private let clock: C
|
||||||
|
|
||||||
init(base: Base, interval: C.Instant.Duration, tolerance: C.Instant.Duration?, clock: C) {
|
init(base: Base, interval: C.Instant.Duration, tolerance: C.Instant.Duration?, clock: C) {
|
||||||
self.stateMachine = .init(.init(base: base, clock: clock, interval: interval))
|
self.stateMachine = .init(.init(base: base, clock: clock, interval: interval))
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.tolerance = tolerance
|
self.tolerance = tolerance
|
||||||
self.clock = clock
|
self.clock = clock
|
||||||
|
}
|
||||||
|
|
||||||
|
func iteratorDeinitialized() {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { $0.iteratorDeinitialized() }
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .cancelTaskAndUpstreamAndClockContinuations(
|
||||||
|
let task,
|
||||||
|
let upstreamContinuation,
|
||||||
|
let clockContinuation
|
||||||
|
):
|
||||||
|
upstreamContinuation?.resume(throwing: CancellationError())
|
||||||
|
clockContinuation?.resume(throwing: CancellationError())
|
||||||
|
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func iteratorDeinitialized() {
|
func next() async rethrows -> Element? {
|
||||||
let action = self.stateMachine.withCriticalRegion { $0.iteratorDeinitialized() }
|
// We need to handle cancellation here because we are creating a continuation
|
||||||
|
// and because we need to cancel the `Task` we created to consume the upstream
|
||||||
|
return try await withTaskCancellationHandler {
|
||||||
|
// We always suspend since we can never return an element right away
|
||||||
|
|
||||||
|
let result: Result<Element?, Error> = await withUnsafeContinuation { continuation in
|
||||||
|
let action: DebounceStateMachine<Base, C>.NextAction? = self.stateMachine.withCriticalRegion {
|
||||||
|
let action = $0.next(for: continuation)
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .startTask(let base):
|
||||||
|
self.startTask(
|
||||||
|
stateMachine: &$0,
|
||||||
|
base: base,
|
||||||
|
downstreamContinuation: continuation
|
||||||
|
)
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case .resumeUpstreamContinuation:
|
||||||
|
return action
|
||||||
|
|
||||||
|
case .resumeUpstreamAndClockContinuation:
|
||||||
|
return action
|
||||||
|
|
||||||
|
case .resumeDownstreamContinuationWithNil:
|
||||||
|
return action
|
||||||
|
|
||||||
|
case .resumeDownstreamContinuationWithError:
|
||||||
|
return action
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .cancelTaskAndUpstreamAndClockContinuations(
|
case .startTask:
|
||||||
let task,
|
// We are handling the startTask in the lock already because we want to avoid
|
||||||
let upstreamContinuation,
|
// other inputs interleaving while starting the task
|
||||||
let clockContinuation
|
fatalError("Internal inconsistency")
|
||||||
):
|
|
||||||
upstreamContinuation?.resume(throwing: CancellationError())
|
|
||||||
clockContinuation?.resume(throwing: CancellationError())
|
|
||||||
|
|
||||||
task.cancel()
|
case .resumeUpstreamContinuation(let upstreamContinuation):
|
||||||
|
// This is signalling the upstream task that is consuming the upstream
|
||||||
|
// sequence to signal demand.
|
||||||
|
upstreamContinuation?.resume(returning: ())
|
||||||
|
|
||||||
|
case .resumeUpstreamAndClockContinuation(let upstreamContinuation, let clockContinuation, let deadline):
|
||||||
|
// This is signalling the upstream task that is consuming the upstream
|
||||||
|
// sequence to signal demand and start the clock task.
|
||||||
|
upstreamContinuation?.resume(returning: ())
|
||||||
|
clockContinuation?.resume(returning: deadline)
|
||||||
|
|
||||||
|
case .resumeDownstreamContinuationWithNil(let continuation):
|
||||||
|
continuation.resume(returning: .success(nil))
|
||||||
|
|
||||||
|
case .resumeDownstreamContinuationWithError(let continuation, let error):
|
||||||
|
continuation.resume(returning: .failure(error))
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return try result._rethrowGet()
|
||||||
|
} onCancel: {
|
||||||
|
let action = self.stateMachine.withCriticalRegion { $0.cancelled() }
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamAndClockContinuation(
|
||||||
|
let downstreamContinuation,
|
||||||
|
let task,
|
||||||
|
let upstreamContinuation,
|
||||||
|
let clockContinuation
|
||||||
|
):
|
||||||
|
upstreamContinuation?.resume(throwing: CancellationError())
|
||||||
|
clockContinuation?.resume(throwing: CancellationError())
|
||||||
|
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
downstreamContinuation.resume(returning: .success(nil))
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func next() async rethrows -> Element? {
|
private func startTask(
|
||||||
// We need to handle cancellation here because we are creating a continuation
|
stateMachine: inout DebounceStateMachine<Base, C>,
|
||||||
// and because we need to cancel the `Task` we created to consume the upstream
|
base: Base,
|
||||||
return try await withTaskCancellationHandler {
|
downstreamContinuation: UnsafeContinuation<Result<Base.Element?, Error>, Never>
|
||||||
// We always suspend since we can never return an element right away
|
) {
|
||||||
|
let task = Task {
|
||||||
|
await withThrowingTaskGroup(of: Void.self) { group in
|
||||||
|
// The task that consumes the upstream sequence
|
||||||
|
group.addTask {
|
||||||
|
var iterator = base.makeAsyncIterator()
|
||||||
|
|
||||||
let result: Result<Element?, Error> = await withUnsafeContinuation { continuation in
|
// This is our upstream consumption loop
|
||||||
self.stateMachine.withCriticalRegion {
|
loop: while true {
|
||||||
let action = $0.next(for: continuation)
|
// We are creating a continuation before requesting the next
|
||||||
|
// element from upstream. This continuation is only resumed
|
||||||
|
// if the downstream consumer called `next` to signal his demand
|
||||||
|
// and until the Clock sleep finished.
|
||||||
|
try await withUnsafeThrowingContinuation { continuation in
|
||||||
|
let action = self.stateMachine.withCriticalRegion { $0.upstreamTaskSuspended(continuation) }
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .startTask(let base):
|
case .resumeContinuation(let continuation):
|
||||||
self.startTask(
|
// This happens if there is outstanding demand
|
||||||
stateMachine: &$0,
|
// and we need to demand from upstream right away
|
||||||
base: base,
|
continuation.resume(returning: ())
|
||||||
downstreamContinuation: continuation
|
|
||||||
)
|
|
||||||
|
|
||||||
case .resumeUpstreamContinuation(let upstreamContinuation):
|
case .resumeContinuationWithError(let continuation, let error):
|
||||||
// This is signalling the upstream task that is consuming the upstream
|
// This happens if the task got cancelled.
|
||||||
// sequence to signal demand.
|
continuation.resume(throwing: error)
|
||||||
upstreamContinuation?.resume(returning: ())
|
|
||||||
|
|
||||||
case .resumeUpstreamAndClockContinuation(let upstreamContinuation, let clockContinuation, let deadline):
|
case .none:
|
||||||
// This is signalling the upstream task that is consuming the upstream
|
break
|
||||||
// sequence to signal demand and start the clock task.
|
}
|
||||||
upstreamContinuation?.resume(returning: ())
|
|
||||||
clockContinuation?.resume(returning: deadline)
|
|
||||||
|
|
||||||
case .resumeDownstreamContinuationWithNil(let continuation):
|
|
||||||
continuation.resume(returning: .success(nil))
|
|
||||||
|
|
||||||
case .resumeDownstreamContinuationWithError(let continuation, let error):
|
|
||||||
continuation.resume(returning: .failure(error))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return try result._rethrowGet()
|
// We got signalled from the downstream that we have demand so let's
|
||||||
} onCancel: {
|
// request a new element from the upstream
|
||||||
let action = self.stateMachine.withCriticalRegion { $0.cancelled() }
|
if let element = try await iterator.next() {
|
||||||
|
let action = self.stateMachine.withCriticalRegion {
|
||||||
|
let deadline = self.clock.now.advanced(by: self.interval)
|
||||||
|
return $0.elementProduced(element, deadline: deadline)
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamAndClockContinuation(
|
case .resumeClockContinuation(let clockContinuation, let deadline):
|
||||||
|
clockContinuation?.resume(returning: deadline)
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// The upstream returned `nil` which indicates that it finished
|
||||||
|
let action = self.stateMachine.withCriticalRegion { $0.upstreamFinished() }
|
||||||
|
|
||||||
|
// All of this is mostly cleanup around the Task and the outstanding
|
||||||
|
// continuations used for signalling.
|
||||||
|
switch action {
|
||||||
|
case .cancelTaskAndClockContinuation(let task, let clockContinuation):
|
||||||
|
task.cancel()
|
||||||
|
clockContinuation?.resume(throwing: CancellationError())
|
||||||
|
|
||||||
|
break loop
|
||||||
|
case .resumeContinuationWithNilAndCancelTaskAndUpstreamAndClockContinuation(
|
||||||
let downstreamContinuation,
|
let downstreamContinuation,
|
||||||
let task,
|
let task,
|
||||||
let upstreamContinuation,
|
let upstreamContinuation,
|
||||||
let clockContinuation
|
let clockContinuation
|
||||||
):
|
):
|
||||||
upstreamContinuation?.resume(throwing: CancellationError())
|
upstreamContinuation?.resume(throwing: CancellationError())
|
||||||
clockContinuation?.resume(throwing: CancellationError())
|
clockContinuation?.resume(throwing: CancellationError())
|
||||||
|
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .success(nil))
|
downstreamContinuation.resume(returning: .success(nil))
|
||||||
|
|
||||||
case .none:
|
break loop
|
||||||
|
|
||||||
|
case .resumeContinuationWithElementAndCancelTaskAndUpstreamAndClockContinuation(
|
||||||
|
let downstreamContinuation,
|
||||||
|
let element,
|
||||||
|
let task,
|
||||||
|
let upstreamContinuation,
|
||||||
|
let clockContinuation
|
||||||
|
):
|
||||||
|
upstreamContinuation?.resume(throwing: CancellationError())
|
||||||
|
clockContinuation?.resume(throwing: CancellationError())
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
downstreamContinuation.resume(returning: .success(element))
|
||||||
|
|
||||||
|
break loop
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
|
||||||
|
break loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
group.addTask {
|
||||||
|
// This is our clock scheduling loop
|
||||||
|
loop: while true {
|
||||||
|
do {
|
||||||
|
// We are creating a continuation sleeping on the Clock.
|
||||||
|
// This continuation is only resumed if the downstream consumer called `next`.
|
||||||
|
let deadline: C.Instant = try await withUnsafeThrowingContinuation { continuation in
|
||||||
|
let action = self.stateMachine.withCriticalRegion {
|
||||||
|
$0.clockTaskSuspended(continuation)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .resumeContinuation(let continuation, let deadline):
|
||||||
|
// This happens if there is outstanding demand
|
||||||
|
// and we need to demand from upstream right away
|
||||||
|
continuation.resume(returning: deadline)
|
||||||
|
|
||||||
|
case .resumeContinuationWithError(let continuation, let error):
|
||||||
|
// This happens if the task got cancelled.
|
||||||
|
continuation.resume(throwing: error)
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try await self.clock.sleep(until: deadline, tolerance: self.tolerance)
|
||||||
|
|
||||||
|
let action = self.stateMachine.withCriticalRegion { $0.clockSleepFinished() }
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .resumeDownstreamContinuation(let downstreamContinuation, let element):
|
||||||
|
downstreamContinuation.resume(returning: .success(element))
|
||||||
|
|
||||||
|
case .none:
|
||||||
break
|
break
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// The only error that we expect is the `CancellationError`
|
||||||
|
// thrown from the Clock.sleep or from the withUnsafeContinuation.
|
||||||
|
// This happens if we are cleaning everything up. We can just drop that error and break our loop
|
||||||
|
precondition(
|
||||||
|
error is CancellationError,
|
||||||
|
"Received unexpected error \(error) in the Clock loop"
|
||||||
|
)
|
||||||
|
break loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private func startTask(
|
|
||||||
stateMachine: inout DebounceStateMachine<Base, C>,
|
|
||||||
base: Base,
|
|
||||||
downstreamContinuation: UnsafeContinuation<Result<Base.Element?, Error>, Never>
|
|
||||||
) {
|
|
||||||
let task = Task {
|
|
||||||
await withThrowingTaskGroup(of: Void.self) { group in
|
|
||||||
// The task that consumes the upstream sequence
|
|
||||||
group.addTask {
|
|
||||||
var iterator = base.makeAsyncIterator()
|
|
||||||
|
|
||||||
// This is our upstream consumption loop
|
|
||||||
loop: while true {
|
|
||||||
// We are creating a continuation before requesting the next
|
|
||||||
// element from upstream. This continuation is only resumed
|
|
||||||
// if the downstream consumer called `next` to signal his demand
|
|
||||||
// and until the Clock sleep finished.
|
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
|
||||||
let action = self.stateMachine.withCriticalRegion { $0.upstreamTaskSuspended(continuation) }
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case .resumeContinuation(let continuation):
|
|
||||||
// This happens if there is outstanding demand
|
|
||||||
// and we need to demand from upstream right away
|
|
||||||
continuation.resume(returning: ())
|
|
||||||
|
|
||||||
case .resumeContinuationWithError(let continuation, let error):
|
|
||||||
// This happens if the task got cancelled.
|
|
||||||
continuation.resume(throwing: error)
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We got signalled from the downstream that we have demand so let's
|
|
||||||
// request a new element from the upstream
|
|
||||||
if let element = try await iterator.next() {
|
|
||||||
let action = self.stateMachine.withCriticalRegion {
|
|
||||||
let deadline = self.clock.now.advanced(by: self.interval)
|
|
||||||
return $0.elementProduced(element, deadline: deadline)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case .resumeClockContinuation(let clockContinuation, let deadline):
|
|
||||||
clockContinuation?.resume(returning: deadline)
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// The upstream returned `nil` which indicates that it finished
|
|
||||||
let action = self.stateMachine.withCriticalRegion { $0.upstreamFinished() }
|
|
||||||
|
|
||||||
// All of this is mostly cleanup around the Task and the outstanding
|
|
||||||
// continuations used for signalling.
|
|
||||||
switch action {
|
|
||||||
case .cancelTaskAndClockContinuation(let task, let clockContinuation):
|
|
||||||
task.cancel()
|
|
||||||
clockContinuation?.resume(throwing: CancellationError())
|
|
||||||
|
|
||||||
break loop
|
|
||||||
case .resumeContinuationWithNilAndCancelTaskAndUpstreamAndClockContinuation(
|
|
||||||
let downstreamContinuation,
|
|
||||||
let task,
|
|
||||||
let upstreamContinuation,
|
|
||||||
let clockContinuation
|
|
||||||
):
|
|
||||||
upstreamContinuation?.resume(throwing: CancellationError())
|
|
||||||
clockContinuation?.resume(throwing: CancellationError())
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .success(nil))
|
|
||||||
|
|
||||||
break loop
|
|
||||||
|
|
||||||
case .resumeContinuationWithElementAndCancelTaskAndUpstreamAndClockContinuation(
|
|
||||||
let downstreamContinuation,
|
|
||||||
let element,
|
|
||||||
let task,
|
|
||||||
let upstreamContinuation,
|
|
||||||
let clockContinuation
|
|
||||||
):
|
|
||||||
upstreamContinuation?.resume(throwing: CancellationError())
|
|
||||||
clockContinuation?.resume(throwing: CancellationError())
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .success(element))
|
|
||||||
|
|
||||||
break loop
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
|
|
||||||
break loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
group.addTask {
|
|
||||||
// This is our clock scheduling loop
|
|
||||||
loop: while true {
|
|
||||||
do {
|
|
||||||
// We are creating a continuation sleeping on the Clock.
|
|
||||||
// This continuation is only resumed if the downstream consumer called `next`.
|
|
||||||
let deadline: C.Instant = try await withUnsafeThrowingContinuation { continuation in
|
|
||||||
let action = self.stateMachine.withCriticalRegion { $0.clockTaskSuspended(continuation) }
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case .resumeContinuation(let continuation, let deadline):
|
|
||||||
// This happens if there is outstanding demand
|
|
||||||
// and we need to demand from upstream right away
|
|
||||||
continuation.resume(returning: deadline)
|
|
||||||
|
|
||||||
case .resumeContinuationWithError(let continuation, let error):
|
|
||||||
// This happens if the task got cancelled.
|
|
||||||
continuation.resume(throwing: error)
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try await self.clock.sleep(until: deadline, tolerance: self.tolerance)
|
|
||||||
|
|
||||||
let action = self.stateMachine.withCriticalRegion { $0.clockSleepFinished() }
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case .resumeDownStreamContinuation(let downStreamContinuation, let element):
|
|
||||||
downStreamContinuation.resume(returning: .success(element))
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// The only error that we expect is the `CancellationError`
|
|
||||||
// thrown from the Clock.sleep or from the withUnsafeContinuation.
|
|
||||||
// This happens if we are cleaning everything up. We can just drop that error and break our loop
|
|
||||||
precondition(error is CancellationError, "Received unexpected error \(error) in the Clock loop")
|
|
||||||
break loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
try await group.waitForAll()
|
|
||||||
} catch {
|
|
||||||
// The upstream sequence threw an error
|
|
||||||
let action = self.stateMachine.withCriticalRegion { $0.upstreamThrew(error) }
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuation(
|
|
||||||
let downstreamContinuation,
|
|
||||||
let error,
|
|
||||||
let task,
|
|
||||||
let upstreamContinuation,
|
|
||||||
let clockContinuation
|
|
||||||
):
|
|
||||||
upstreamContinuation?.resume(throwing: CancellationError())
|
|
||||||
clockContinuation?.resume(throwing: CancellationError())
|
|
||||||
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .failure(error))
|
|
||||||
|
|
||||||
case .cancelTaskAndClockContinuation(
|
|
||||||
let task,
|
|
||||||
let clockContinuation
|
|
||||||
):
|
|
||||||
clockContinuation?.resume(throwing: CancellationError())
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
group.cancelAll()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stateMachine.taskStarted(task, downstreamContinuation: downstreamContinuation)
|
while !group.isEmpty {
|
||||||
|
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 .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuation(
|
||||||
|
let downstreamContinuation,
|
||||||
|
let error,
|
||||||
|
let task,
|
||||||
|
let upstreamContinuation,
|
||||||
|
let clockContinuation
|
||||||
|
):
|
||||||
|
upstreamContinuation?.resume(throwing: CancellationError())
|
||||||
|
clockContinuation?.resume(throwing: CancellationError())
|
||||||
|
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
downstreamContinuation.resume(returning: .failure(error))
|
||||||
|
|
||||||
|
case .cancelTaskAndClockContinuation(
|
||||||
|
let task,
|
||||||
|
let clockContinuation
|
||||||
|
):
|
||||||
|
clockContinuation?.resume(throwing: CancellationError())
|
||||||
|
task.cancel()
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
group.cancelAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stateMachine.taskStarted(task, downstreamContinuation: downstreamContinuation)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension Dictionary {
|
extension Dictionary {
|
||||||
/// Creates a new dictionary from the key-value pairs in the given asynchronous sequence.
|
/// Creates a new dictionary from the key-value pairs in the given asynchronous sequence.
|
||||||
///
|
///
|
||||||
@@ -20,14 +21,14 @@ extension Dictionary {
|
|||||||
///
|
///
|
||||||
/// - Parameter keysAndValues: An asynchronous sequence of key-value pairs to use for
|
/// - Parameter keysAndValues: An asynchronous sequence of key-value pairs to use for
|
||||||
/// the new dictionary. Every key in `keysAndValues` must be unique.
|
/// the new dictionary. Every key in `keysAndValues` must be unique.
|
||||||
/// - Returns: A new dictionary initialized with the elements of
|
|
||||||
/// `keysAndValues`.
|
|
||||||
/// - Precondition: The sequence must not have duplicate keys.
|
/// - Precondition: The sequence must not have duplicate keys.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public init<S: AsyncSequence>(uniqueKeysWithValues keysAndValues: S) async rethrows where S.Element == (Key, Value) {
|
public init<S: AsyncSequence>(uniqueKeysWithValues keysAndValues: S) async rethrows
|
||||||
|
where S.Element == (Key, Value) {
|
||||||
self.init(uniqueKeysWithValues: try await Array(keysAndValues))
|
self.init(uniqueKeysWithValues: try await Array(keysAndValues))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new dictionary from the key-value pairs in the given asynchronous sequence,
|
/// Creates a new dictionary from the key-value pairs in the given asynchronous sequence,
|
||||||
/// using a combining closure to determine the value for any duplicate keys.
|
/// using a combining closure to determine the value for any duplicate keys.
|
||||||
///
|
///
|
||||||
@@ -46,8 +47,12 @@ extension Dictionary {
|
|||||||
/// keys that are encountered. The closure returns the desired value for
|
/// keys that are encountered. The closure returns the desired value for
|
||||||
/// the final dictionary, or throws an error if building the dictionary
|
/// the final dictionary, or throws an error if building the dictionary
|
||||||
/// can't proceed.
|
/// can't proceed.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public init<S: AsyncSequence>(_ keysAndValues: S, uniquingKeysWith combine: (Value, Value) async throws -> Value) async rethrows where S.Element == (Key, Value) {
|
public init<S: AsyncSequence>(
|
||||||
|
_ keysAndValues: S,
|
||||||
|
uniquingKeysWith combine: (Value, Value) async throws -> Value
|
||||||
|
) async rethrows where S.Element == (Key, Value) {
|
||||||
self.init()
|
self.init()
|
||||||
for try await (key, value) in keysAndValues {
|
for try await (key, value) in keysAndValues {
|
||||||
if let existing = self[key] {
|
if let existing = self[key] {
|
||||||
@@ -57,7 +62,7 @@ extension Dictionary {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new dictionary whose keys are the groupings returned by the
|
/// Creates a new dictionary whose keys are the groupings returned by the
|
||||||
/// given closure and whose values are arrays of the elements that returned
|
/// given closure and whose values are arrays of the elements that returned
|
||||||
/// each key.
|
/// each key.
|
||||||
@@ -70,8 +75,10 @@ extension Dictionary {
|
|||||||
/// - values: An asynchronous sequence of values to group into a dictionary.
|
/// - values: An asynchronous sequence of values to group into a dictionary.
|
||||||
/// - keyForValue: A closure that returns a key for each element in
|
/// - keyForValue: A closure that returns a key for each element in
|
||||||
/// `values`.
|
/// `values`.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@inlinable
|
@inlinable
|
||||||
public init<S: AsyncSequence>(grouping values: S, by keyForValue: (S.Element) async throws -> Key) async rethrows where Value == [S.Element] {
|
public init<S: AsyncSequence>(grouping values: S, by keyForValue: (S.Element) async throws -> Key) async rethrows
|
||||||
|
where Value == [S.Element] {
|
||||||
self.init()
|
self.init()
|
||||||
for try await value in values {
|
for try await value in values {
|
||||||
let key = try await keyForValue(value)
|
let key = try await keyForValue(value)
|
||||||
|
|||||||
@@ -0,0 +1,468 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncSequence {
|
||||||
|
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
|
||||||
|
/// the given separator between each element.
|
||||||
|
///
|
||||||
|
/// Any value of this asynchronous sequence's element type can be used as the separator.
|
||||||
|
///
|
||||||
|
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let input = ["A", "B", "C"].async
|
||||||
|
/// let interspersed = input.interspersed(with: "-")
|
||||||
|
/// for await element in interspersed {
|
||||||
|
/// print(element)
|
||||||
|
/// }
|
||||||
|
/// // Prints "A" "-" "B" "-" "C"
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - every: Dictates after how many elements a separator should be inserted.
|
||||||
|
/// - separator: The value to insert in between each of this async sequence’s elements.
|
||||||
|
/// - Returns: The interspersed asynchronous sequence of elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
@inlinable
|
||||||
|
public func interspersed(every: Int = 1, with separator: Element) -> AsyncInterspersedSequence<Self> {
|
||||||
|
AsyncInterspersedSequence(self, every: every, separator: separator)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
|
||||||
|
/// the given separator between each element.
|
||||||
|
///
|
||||||
|
/// Any value of this asynchronous sequence's element type can be used as the separator.
|
||||||
|
///
|
||||||
|
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let input = ["A", "B", "C"].async
|
||||||
|
/// let interspersed = input.interspersed(with: { "-" })
|
||||||
|
/// for await element in interspersed {
|
||||||
|
/// print(element)
|
||||||
|
/// }
|
||||||
|
/// // Prints "A" "-" "B" "-" "C"
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - every: Dictates after how many elements a separator should be inserted.
|
||||||
|
/// - separator: A closure that produces the value to insert in between each of this async sequence’s elements.
|
||||||
|
/// - Returns: The interspersed asynchronous sequence of elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
@inlinable
|
||||||
|
public func interspersed(
|
||||||
|
every: Int = 1,
|
||||||
|
with separator: @Sendable @escaping () -> Element
|
||||||
|
) -> AsyncInterspersedSequence<Self> {
|
||||||
|
AsyncInterspersedSequence(self, every: every, separator: separator)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
|
||||||
|
/// the given separator between each element.
|
||||||
|
///
|
||||||
|
/// Any value of this asynchronous sequence's element type can be used as the separator.
|
||||||
|
///
|
||||||
|
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let input = ["A", "B", "C"].async
|
||||||
|
/// let interspersed = input.interspersed(with: { "-" })
|
||||||
|
/// for await element in interspersed {
|
||||||
|
/// print(element)
|
||||||
|
/// }
|
||||||
|
/// // Prints "A" "-" "B" "-" "C"
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - every: Dictates after how many elements a separator should be inserted.
|
||||||
|
/// - separator: A closure that produces the value to insert in between each of this async sequence’s elements.
|
||||||
|
/// - Returns: The interspersed asynchronous sequence of elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
@inlinable
|
||||||
|
public func interspersed(
|
||||||
|
every: Int = 1,
|
||||||
|
with separator: @Sendable @escaping () async -> Element
|
||||||
|
) -> AsyncInterspersedSequence<Self> {
|
||||||
|
AsyncInterspersedSequence(self, every: every, separator: separator)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
|
||||||
|
/// the given separator between each element.
|
||||||
|
///
|
||||||
|
/// Any value of this asynchronous sequence's element type can be used as the separator.
|
||||||
|
///
|
||||||
|
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let input = ["A", "B", "C"].async
|
||||||
|
/// let interspersed = input.interspersed(with: { "-" })
|
||||||
|
/// for await element in interspersed {
|
||||||
|
/// print(element)
|
||||||
|
/// }
|
||||||
|
/// // Prints "A" "-" "B" "-" "C"
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - every: Dictates after how many elements a separator should be inserted.
|
||||||
|
/// - separator: A closure that produces the value to insert in between each of this async sequence’s elements.
|
||||||
|
/// - Returns: The interspersed asynchronous sequence of elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
@inlinable
|
||||||
|
public func interspersed(
|
||||||
|
every: Int = 1,
|
||||||
|
with separator: @Sendable @escaping () throws -> Element
|
||||||
|
) -> AsyncThrowingInterspersedSequence<Self> {
|
||||||
|
AsyncThrowingInterspersedSequence(self, every: every, separator: separator)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
|
||||||
|
/// the given separator between each element.
|
||||||
|
///
|
||||||
|
/// Any value of this asynchronous sequence's element type can be used as the separator.
|
||||||
|
///
|
||||||
|
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let input = ["A", "B", "C"].async
|
||||||
|
/// let interspersed = input.interspersed(with: { "-" })
|
||||||
|
/// for await element in interspersed {
|
||||||
|
/// print(element)
|
||||||
|
/// }
|
||||||
|
/// // Prints "A" "-" "B" "-" "C"
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - every: Dictates after how many elements a separator should be inserted.
|
||||||
|
/// - separator: A closure that produces the value to insert in between each of this async sequence’s elements.
|
||||||
|
/// - Returns: The interspersed asynchronous sequence of elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
@inlinable
|
||||||
|
public func interspersed(
|
||||||
|
every: Int = 1,
|
||||||
|
with separator: @Sendable @escaping () async throws -> Element
|
||||||
|
) -> AsyncThrowingInterspersedSequence<Self> {
|
||||||
|
AsyncThrowingInterspersedSequence(self, every: every, separator: separator)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An asynchronous sequence that presents the elements of a base asynchronous sequence of
|
||||||
|
/// elements with a separator between each of those elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct AsyncInterspersedSequence<Base: AsyncSequence> {
|
||||||
|
@usableFromInline
|
||||||
|
internal enum Separator {
|
||||||
|
case element(Element)
|
||||||
|
case syncClosure(@Sendable () -> Element)
|
||||||
|
case asyncClosure(@Sendable () async -> Element)
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let base: Base
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let separator: Separator
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let every: Int
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ base: Base, every: Int, separator: Element) {
|
||||||
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
|
self.base = base
|
||||||
|
self.separator = .element(separator)
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () -> Element) {
|
||||||
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
|
self.base = base
|
||||||
|
self.separator = .syncClosure(separator)
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () async -> Element) {
|
||||||
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
|
self.base = base
|
||||||
|
self.separator = .asyncClosure(separator)
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncInterspersedSequence: AsyncSequence {
|
||||||
|
public typealias Element = Base.Element
|
||||||
|
|
||||||
|
/// The iterator for an `AsyncInterspersedSequence` asynchronous sequence.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
@usableFromInline
|
||||||
|
internal enum State {
|
||||||
|
case start(Element?)
|
||||||
|
case element(Int)
|
||||||
|
case separator
|
||||||
|
case finished
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal var iterator: Base.AsyncIterator
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let separator: Separator
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let every: Int
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal var state = State.start(nil)
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ iterator: Base.AsyncIterator, every: Int, separator: Separator) {
|
||||||
|
self.iterator = iterator
|
||||||
|
self.separator = separator
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
|
||||||
|
public mutating func next() async rethrows -> Base.Element? {
|
||||||
|
switch self.state {
|
||||||
|
case .start(var element):
|
||||||
|
do {
|
||||||
|
if element == nil {
|
||||||
|
element = try await self.iterator.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let element = element else {
|
||||||
|
self.state = .finished
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if self.every == 1 {
|
||||||
|
self.state = .separator
|
||||||
|
} else {
|
||||||
|
self.state = .element(1)
|
||||||
|
}
|
||||||
|
return element
|
||||||
|
} catch {
|
||||||
|
self.state = .finished
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
case .separator:
|
||||||
|
do {
|
||||||
|
guard let element = try await iterator.next() else {
|
||||||
|
self.state = .finished
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
self.state = .start(element)
|
||||||
|
switch self.separator {
|
||||||
|
case .element(let element):
|
||||||
|
return element
|
||||||
|
|
||||||
|
case .syncClosure(let closure):
|
||||||
|
return closure()
|
||||||
|
|
||||||
|
case .asyncClosure(let closure):
|
||||||
|
return await closure()
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
self.state = .finished
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
case .element(let count):
|
||||||
|
do {
|
||||||
|
guard let element = try await iterator.next() else {
|
||||||
|
self.state = .finished
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
let newCount = count + 1
|
||||||
|
if self.every == newCount {
|
||||||
|
self.state = .separator
|
||||||
|
} else {
|
||||||
|
self.state = .element(newCount)
|
||||||
|
}
|
||||||
|
return element
|
||||||
|
} catch {
|
||||||
|
self.state = .finished
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
@inlinable
|
||||||
|
public func makeAsyncIterator() -> Iterator {
|
||||||
|
Iterator(self.base.makeAsyncIterator(), every: self.every, separator: self.separator)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An asynchronous sequence that presents the elements of a base asynchronous sequence of
|
||||||
|
/// elements with a separator between each of those elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct AsyncThrowingInterspersedSequence<Base: AsyncSequence> {
|
||||||
|
@usableFromInline
|
||||||
|
internal enum Separator {
|
||||||
|
case syncClosure(@Sendable () throws -> Element)
|
||||||
|
case asyncClosure(@Sendable () async throws -> Element)
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let base: Base
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let separator: Separator
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let every: Int
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () throws -> Element) {
|
||||||
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
|
self.base = base
|
||||||
|
self.separator = .syncClosure(separator)
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () async throws -> Element) {
|
||||||
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
|
self.base = base
|
||||||
|
self.separator = .asyncClosure(separator)
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncThrowingInterspersedSequence: AsyncSequence {
|
||||||
|
public typealias Element = Base.Element
|
||||||
|
|
||||||
|
/// The iterator for an `AsyncInterspersedSequence` asynchronous sequence.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
|
@usableFromInline
|
||||||
|
internal enum State {
|
||||||
|
case start(Element?)
|
||||||
|
case element(Int)
|
||||||
|
case separator
|
||||||
|
case finished
|
||||||
|
}
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal var iterator: Base.AsyncIterator
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let separator: Separator
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal let every: Int
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal var state = State.start(nil)
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
internal init(_ iterator: Base.AsyncIterator, every: Int, separator: Separator) {
|
||||||
|
self.iterator = iterator
|
||||||
|
self.separator = separator
|
||||||
|
self.every = every
|
||||||
|
}
|
||||||
|
|
||||||
|
public mutating func next() async throws -> Base.Element? {
|
||||||
|
switch self.state {
|
||||||
|
case .start(var element):
|
||||||
|
do {
|
||||||
|
if element == nil {
|
||||||
|
element = try await self.iterator.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let element = element else {
|
||||||
|
self.state = .finished
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if self.every == 1 {
|
||||||
|
self.state = .separator
|
||||||
|
} else {
|
||||||
|
self.state = .element(1)
|
||||||
|
}
|
||||||
|
return element
|
||||||
|
} catch {
|
||||||
|
self.state = .finished
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
case .separator:
|
||||||
|
do {
|
||||||
|
guard let element = try await iterator.next() else {
|
||||||
|
self.state = .finished
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
self.state = .start(element)
|
||||||
|
switch self.separator {
|
||||||
|
case .syncClosure(let closure):
|
||||||
|
return try closure()
|
||||||
|
|
||||||
|
case .asyncClosure(let closure):
|
||||||
|
return try await closure()
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
self.state = .finished
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
case .element(let count):
|
||||||
|
do {
|
||||||
|
guard let element = try await iterator.next() else {
|
||||||
|
self.state = .finished
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
let newCount = count + 1
|
||||||
|
if self.every == newCount {
|
||||||
|
self.state = .separator
|
||||||
|
} else {
|
||||||
|
self.state = .element(newCount)
|
||||||
|
}
|
||||||
|
return element
|
||||||
|
} catch {
|
||||||
|
self.state = .finished
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
case .finished:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
@inlinable
|
||||||
|
public func makeAsyncIterator() -> Iterator {
|
||||||
|
Iterator(self.base.makeAsyncIterator(), every: self.every, separator: self.separator)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncInterspersedSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncInterspersedSequence.Separator: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncThrowingInterspersedSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncThrowingInterspersedSequence.Separator: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncInterspersedSequence.Iterator: Sendable {}
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncThrowingInterspersedSequence.Iterator: Sendable {}
|
||||||
@@ -10,64 +10,81 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
@_implementationOnly import Darwin
|
import Darwin
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc)
|
||||||
@_implementationOnly import Glibc
|
import Glibc
|
||||||
|
#elseif canImport(Musl)
|
||||||
|
import Musl
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
@_implementationOnly import WinSDK
|
import WinSDK
|
||||||
|
#elseif canImport(Bionic)
|
||||||
|
import Bionic
|
||||||
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
internal struct Lock {
|
internal struct Lock {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
typealias Primitive = os_unfair_lock
|
typealias Primitive = os_unfair_lock
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
typealias Primitive = pthread_mutex_t
|
typealias Primitive = pthread_mutex_t
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
typealias Primitive = SRWLOCK
|
typealias Primitive = SRWLOCK
|
||||||
#endif
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
|
#endif
|
||||||
|
|
||||||
typealias PlatformLock = UnsafeMutablePointer<Primitive>
|
typealias PlatformLock = UnsafeMutablePointer<Primitive>
|
||||||
let platformLock: PlatformLock
|
let platformLock: PlatformLock
|
||||||
|
|
||||||
private init(_ platformLock: PlatformLock) {
|
private init(_ platformLock: PlatformLock) {
|
||||||
self.platformLock = platformLock
|
self.platformLock = platformLock
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate static func initialize(_ platformLock: PlatformLock) {
|
fileprivate static func initialize(_ platformLock: PlatformLock) {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
platformLock.initialize(to: os_unfair_lock())
|
platformLock.initialize(to: os_unfair_lock())
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
pthread_mutex_init(platformLock, nil)
|
let result = pthread_mutex_init(platformLock, nil)
|
||||||
#elseif canImport(WinSDK)
|
precondition(result == 0, "pthread_mutex_init failed")
|
||||||
|
#elseif canImport(WinSDK)
|
||||||
InitializeSRWLock(platformLock)
|
InitializeSRWLock(platformLock)
|
||||||
#endif
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate static func deinitialize(_ platformLock: PlatformLock) {
|
fileprivate static func deinitialize(_ platformLock: PlatformLock) {
|
||||||
#if canImport(Glibc)
|
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
pthread_mutex_destroy(platformLock)
|
let result = pthread_mutex_destroy(platformLock)
|
||||||
#endif
|
precondition(result == 0, "pthread_mutex_destroy failed")
|
||||||
|
#endif
|
||||||
platformLock.deinitialize(count: 1)
|
platformLock.deinitialize(count: 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate static func lock(_ platformLock: PlatformLock) {
|
fileprivate static func lock(_ platformLock: PlatformLock) {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
os_unfair_lock_lock(platformLock)
|
os_unfair_lock_lock(platformLock)
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
pthread_mutex_lock(platformLock)
|
pthread_mutex_lock(platformLock)
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
AcquireSRWLockExclusive(platformLock)
|
AcquireSRWLockExclusive(platformLock)
|
||||||
#endif
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate static func unlock(_ platformLock: PlatformLock) {
|
fileprivate static func unlock(_ platformLock: PlatformLock) {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
os_unfair_lock_unlock(platformLock)
|
os_unfair_lock_unlock(platformLock)
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
pthread_mutex_unlock(platformLock)
|
let result = pthread_mutex_unlock(platformLock)
|
||||||
#elseif canImport(WinSDK)
|
precondition(result == 0, "pthread_mutex_unlock failed")
|
||||||
|
#elseif canImport(WinSDK)
|
||||||
ReleaseSRWLockExclusive(platformLock)
|
ReleaseSRWLockExclusive(platformLock)
|
||||||
#endif
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static func allocate() -> Lock {
|
static func allocate() -> Lock {
|
||||||
@@ -78,6 +95,7 @@ internal struct Lock {
|
|||||||
|
|
||||||
func deinitialize() {
|
func deinitialize() {
|
||||||
Lock.deinitialize(platformLock)
|
Lock.deinitialize(platformLock)
|
||||||
|
platformLock.deallocate()
|
||||||
}
|
}
|
||||||
|
|
||||||
func lock() {
|
func lock() {
|
||||||
@@ -88,26 +106,26 @@ internal struct Lock {
|
|||||||
Lock.unlock(platformLock)
|
Lock.unlock(platformLock)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Acquire the lock for the duration of the given block.
|
/// Acquire the lock for the duration of the given block.
|
||||||
///
|
///
|
||||||
/// This convenience method should be preferred to `lock` and `unlock` in
|
/// This convenience method should be preferred to `lock` and `unlock` in
|
||||||
/// most situations, as it ensures that the lock will be released regardless
|
/// most situations, as it ensures that the lock will be released regardless
|
||||||
/// of how `body` exits.
|
/// of how `body` exits.
|
||||||
///
|
///
|
||||||
/// - Parameter body: The block to execute while holding the lock.
|
/// - Parameter body: The block to execute while holding the lock.
|
||||||
/// - Returns: The value returned by the block.
|
/// - Returns: The value returned by the block.
|
||||||
func withLock<T>(_ body: () throws -> T) rethrows -> T {
|
func withLock<T>(_ body: () throws -> T) rethrows -> T {
|
||||||
self.lock()
|
self.lock()
|
||||||
defer {
|
defer {
|
||||||
self.unlock()
|
self.unlock()
|
||||||
}
|
|
||||||
return try body()
|
|
||||||
}
|
}
|
||||||
|
return try body()
|
||||||
|
}
|
||||||
|
|
||||||
// specialise Void return (for performance)
|
// specialise Void return (for performance)
|
||||||
func withLockVoid(_ body: () throws -> Void) rethrows -> Void {
|
func withLockVoid(_ body: () throws -> Void) rethrows {
|
||||||
try self.withLock(body)
|
try self.withLock(body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ManagedCriticalState<State> {
|
struct ManagedCriticalState<State> {
|
||||||
@@ -116,16 +134,16 @@ struct ManagedCriticalState<State> {
|
|||||||
withUnsafeMutablePointerToElements { Lock.deinitialize($0) }
|
withUnsafeMutablePointerToElements { Lock.deinitialize($0) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private let buffer: ManagedBuffer<State, Lock.Primitive>
|
private let buffer: ManagedBuffer<State, Lock.Primitive>
|
||||||
|
|
||||||
init(_ initial: State) {
|
init(_ initial: State) {
|
||||||
buffer = LockedBuffer.create(minimumCapacity: 1) { buffer in
|
buffer = LockedBuffer.create(minimumCapacity: 1) { buffer in
|
||||||
buffer.withUnsafeMutablePointerToElements { Lock.initialize($0) }
|
buffer.withUnsafeMutablePointerToElements { Lock.initialize($0) }
|
||||||
return initial
|
return initial
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func withCriticalRegion<R>(_ critical: (inout State) throws -> R) rethrows -> R {
|
func withCriticalRegion<R>(_ critical: (inout State) throws -> R) rethrows -> R {
|
||||||
try buffer.withUnsafeMutablePointers { header, lock in
|
try buffer.withUnsafeMutablePointers { header, lock in
|
||||||
Lock.lock(lock)
|
Lock.lock(lock)
|
||||||
@@ -135,4 +153,4 @@ struct ManagedCriticalState<State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ManagedCriticalState: @unchecked Sendable where State: Sendable { }
|
extension ManagedCriticalState: @unchecked Sendable where State: Sendable {}
|
||||||
@@ -9,86 +9,101 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
@_implementationOnly import DequeModule
|
import DequeModule
|
||||||
|
|
||||||
/// Creates an asynchronous sequence of elements from two underlying asynchronous sequences
|
/// Creates an asynchronous sequence of elements from two underlying asynchronous sequences
|
||||||
public func merge<Base1: AsyncSequence, Base2: AsyncSequence>(_ base1: Base1, _ base2: Base2) -> AsyncMerge2Sequence<Base1, Base2>
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
where
|
public func merge<Base1: AsyncSequence, Base2: AsyncSequence>(
|
||||||
Base1.Element == Base2.Element,
|
_ base1: Base1,
|
||||||
Base1: Sendable, Base2: Sendable,
|
_ base2: Base2
|
||||||
Base1.Element: Sendable
|
) -> AsyncMerge2Sequence<Base1, Base2>
|
||||||
|
where
|
||||||
|
Base1.Element == Base2.Element,
|
||||||
|
Base1: Sendable,
|
||||||
|
Base2: Sendable,
|
||||||
|
Base1.Element: Sendable
|
||||||
{
|
{
|
||||||
return AsyncMerge2Sequence(base1, base2)
|
return AsyncMerge2Sequence(base1, base2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An ``Swift/AsyncSequence`` that takes two upstream ``Swift/AsyncSequence``s and combines their elements.
|
/// An `AsyncSequence` that takes two upstream `AsyncSequence`s and combines their elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public struct AsyncMerge2Sequence<
|
public struct AsyncMerge2Sequence<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence
|
Base2: AsyncSequence
|
||||||
>: Sendable where
|
>: Sendable
|
||||||
Base1.Element == Base2.Element,
|
where
|
||||||
Base1: Sendable, Base2: Sendable,
|
Base1.Element == Base2.Element,
|
||||||
Base1.Element: Sendable
|
Base1: Sendable,
|
||||||
|
Base2: Sendable,
|
||||||
|
Base1.Element: Sendable
|
||||||
{
|
{
|
||||||
public typealias Element = Base1.Element
|
public typealias Element = Base1.Element
|
||||||
|
|
||||||
private let base1: Base1
|
private let base1: Base1
|
||||||
private let base2: Base2
|
private let base2: Base2
|
||||||
|
|
||||||
/// Initializes a new ``AsyncMerge2Sequence``.
|
/// Initializes a new ``AsyncMerge2Sequence``.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - base1: The first upstream ``Swift/AsyncSequence``.
|
/// - base1: The first upstream ``Swift/AsyncSequence``.
|
||||||
/// - base2: The second upstream ``Swift/AsyncSequence``.
|
/// - base2: The second upstream ``Swift/AsyncSequence``.
|
||||||
public init(
|
init(
|
||||||
_ base1: Base1,
|
_ base1: Base1,
|
||||||
_ base2: Base2
|
_ base2: Base2
|
||||||
) {
|
) {
|
||||||
self.base1 = base1
|
self.base1 = base1
|
||||||
self.base2 = base2
|
self.base2 = base2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncMerge2Sequence: AsyncSequence {
|
extension AsyncMerge2Sequence: AsyncSequence {
|
||||||
public func makeAsyncIterator() -> AsyncIterator {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
let storage = MergeStorage<Base1, Base2, Base1>(
|
public func makeAsyncIterator() -> Iterator {
|
||||||
base1: base1,
|
let storage = MergeStorage<Base1, Base2, Base1>(
|
||||||
base2: base2,
|
base1: base1,
|
||||||
base3: nil
|
base2: base2,
|
||||||
)
|
base3: nil
|
||||||
return AsyncIterator(storage: storage)
|
)
|
||||||
}
|
return Iterator(storage: storage)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncMerge2Sequence {
|
extension AsyncMerge2Sequence {
|
||||||
public struct AsyncIterator: AsyncIteratorProtocol {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
/// This class is needed to hook the deinit to observe once all references to the ``AsyncIterator`` are dropped.
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
///
|
/// This class is needed to hook the deinit to observe once all references to the ``AsyncIterator`` are dropped.
|
||||||
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
|
///
|
||||||
final class InternalClass: Sendable {
|
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
|
||||||
private let storage: MergeStorage<Base1, Base2, Base1>
|
final class InternalClass: Sendable {
|
||||||
|
private let storage: MergeStorage<Base1, Base2, Base1>
|
||||||
|
|
||||||
fileprivate init(storage: MergeStorage<Base1, Base2, Base1>) {
|
fileprivate init(storage: MergeStorage<Base1, Base2, Base1>) {
|
||||||
self.storage = storage
|
self.storage = storage
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
self.storage.iteratorDeinitialized()
|
self.storage.iteratorDeinitialized()
|
||||||
}
|
}
|
||||||
|
|
||||||
func next() async rethrows -> Element? {
|
func next() async rethrows -> Element? {
|
||||||
try await storage.next()
|
try await storage.next()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let internalClass: InternalClass
|
|
||||||
|
|
||||||
fileprivate init(storage: MergeStorage<Base1, Base2, Base1>) {
|
|
||||||
internalClass = InternalClass(storage: storage)
|
|
||||||
}
|
|
||||||
|
|
||||||
public mutating func next() async rethrows -> Element? {
|
|
||||||
try await internalClass.next()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let internalClass: InternalClass
|
||||||
|
|
||||||
|
fileprivate init(storage: MergeStorage<Base1, Base2, Base1>) {
|
||||||
|
internalClass = InternalClass(storage: storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
public mutating func next() async rethrows -> Element? {
|
||||||
|
try await internalClass.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncMerge2Sequence.Iterator: Sendable {}
|
||||||
@@ -9,97 +9,111 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
@_implementationOnly import DequeModule
|
import DequeModule
|
||||||
|
|
||||||
/// Creates an asynchronous sequence of elements from two underlying asynchronous sequences
|
/// Creates an asynchronous sequence of elements from two underlying asynchronous sequences
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public func merge<
|
public func merge<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence,
|
Base2: AsyncSequence,
|
||||||
Base3: AsyncSequence
|
Base3: AsyncSequence
|
||||||
>(_ base1: Base1, _ base2: Base2, _ base3: Base3) -> AsyncMerge3Sequence<Base1, Base2, Base3>
|
>(_ base1: Base1, _ base2: Base2, _ base3: Base3) -> AsyncMerge3Sequence<Base1, Base2, Base3>
|
||||||
where
|
where
|
||||||
Base1.Element == Base2.Element,
|
Base1.Element == Base2.Element,
|
||||||
Base1.Element == Base3.Element,
|
Base1.Element == Base3.Element,
|
||||||
Base1: Sendable, Base2: Sendable, Base3: Sendable,
|
Base1: Sendable,
|
||||||
Base1.Element: Sendable
|
Base2: Sendable,
|
||||||
|
Base3: Sendable,
|
||||||
|
Base1.Element: Sendable
|
||||||
{
|
{
|
||||||
return AsyncMerge3Sequence(base1, base2, base3)
|
return AsyncMerge3Sequence(base1, base2, base3)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An ``Swift/AsyncSequence`` that takes three upstream ``Swift/AsyncSequence``s and combines their elements.
|
/// An `AsyncSequence` that takes three upstream `AsyncSequence`s and combines their elements.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public struct AsyncMerge3Sequence<
|
public struct AsyncMerge3Sequence<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence,
|
Base2: AsyncSequence,
|
||||||
Base3: AsyncSequence
|
Base3: AsyncSequence
|
||||||
>: Sendable where
|
>: Sendable
|
||||||
Base1.Element == Base2.Element,
|
where
|
||||||
Base1.Element == Base3.Element,
|
Base1.Element == Base2.Element,
|
||||||
Base1: Sendable, Base2: Sendable, Base3: Sendable,
|
Base1.Element == Base3.Element,
|
||||||
Base1.Element: Sendable
|
Base1: Sendable,
|
||||||
|
Base2: Sendable,
|
||||||
|
Base3: Sendable,
|
||||||
|
Base1.Element: Sendable
|
||||||
{
|
{
|
||||||
public typealias Element = Base1.Element
|
public typealias Element = Base1.Element
|
||||||
|
|
||||||
private let base1: Base1
|
private let base1: Base1
|
||||||
private let base2: Base2
|
private let base2: Base2
|
||||||
private let base3: Base3
|
private let base3: Base3
|
||||||
|
|
||||||
/// Initializes a new ``AsyncMerge2Sequence``.
|
/// Initializes a new ``AsyncMerge2Sequence``.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - base1: The first upstream ``Swift/AsyncSequence``.
|
/// - base1: The first upstream ``Swift/AsyncSequence``.
|
||||||
/// - base2: The second upstream ``Swift/AsyncSequence``.
|
/// - base2: The second upstream ``Swift/AsyncSequence``.
|
||||||
/// - base3: The third upstream ``Swift/AsyncSequence``.
|
/// - base3: The third upstream ``Swift/AsyncSequence``.
|
||||||
public init(
|
init(
|
||||||
_ base1: Base1,
|
_ base1: Base1,
|
||||||
_ base2: Base2,
|
_ base2: Base2,
|
||||||
_ base3: Base3
|
_ base3: Base3
|
||||||
) {
|
) {
|
||||||
self.base1 = base1
|
self.base1 = base1
|
||||||
self.base2 = base2
|
self.base2 = base2
|
||||||
self.base3 = base3
|
self.base3 = base3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncMerge3Sequence: AsyncSequence {
|
extension AsyncMerge3Sequence: AsyncSequence {
|
||||||
public func makeAsyncIterator() -> AsyncIterator {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
let storage = MergeStorage(
|
public func makeAsyncIterator() -> Iterator {
|
||||||
base1: base1,
|
let storage = MergeStorage(
|
||||||
base2: base2,
|
base1: base1,
|
||||||
base3: base3
|
base2: base2,
|
||||||
)
|
base3: base3
|
||||||
return AsyncIterator(storage: storage)
|
)
|
||||||
}
|
return Iterator(storage: storage)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension AsyncMerge3Sequence {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
struct AsyncIterator: AsyncIteratorProtocol {
|
extension AsyncMerge3Sequence {
|
||||||
/// This class is needed to hook the deinit to observe once all references to the ``AsyncIterator`` are dropped.
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
///
|
public struct Iterator: AsyncIteratorProtocol {
|
||||||
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
|
/// This class is needed to hook the deinit to observe once all references to the ``AsyncIterator`` are dropped.
|
||||||
final class InternalClass: Sendable {
|
///
|
||||||
private let storage: MergeStorage<Base1, Base2, Base3>
|
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
|
||||||
|
final class InternalClass: Sendable {
|
||||||
|
private let storage: MergeStorage<Base1, Base2, Base3>
|
||||||
|
|
||||||
fileprivate init(storage: MergeStorage<Base1, Base2, Base3>) {
|
fileprivate init(storage: MergeStorage<Base1, Base2, Base3>) {
|
||||||
self.storage = storage
|
self.storage = storage
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
self.storage.iteratorDeinitialized()
|
self.storage.iteratorDeinitialized()
|
||||||
}
|
}
|
||||||
|
|
||||||
func next() async rethrows -> Element? {
|
func next() async rethrows -> Element? {
|
||||||
try await storage.next()
|
try await storage.next()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let internalClass: InternalClass
|
|
||||||
|
|
||||||
fileprivate init(storage: MergeStorage<Base1, Base2, Base3>) {
|
|
||||||
internalClass = InternalClass(storage: storage)
|
|
||||||
}
|
|
||||||
|
|
||||||
public mutating func next() async rethrows -> Element? {
|
|
||||||
try await internalClass.next()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let internalClass: InternalClass
|
||||||
|
|
||||||
|
fileprivate init(storage: MergeStorage<Base1, Base2, Base3>) {
|
||||||
|
internalClass = InternalClass(storage: storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
public mutating func next() async rethrows -> Element? {
|
||||||
|
try await internalClass.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncMerge3Sequence.Iterator: Sendable {}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -9,441 +9,288 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
final class MergeStorage<
|
final class MergeStorage<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence,
|
Base2: AsyncSequence,
|
||||||
Base3: AsyncSequence
|
Base3: AsyncSequence
|
||||||
>: @unchecked Sendable where
|
>: @unchecked Sendable
|
||||||
Base1.Element == Base2.Element,
|
where
|
||||||
Base1.Element == Base3.Element,
|
Base1.Element == Base2.Element,
|
||||||
Base1: Sendable, Base2: Sendable, Base3: Sendable,
|
Base1.Element == Base3.Element,
|
||||||
Base1.Element: Sendable
|
Base1: Sendable,
|
||||||
|
Base2: Sendable,
|
||||||
|
Base3: Sendable,
|
||||||
|
Base1.Element: Sendable
|
||||||
{
|
{
|
||||||
typealias Element = Base1.Element
|
typealias Element = Base1.Element
|
||||||
|
|
||||||
/// The lock that protects our state.
|
/// The lock that protects our state.
|
||||||
private let lock = Lock.allocate()
|
private let lock = Lock.allocate()
|
||||||
/// The state machine.
|
/// The state machine.
|
||||||
private var stateMachine: MergeStateMachine<Base1, Base2, Base3>
|
private var stateMachine: MergeStateMachine<Base1, Base2, Base3>
|
||||||
|
|
||||||
init(
|
init(
|
||||||
base1: Base1,
|
base1: Base1,
|
||||||
base2: Base2,
|
base2: Base2,
|
||||||
base3: Base3?
|
base3: Base3?
|
||||||
) {
|
) {
|
||||||
stateMachine = .init(base1: base1, base2: base2, base3: base3)
|
stateMachine = .init(base1: base1, base2: base2, base3: base3)
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
self.lock.deinitialize()
|
||||||
|
}
|
||||||
|
|
||||||
|
func iteratorDeinitialized() {
|
||||||
|
let action = lock.withLock { self.stateMachine.iteratorDeinitialized() }
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case let .cancelTaskAndUpstreamContinuations(
|
||||||
|
task,
|
||||||
|
upstreamContinuations
|
||||||
|
):
|
||||||
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
|
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func next() async rethrows -> Element? {
|
||||||
|
// We need to handle cancellation here because we are creating a continuation
|
||||||
|
// and because we need to cancel the `Task` we created to consume the upstream
|
||||||
|
try await withTaskCancellationHandler {
|
||||||
|
self.lock.lock()
|
||||||
|
let action = self.stateMachine.next()
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case .startTaskAndSuspendDownstreamTask(let base1, let base2, let base3):
|
||||||
|
self.startTask(
|
||||||
|
stateMachine: &self.stateMachine,
|
||||||
|
base1: base1,
|
||||||
|
base2: base2,
|
||||||
|
base3: base3
|
||||||
|
)
|
||||||
|
// It is safe to hold the lock across this method
|
||||||
|
// since the closure is guaranteed to be run straight away
|
||||||
|
return try await withUnsafeThrowingContinuation { continuation in
|
||||||
|
let action = self.stateMachine.next(for: continuation)
|
||||||
|
self.lock.unlock()
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case let .resumeUpstreamContinuations(upstreamContinuations):
|
||||||
|
// This is signalling the child tasks that are consuming the upstream
|
||||||
|
// sequences to signal demand.
|
||||||
|
upstreamContinuations.forEach { $0.resume(returning: ()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case let .returnElement(element):
|
||||||
|
self.lock.unlock()
|
||||||
|
|
||||||
|
return try element._rethrowGet()
|
||||||
|
|
||||||
|
case .returnNil:
|
||||||
|
self.lock.unlock()
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case let .throwError(error):
|
||||||
|
self.lock.unlock()
|
||||||
|
throw error
|
||||||
|
|
||||||
|
case .suspendDownstreamTask:
|
||||||
|
// It is safe to hold the lock across this method
|
||||||
|
// since the closure is guaranteed to be run straight away
|
||||||
|
return try await withUnsafeThrowingContinuation { continuation in
|
||||||
|
let action = self.stateMachine.next(for: continuation)
|
||||||
|
self.lock.unlock()
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case let .resumeUpstreamContinuations(upstreamContinuations):
|
||||||
|
// This is signalling the child tasks that are consuming the upstream
|
||||||
|
// sequences to signal demand.
|
||||||
|
upstreamContinuations.forEach { $0.resume(returning: ()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} onCancel: {
|
||||||
|
let action = self.lock.withLock { self.stateMachine.cancelled() }
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case let .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
|
downstreamContinuation,
|
||||||
|
task,
|
||||||
|
upstreamContinuations
|
||||||
|
):
|
||||||
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
|
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
downstreamContinuation.resume(returning: nil)
|
||||||
|
|
||||||
|
case let .cancelTaskAndUpstreamContinuations(
|
||||||
|
task,
|
||||||
|
upstreamContinuations
|
||||||
|
):
|
||||||
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
|
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func startTask(
|
||||||
|
stateMachine: inout MergeStateMachine<Base1, Base2, Base3>,
|
||||||
|
base1: Base1,
|
||||||
|
base2: Base2,
|
||||||
|
base3: Base3?
|
||||||
|
) {
|
||||||
|
// This creates a new `Task` that is iterating the upstream
|
||||||
|
// sequences. We must store it to cancel it at the right times.
|
||||||
|
let task = Task {
|
||||||
|
await withThrowingTaskGroup(of: Void.self) { group in
|
||||||
|
self.iterateAsyncSequence(base1, in: &group)
|
||||||
|
self.iterateAsyncSequence(base2, in: &group)
|
||||||
|
|
||||||
|
// Copy from the above just using the base3 sequence
|
||||||
|
if let base3 = base3 {
|
||||||
|
self.iterateAsyncSequence(base3, in: &group)
|
||||||
|
}
|
||||||
|
|
||||||
|
while !group.isEmpty {
|
||||||
|
do {
|
||||||
|
try await group.next()
|
||||||
|
} catch {
|
||||||
|
// One of the upstream sequences threw an error
|
||||||
|
let action = self.lock.withLock {
|
||||||
|
self.stateMachine.upstreamThrew(error)
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case let .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
|
||||||
|
downstreamContinuation,
|
||||||
|
error,
|
||||||
|
task,
|
||||||
|
upstreamContinuations
|
||||||
|
):
|
||||||
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
|
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
downstreamContinuation.resume(throwing: error)
|
||||||
|
case let .cancelTaskAndUpstreamContinuations(
|
||||||
|
task,
|
||||||
|
upstreamContinuations
|
||||||
|
):
|
||||||
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
|
|
||||||
|
task.cancel()
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
group.cancelAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
// We need to inform our state machine that we started the Task
|
||||||
self.lock.deinitialize()
|
stateMachine.taskStarted(task)
|
||||||
}
|
}
|
||||||
|
|
||||||
func iteratorDeinitialized() {
|
private func iterateAsyncSequence<AsyncSequence: _Concurrency.AsyncSequence>(
|
||||||
let action = lock.withLock { self.stateMachine.iteratorDeinitialized() }
|
_ base: AsyncSequence,
|
||||||
|
in taskGroup: inout ThrowingTaskGroup<Void, Error>
|
||||||
|
) where AsyncSequence.Element == Base1.Element, AsyncSequence: Sendable {
|
||||||
|
// For each upstream sequence we are adding a child task that
|
||||||
|
// is consuming the upstream sequence
|
||||||
|
taskGroup.addTask {
|
||||||
|
var iterator = base.makeAsyncIterator()
|
||||||
|
|
||||||
switch action {
|
// This is our upstream consumption loop
|
||||||
case let .cancelTaskAndUpstreamContinuations(
|
loop: while true {
|
||||||
|
// We are creating a continuation before requesting the next
|
||||||
|
// element from upstream. This continuation is only resumed
|
||||||
|
// if the downstream consumer called `next` to signal his demand.
|
||||||
|
try await withUnsafeThrowingContinuation { continuation in
|
||||||
|
let action = self.lock.withLock {
|
||||||
|
self.stateMachine.childTaskSuspended(continuation)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case let .resumeContinuation(continuation):
|
||||||
|
// This happens if there is outstanding demand
|
||||||
|
// and we need to demand from upstream right away
|
||||||
|
continuation.resume(returning: ())
|
||||||
|
|
||||||
|
case let .resumeContinuationWithError(continuation, error):
|
||||||
|
// This happens if another upstream already failed or if
|
||||||
|
// the task got cancelled.
|
||||||
|
continuation.resume(throwing: error)
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We got signalled from the downstream that we have demand so let's
|
||||||
|
// request a new element from the upstream
|
||||||
|
if let element1 = try await iterator.next() {
|
||||||
|
let action = self.lock.withLock {
|
||||||
|
self.stateMachine.elementProduced(element1)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case let .resumeContinuation(continuation, element):
|
||||||
|
// We had an outstanding demand and where the first
|
||||||
|
// upstream to produce an element so we can forward it to
|
||||||
|
// the downstream
|
||||||
|
continuation.resume(returning: element)
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// The upstream returned `nil` which indicates that it finished
|
||||||
|
let action = self.lock.withLock {
|
||||||
|
self.stateMachine.upstreamFinished()
|
||||||
|
}
|
||||||
|
|
||||||
|
// All of this is mostly cleanup around the Task and the outstanding
|
||||||
|
// continuations used for signalling.
|
||||||
|
switch action {
|
||||||
|
case let .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
|
downstreamContinuation,
|
||||||
task,
|
task,
|
||||||
upstreamContinuations
|
upstreamContinuations
|
||||||
):
|
):
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
|
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
case .none:
|
downstreamContinuation.resume(returning: nil)
|
||||||
break
|
|
||||||
|
break loop
|
||||||
|
|
||||||
|
case let .cancelTaskAndUpstreamContinuations(
|
||||||
|
task,
|
||||||
|
upstreamContinuations
|
||||||
|
):
|
||||||
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
break loop
|
||||||
|
case .none:
|
||||||
|
|
||||||
|
break loop
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
func next() async rethrows -> Element? {
|
|
||||||
// We need to handle cancellation here because we are creating a continuation
|
|
||||||
// and because we need to cancel the `Task` we created to consume the upstream
|
|
||||||
try await withTaskCancellationHandler {
|
|
||||||
self.lock.lock()
|
|
||||||
let action = self.stateMachine.next()
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case .startTaskAndSuspendDownstreamTask(let base1, let base2, let base3):
|
|
||||||
self.startTask(
|
|
||||||
stateMachine: &self.stateMachine,
|
|
||||||
base1: base1,
|
|
||||||
base2: base2,
|
|
||||||
base3: base3
|
|
||||||
)
|
|
||||||
// It is safe to hold the lock across this method
|
|
||||||
// since the closure is guaranteed to be run straight away
|
|
||||||
return try await withUnsafeThrowingContinuation { continuation in
|
|
||||||
let action = self.stateMachine.next(for: continuation)
|
|
||||||
self.lock.unlock()
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case let .resumeUpstreamContinuations(upstreamContinuations):
|
|
||||||
// This is signalling the child tasks that are consuming the upstream
|
|
||||||
// sequences to signal demand.
|
|
||||||
upstreamContinuations.forEach { $0.resume(returning: ()) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
case let .returnElement(element):
|
|
||||||
self.lock.unlock()
|
|
||||||
|
|
||||||
return try element._rethrowGet()
|
|
||||||
|
|
||||||
case .returnNil:
|
|
||||||
self.lock.unlock()
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case let .throwError(error):
|
|
||||||
self.lock.unlock()
|
|
||||||
throw error
|
|
||||||
|
|
||||||
case .suspendDownstreamTask:
|
|
||||||
// It is safe to hold the lock across this method
|
|
||||||
// since the closure is guaranteed to be run straight away
|
|
||||||
return try await withUnsafeThrowingContinuation { continuation in
|
|
||||||
let action = self.stateMachine.next(for: continuation)
|
|
||||||
self.lock.unlock()
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case let .resumeUpstreamContinuations(upstreamContinuations):
|
|
||||||
// This is signalling the child tasks that are consuming the upstream
|
|
||||||
// sequences to signal demand.
|
|
||||||
upstreamContinuations.forEach { $0.resume(returning: ()) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} onCancel: {
|
|
||||||
let action = self.lock.withLock { self.stateMachine.cancelled() }
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case let .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
|
||||||
downstreamContinuation,
|
|
||||||
task,
|
|
||||||
upstreamContinuations
|
|
||||||
):
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
|
||||||
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: nil)
|
|
||||||
|
|
||||||
case let .cancelTaskAndUpstreamContinuations(
|
|
||||||
task,
|
|
||||||
upstreamContinuations
|
|
||||||
):
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
|
||||||
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func startTask(stateMachine: inout MergeStateMachine<Base1, Base2, Base3>, base1: Base1, base2: Base2, base3: Base3?) {
|
|
||||||
// This creates a new `Task` that is iterating the upstream
|
|
||||||
// sequences. We must store it to cancel it at the right times.
|
|
||||||
let task = Task {
|
|
||||||
await withThrowingTaskGroup(of: Void.self) { group in
|
|
||||||
// For each upstream sequence we are adding a child task that
|
|
||||||
// is consuming the upstream sequence
|
|
||||||
group.addTask {
|
|
||||||
var iterator1 = base1.makeAsyncIterator()
|
|
||||||
|
|
||||||
// This is our upstream consumption loop
|
|
||||||
loop: while true {
|
|
||||||
// We are creating a continuation before requesting the next
|
|
||||||
// element from upstream. This continuation is only resumed
|
|
||||||
// if the downstream consumer called `next` to signal his demand.
|
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
|
||||||
let action = self.lock.withLock {
|
|
||||||
self.stateMachine.childTaskSuspended(continuation)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case let .resumeContinuation(continuation):
|
|
||||||
// This happens if there is outstanding demand
|
|
||||||
// and we need to demand from upstream right away
|
|
||||||
continuation.resume(returning: ())
|
|
||||||
|
|
||||||
case let .resumeContinuationWithError(continuation, error):
|
|
||||||
// This happens if another upstream already failed or if
|
|
||||||
// the task got cancelled.
|
|
||||||
continuation.resume(throwing: error)
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We got signalled from the downstream that we have demand so let's
|
|
||||||
// request a new element from the upstream
|
|
||||||
if let element1 = try await iterator1.next() {
|
|
||||||
let action = self.lock.withLock {
|
|
||||||
self.stateMachine.elementProduced(element1)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case let .resumeContinuation(continuation, element):
|
|
||||||
// We had an outstanding demand and where the first
|
|
||||||
// upstream to produce an element so we can forward it to
|
|
||||||
// the downstream
|
|
||||||
continuation.resume(returning: element)
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// The upstream returned `nil` which indicates that it finished
|
|
||||||
let action = self.lock.withLock {
|
|
||||||
self.stateMachine.upstreamFinished()
|
|
||||||
}
|
|
||||||
|
|
||||||
// All of this is mostly cleanup around the Task and the outstanding
|
|
||||||
// continuations used for signalling.
|
|
||||||
switch action {
|
|
||||||
case let .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
|
||||||
downstreamContinuation,
|
|
||||||
task,
|
|
||||||
upstreamContinuations
|
|
||||||
):
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: nil)
|
|
||||||
|
|
||||||
break loop
|
|
||||||
|
|
||||||
case let .cancelTaskAndUpstreamContinuations(
|
|
||||||
task,
|
|
||||||
upstreamContinuations
|
|
||||||
):
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
break loop
|
|
||||||
case .none:
|
|
||||||
|
|
||||||
break loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy from the above just using the base2 sequence
|
|
||||||
group.addTask {
|
|
||||||
var iterator2 = base2.makeAsyncIterator()
|
|
||||||
|
|
||||||
// This is our upstream consumption loop
|
|
||||||
loop: while true {
|
|
||||||
// We are creating a continuation before requesting the next
|
|
||||||
// element from upstream. This continuation is only resumed
|
|
||||||
// if the downstream consumer called `next` to signal his demand.
|
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
|
||||||
let action = self.lock.withLock {
|
|
||||||
self.stateMachine.childTaskSuspended(continuation)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case let .resumeContinuation(continuation):
|
|
||||||
// This happens if there is outstanding demand
|
|
||||||
// and we need to demand from upstream right away
|
|
||||||
continuation.resume(returning: ())
|
|
||||||
|
|
||||||
case let .resumeContinuationWithError(continuation, error):
|
|
||||||
// This happens if another upstream already failed or if
|
|
||||||
// the task got cancelled.
|
|
||||||
continuation.resume(throwing: error)
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We got signalled from the downstream that we have demand so let's
|
|
||||||
// request a new element from the upstream
|
|
||||||
if let element2 = try await iterator2.next() {
|
|
||||||
let action = self.lock.withLock {
|
|
||||||
self.stateMachine.elementProduced(element2)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case let .resumeContinuation(continuation, element):
|
|
||||||
// We had an outstanding demand and where the first
|
|
||||||
// upstream to produce an element so we can forward it to
|
|
||||||
// the downstream
|
|
||||||
continuation.resume(returning: element)
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// The upstream returned `nil` which indicates that it finished
|
|
||||||
let action = self.lock.withLock {
|
|
||||||
self.stateMachine.upstreamFinished()
|
|
||||||
}
|
|
||||||
|
|
||||||
// All of this is mostly cleanup around the Task and the outstanding
|
|
||||||
// continuations used for signalling.
|
|
||||||
switch action {
|
|
||||||
case let .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
|
||||||
downstreamContinuation,
|
|
||||||
task,
|
|
||||||
upstreamContinuations
|
|
||||||
):
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: nil)
|
|
||||||
|
|
||||||
break loop
|
|
||||||
|
|
||||||
case let .cancelTaskAndUpstreamContinuations(
|
|
||||||
task,
|
|
||||||
upstreamContinuations
|
|
||||||
):
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
break loop
|
|
||||||
case .none:
|
|
||||||
|
|
||||||
break loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy from the above just using the base3 sequence
|
|
||||||
if let base3 = base3 {
|
|
||||||
group.addTask {
|
|
||||||
var iterator3 = base3.makeAsyncIterator()
|
|
||||||
|
|
||||||
// This is our upstream consumption loop
|
|
||||||
loop: while true {
|
|
||||||
// We are creating a continuation before requesting the next
|
|
||||||
// element from upstream. This continuation is only resumed
|
|
||||||
// if the downstream consumer called `next` to signal his demand.
|
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
|
||||||
let action = self.lock.withLock {
|
|
||||||
self.stateMachine.childTaskSuspended(continuation)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case let .resumeContinuation(continuation):
|
|
||||||
// This happens if there is outstanding demand
|
|
||||||
// and we need to demand from upstream right away
|
|
||||||
continuation.resume(returning: ())
|
|
||||||
|
|
||||||
case let .resumeContinuationWithError(continuation, error):
|
|
||||||
// This happens if another upstream already failed or if
|
|
||||||
// the task got cancelled.
|
|
||||||
continuation.resume(throwing: error)
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We got signalled from the downstream that we have demand so let's
|
|
||||||
// request a new element from the upstream
|
|
||||||
if let element3 = try await iterator3.next() {
|
|
||||||
let action = self.lock.withLock {
|
|
||||||
self.stateMachine.elementProduced(element3)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case let .resumeContinuation(continuation, element):
|
|
||||||
// We had an outstanding demand and where the first
|
|
||||||
// upstream to produce an element so we can forward it to
|
|
||||||
// the downstream
|
|
||||||
continuation.resume(returning: element)
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// The upstream returned `nil` which indicates that it finished
|
|
||||||
let action = self.lock.withLock {
|
|
||||||
self.stateMachine.upstreamFinished()
|
|
||||||
}
|
|
||||||
|
|
||||||
// All of this is mostly cleanup around the Task and the outstanding
|
|
||||||
// continuations used for signalling.
|
|
||||||
switch action {
|
|
||||||
case let .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
|
||||||
downstreamContinuation,
|
|
||||||
task,
|
|
||||||
upstreamContinuations
|
|
||||||
):
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: nil)
|
|
||||||
|
|
||||||
break loop
|
|
||||||
|
|
||||||
case let .cancelTaskAndUpstreamContinuations(
|
|
||||||
task,
|
|
||||||
upstreamContinuations
|
|
||||||
):
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
break loop
|
|
||||||
case .none:
|
|
||||||
|
|
||||||
break loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
try await group.waitForAll()
|
|
||||||
} catch {
|
|
||||||
// One of the upstream sequences threw an error
|
|
||||||
let action = self.lock.withLock {
|
|
||||||
self.stateMachine.upstreamThrew(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case let .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
|
|
||||||
downstreamContinuation,
|
|
||||||
error,
|
|
||||||
task,
|
|
||||||
upstreamContinuations
|
|
||||||
):
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
|
||||||
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
downstreamContinuation.resume(throwing: error)
|
|
||||||
case let .cancelTaskAndUpstreamContinuations(
|
|
||||||
task,
|
|
||||||
upstreamContinuations
|
|
||||||
):
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
|
||||||
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
case .none:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
group.cancelAll()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We need to inform our state machine that we started the Task
|
|
||||||
stateMachine.taskStarted(task)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,47 +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
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
enum PartialIteration<Iterator: AsyncIteratorProtocol, Partial: Sendable>: CustomStringConvertible {
|
|
||||||
case idle(Iterator)
|
|
||||||
case pending(Task<Partial, Never>)
|
|
||||||
case terminal
|
|
||||||
|
|
||||||
var description: String {
|
|
||||||
switch self {
|
|
||||||
case .idle: return "idle"
|
|
||||||
case .pending: return "pending"
|
|
||||||
case .terminal: return "terminal"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mutating func resolve(_ result: Result<Iterator.Element?, Error>, _ iterator: Iterator) rethrows -> Iterator.Element? {
|
|
||||||
do {
|
|
||||||
guard let value = try result._rethrowGet() else {
|
|
||||||
self = .terminal
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
self = .idle(iterator)
|
|
||||||
return value
|
|
||||||
} catch {
|
|
||||||
self = .terminal
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mutating func cancel() {
|
|
||||||
if case .pending(let task) = self {
|
|
||||||
task.cancel()
|
|
||||||
}
|
|
||||||
self = .terminal
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension PartialIteration: Sendable where Iterator: Sendable, Iterator.Element: Sendable { }
|
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension RangeReplaceableCollection {
|
extension RangeReplaceableCollection {
|
||||||
/// Creates a new instance of a collection containing the elements of an asynchronous sequence.
|
/// Creates a new instance of a collection containing the elements of an asynchronous sequence.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -25,11 +25,10 @@ extension _ErrorMechanism {
|
|||||||
_ = try _rethrowGet()
|
_ = try _rethrowGet()
|
||||||
fatalError("materialized error without being in a throwing context")
|
fatalError("materialized error without being in a throwing context")
|
||||||
}
|
}
|
||||||
|
|
||||||
internal func _rethrowGet() rethrows -> Output {
|
internal func _rethrowGet() rethrows -> Output {
|
||||||
return try get()
|
return try get()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Result: _ErrorMechanism { }
|
extension Result: _ErrorMechanism {}
|
||||||
|
|
||||||
@@ -9,11 +9,12 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension SetAlgebra {
|
extension SetAlgebra {
|
||||||
/// Creates a new set from an asynchronous sequence of items.
|
/// Creates a new set from an asynchronous sequence of items.
|
||||||
///
|
///
|
||||||
/// Use this initializer to create a new set from an asynchronous sequence
|
/// Use this initializer to create a new set from an asynchronous sequence
|
||||||
///
|
///
|
||||||
/// - Parameter source: The elements to use as members of the new set.
|
/// - Parameter source: The elements to use as members of the new set.
|
||||||
@inlinable
|
@inlinable
|
||||||
public init<Source: AsyncSequence>(_ source: Source) async rethrows where Source.Element == Element {
|
public init<Source: AsyncSequence>(_ source: Source) async rethrows where Source.Element == Element {
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This source file is part of the Swift Async Algorithms open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
/// A wrapper struct to unconditionally to transfer an non-Sendable value.
|
||||||
|
struct UnsafeTransfer<Element>: @unchecked Sendable {
|
||||||
|
let wrapped: Element
|
||||||
|
|
||||||
|
init(_ wrapped: Element) {
|
||||||
|
self.wrapped = wrapped
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
/// Creates an asynchronous sequence that concurrently awaits values from two `AsyncSequence` types
|
/// Creates an asynchronous sequence that concurrently awaits values from two `AsyncSequence` types
|
||||||
/// and emits a tuple of the values.
|
/// and emits a tuple of the values.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public func zip<Base1: AsyncSequence, Base2: AsyncSequence>(
|
public func zip<Base1: AsyncSequence, Base2: AsyncSequence>(
|
||||||
_ base1: Base1,
|
_ base1: Base1,
|
||||||
_ base2: Base2
|
_ base2: Base2
|
||||||
@@ -20,8 +21,9 @@ public func zip<Base1: AsyncSequence, Base2: AsyncSequence>(
|
|||||||
|
|
||||||
/// An asynchronous sequence that concurrently awaits values from two `AsyncSequence` types
|
/// An asynchronous sequence that concurrently awaits values from two `AsyncSequence` types
|
||||||
/// and emits a tuple of the values.
|
/// and emits a tuple of the values.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public struct AsyncZip2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: AsyncSequence, Sendable
|
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 Element = (Base1.Element, Base2.Element)
|
||||||
public typealias AsyncIterator = Iterator
|
public typealias AsyncIterator = Iterator
|
||||||
|
|
||||||
@@ -69,3 +71,6 @@ public struct AsyncZip2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Asy
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncZip2Sequence.Iterator: Sendable {}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
/// Creates an asynchronous sequence that concurrently awaits values from three `AsyncSequence` types
|
/// Creates an asynchronous sequence that concurrently awaits values from three `AsyncSequence` types
|
||||||
/// and emits a tuple of the values.
|
/// and emits a tuple of the values.
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public func zip<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>(
|
public func zip<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>(
|
||||||
_ base1: Base1,
|
_ base1: Base1,
|
||||||
_ base2: Base2,
|
_ base2: Base2,
|
||||||
@@ -21,8 +22,17 @@ public func zip<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence
|
|||||||
|
|
||||||
/// An asynchronous sequence that concurrently awaits values from three `AsyncSequence` types
|
/// An asynchronous sequence that concurrently awaits values from three `AsyncSequence` types
|
||||||
/// and emits a tuple of the values.
|
/// and emits a tuple of the values.
|
||||||
public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: AsyncSequence, Sendable
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
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 Element = (Base1.Element, Base2.Element, Base3.Element)
|
||||||
public typealias AsyncIterator = Iterator
|
public typealias AsyncIterator = Iterator
|
||||||
|
|
||||||
@@ -37,7 +47,8 @@ public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func makeAsyncIterator() -> AsyncIterator {
|
public func makeAsyncIterator() -> AsyncIterator {
|
||||||
Iterator(storage: .init(self.base1, self.base2, self.base3)
|
Iterator(
|
||||||
|
storage: .init(self.base1, self.base2, self.base3)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,3 +85,6 @@ public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
|
extension AsyncZip3Sequence.Iterator: Sendable {}
|
||||||
@@ -10,22 +10,29 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
/// State machine for zip
|
/// State machine for zip
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
struct ZipStateMachine<
|
struct ZipStateMachine<
|
||||||
Base1: AsyncSequence,
|
Base1: AsyncSequence,
|
||||||
Base2: AsyncSequence,
|
Base2: AsyncSequence,
|
||||||
Base3: AsyncSequence
|
Base3: AsyncSequence
|
||||||
>: Sendable where
|
>: Sendable
|
||||||
|
where
|
||||||
Base1: Sendable,
|
Base1: Sendable,
|
||||||
Base2: Sendable,
|
Base2: Sendable,
|
||||||
Base3: Sendable,
|
Base3: Sendable,
|
||||||
Base1.Element: Sendable,
|
Base1.Element: Sendable,
|
||||||
Base2.Element: Sendable,
|
Base2.Element: Sendable,
|
||||||
Base3.Element: Sendable {
|
Base3.Element: Sendable
|
||||||
typealias DownstreamContinuation = UnsafeContinuation<Result<(
|
{
|
||||||
Base1.Element,
|
typealias DownstreamContinuation = UnsafeContinuation<
|
||||||
Base2.Element,
|
Result<
|
||||||
Base3.Element?
|
(
|
||||||
)?, Error>, Never>
|
Base1.Element,
|
||||||
|
Base2.Element,
|
||||||
|
Base3.Element?
|
||||||
|
)?, Error
|
||||||
|
>, Never
|
||||||
|
>
|
||||||
|
|
||||||
private enum State: Sendable {
|
private enum State: Sendable {
|
||||||
/// Small wrapper for the state of an upstream sequence.
|
/// Small wrapper for the state of an upstream sequence.
|
||||||
@@ -101,7 +108,9 @@ struct ZipStateMachine<
|
|||||||
|
|
||||||
case .zipping:
|
case .zipping:
|
||||||
// An iterator was deinitialized while we have a suspended continuation.
|
// 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):
|
case .waitingForDemand(let task, let upstreams):
|
||||||
// The iterator was dropped which signals that the consumer is finished.
|
// The iterator was dropped which signals that the consumer is finished.
|
||||||
@@ -110,7 +119,8 @@ struct ZipStateMachine<
|
|||||||
|
|
||||||
return .cancelTaskAndUpstreamContinuations(
|
return .cancelTaskAndUpstreamContinuations(
|
||||||
task: task,
|
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:
|
case .finished:
|
||||||
@@ -159,7 +169,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 {
|
switch self.state {
|
||||||
case .initial:
|
case .initial:
|
||||||
// Child tasks are only created after we transitioned to `zipping`
|
// Child tasks are only created after we transitioned to `zipping`
|
||||||
@@ -179,7 +192,9 @@ struct ZipStateMachine<
|
|||||||
upstreams.2.continuation = continuation
|
upstreams.2.continuation = continuation
|
||||||
|
|
||||||
default:
|
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(
|
self.state = .waitingForDemand(
|
||||||
@@ -194,9 +209,7 @@ struct ZipStateMachine<
|
|||||||
// already then we store the continuation otherwise we just go ahead and resume it
|
// already then we store the continuation otherwise we just go ahead and resume it
|
||||||
switch baseIndex {
|
switch baseIndex {
|
||||||
case 0:
|
case 0:
|
||||||
if upstreams.0.element == nil {
|
guard upstreams.0.element == nil else {
|
||||||
return .resumeContinuation(upstreamContinuation: continuation)
|
|
||||||
} else {
|
|
||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
upstreams.0.continuation = continuation
|
upstreams.0.continuation = continuation
|
||||||
self.state = .zipping(
|
self.state = .zipping(
|
||||||
@@ -206,11 +219,10 @@ struct ZipStateMachine<
|
|||||||
)
|
)
|
||||||
return .none
|
return .none
|
||||||
}
|
}
|
||||||
|
return .resumeContinuation(upstreamContinuation: continuation)
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
if upstreams.1.element == nil {
|
guard upstreams.1.element == nil else {
|
||||||
return .resumeContinuation(upstreamContinuation: continuation)
|
|
||||||
} else {
|
|
||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
upstreams.1.continuation = continuation
|
upstreams.1.continuation = continuation
|
||||||
self.state = .zipping(
|
self.state = .zipping(
|
||||||
@@ -220,11 +232,10 @@ struct ZipStateMachine<
|
|||||||
)
|
)
|
||||||
return .none
|
return .none
|
||||||
}
|
}
|
||||||
|
return .resumeContinuation(upstreamContinuation: continuation)
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
if upstreams.2.element == nil {
|
guard upstreams.2.element == nil else {
|
||||||
return .resumeContinuation(upstreamContinuation: continuation)
|
|
||||||
} else {
|
|
||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
upstreams.2.continuation = continuation
|
upstreams.2.continuation = continuation
|
||||||
self.state = .zipping(
|
self.state = .zipping(
|
||||||
@@ -234,9 +245,12 @@ struct ZipStateMachine<
|
|||||||
)
|
)
|
||||||
return .none
|
return .none
|
||||||
}
|
}
|
||||||
|
return .resumeContinuation(upstreamContinuation: continuation)
|
||||||
|
|
||||||
default:
|
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:
|
case .finished:
|
||||||
@@ -295,8 +309,9 @@ struct ZipStateMachine<
|
|||||||
|
|
||||||
// Implementing this for the two arities without variadic generics is a bit awkward sadly.
|
// Implementing this for the two arities without variadic generics is a bit awkward sadly.
|
||||||
if let first = upstreams.0.element,
|
if let first = upstreams.0.element,
|
||||||
let second = upstreams.1.element,
|
let second = upstreams.1.element,
|
||||||
let third = upstreams.2.element {
|
let third = upstreams.2.element
|
||||||
|
{
|
||||||
// We got an element from each upstream so we can resume the downstream now
|
// We got an element from each upstream so we can resume the downstream now
|
||||||
self.state = .waitingForDemand(
|
self.state = .waitingForDemand(
|
||||||
task: task,
|
task: task,
|
||||||
@@ -313,8 +328,9 @@ struct ZipStateMachine<
|
|||||||
)
|
)
|
||||||
|
|
||||||
} else if let first = upstreams.0.element,
|
} else if let first = upstreams.0.element,
|
||||||
let second = upstreams.1.element,
|
let second = upstreams.1.element,
|
||||||
self.numberOfUpstreamSequences == 2 {
|
self.numberOfUpstreamSequences == 2
|
||||||
|
{
|
||||||
// We got an element from each upstream so we can resume the downstream now
|
// We got an element from each upstream so we can resume the downstream now
|
||||||
self.state = .waitingForDemand(
|
self.state = .waitingForDemand(
|
||||||
task: task,
|
task: task,
|
||||||
@@ -385,7 +401,8 @@ struct ZipStateMachine<
|
|||||||
return .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
return .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
downstreamContinuation: downstreamContinuation,
|
downstreamContinuation: downstreamContinuation,
|
||||||
task: task,
|
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:
|
case .finished:
|
||||||
@@ -429,7 +446,8 @@ struct ZipStateMachine<
|
|||||||
downstreamContinuation: downstreamContinuation,
|
downstreamContinuation: downstreamContinuation,
|
||||||
error: error,
|
error: error,
|
||||||
task: task,
|
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:
|
case .finished:
|
||||||
@@ -460,7 +478,9 @@ struct ZipStateMachine<
|
|||||||
mutating func cancelled() -> CancelledAction? {
|
mutating func cancelled() -> CancelledAction? {
|
||||||
switch self.state {
|
switch self.state {
|
||||||
case .initial:
|
case .initial:
|
||||||
preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")
|
state = .finished
|
||||||
|
|
||||||
|
return .none
|
||||||
|
|
||||||
case .waitingForDemand(let task, let upstreams):
|
case .waitingForDemand(let task, let upstreams):
|
||||||
// The downstream task got cancelled so we need to cancel our upstream Task
|
// The downstream task got cancelled so we need to cancel our upstream Task
|
||||||
@@ -469,7 +489,8 @@ struct ZipStateMachine<
|
|||||||
|
|
||||||
return .cancelTaskAndUpstreamContinuations(
|
return .cancelTaskAndUpstreamContinuations(
|
||||||
task: task,
|
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):
|
case .zipping(let task, let upstreams, let downstreamContinuation):
|
||||||
@@ -480,7 +501,8 @@ struct ZipStateMachine<
|
|||||||
return .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
return .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
downstreamContinuation: downstreamContinuation,
|
downstreamContinuation: downstreamContinuation,
|
||||||
task: task,
|
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:
|
case .finished:
|
||||||
@@ -522,7 +544,8 @@ struct ZipStateMachine<
|
|||||||
// We also need to resume all upstream continuations now
|
// We also need to resume all upstream continuations now
|
||||||
self.state = .modifying
|
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.0.continuation = nil
|
||||||
upstreams.1.continuation = nil
|
upstreams.1.continuation = nil
|
||||||
upstreams.2.continuation = nil
|
upstreams.2.continuation = nil
|
||||||
|
|||||||
@@ -9,8 +9,16 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence>: Sendable
|
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>
|
typealias StateMachine = ZipStateMachine<Base1, Base2, Base3>
|
||||||
|
|
||||||
private let stateMachine: ManagedCriticalState<StateMachine>
|
private let stateMachine: ManagedCriticalState<StateMachine>
|
||||||
@@ -39,7 +47,7 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
|||||||
func next() async rethrows -> (Base1.Element, Base2.Element, Base3.Element?)? {
|
func next() async rethrows -> (Base1.Element, Base2.Element, Base3.Element?)? {
|
||||||
try await withTaskCancellationHandler {
|
try await withTaskCancellationHandler {
|
||||||
let result = await withUnsafeContinuation { continuation in
|
let result = await withUnsafeContinuation { continuation in
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action: StateMachine.NextAction? = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.next(for: continuation)
|
let action = stateMachine.next(for: continuation)
|
||||||
switch action {
|
switch action {
|
||||||
case .startTask(let base1, let base2, let base3):
|
case .startTask(let base1, let base2, let base3):
|
||||||
@@ -49,44 +57,61 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
|||||||
base1: base1,
|
base1: base1,
|
||||||
base2: base2,
|
base2: base2,
|
||||||
base3: base3,
|
base3: base3,
|
||||||
downStreamContinuation: continuation
|
downstreamContinuation: continuation
|
||||||
)
|
)
|
||||||
|
return nil
|
||||||
|
|
||||||
case .resumeUpstreamContinuations(let upstreamContinuations):
|
case .resumeUpstreamContinuations:
|
||||||
// bases can be iterated over for 1 iteration so their next value can be retrieved
|
return action
|
||||||
upstreamContinuations.forEach { $0.resume() }
|
|
||||||
|
|
||||||
case .resumeDownstreamContinuationWithNil(let continuation):
|
case .resumeDownstreamContinuationWithNil:
|
||||||
// the async sequence is already finished, immediately resuming
|
return action
|
||||||
continuation.resume(returning: .success(nil))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
case .resumeUpstreamContinuations(let upstreamContinuations):
|
||||||
|
// bases can be iterated over for 1 iteration so their next value can be retrieved
|
||||||
|
upstreamContinuations.forEach { $0.resume() }
|
||||||
|
|
||||||
|
case .resumeDownstreamContinuationWithNil(let continuation):
|
||||||
|
// the async sequence is already finished, immediately resuming
|
||||||
|
continuation.resume(returning: .success(nil))
|
||||||
|
|
||||||
|
case .none:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return try result._rethrowGet()
|
return try result._rethrowGet()
|
||||||
|
|
||||||
} onCancel: {
|
} onCancel: {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.cancelled()
|
stateMachine.cancelled()
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
case .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
let downstreamContinuation,
|
let downstreamContinuation,
|
||||||
let task,
|
let task,
|
||||||
let upstreamContinuations
|
let upstreamContinuations
|
||||||
):
|
):
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .success(nil))
|
downstreamContinuation.resume(returning: .success(nil))
|
||||||
|
|
||||||
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,7 +121,7 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
|||||||
base1: Base1,
|
base1: Base1,
|
||||||
base2: Base2,
|
base2: Base2,
|
||||||
base3: Base3?,
|
base3: Base3?,
|
||||||
downStreamContinuation: StateMachine.DownstreamContinuation
|
downstreamContinuation: StateMachine.DownstreamContinuation
|
||||||
) {
|
) {
|
||||||
// This creates a new `Task` that is iterating the upstream
|
// This creates a new `Task` that is iterating the upstream
|
||||||
// sequences. We must store it to cancel it at the right times.
|
// sequences. We must store it to cancel it at the right times.
|
||||||
@@ -112,113 +137,113 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
|||||||
// element from upstream. This continuation is only resumed
|
// element from upstream. This continuation is only resumed
|
||||||
// if the downstream consumer called `next` to signal his demand.
|
// if the downstream consumer called `next` to signal his demand.
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
try await withUnsafeThrowingContinuation { continuation in
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.childTaskSuspended(baseIndex: 0, continuation: continuation)
|
stateMachine.childTaskSuspended(baseIndex: 0, continuation: continuation)
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let upstreamContinuation):
|
case .resumeContinuation(let upstreamContinuation):
|
||||||
upstreamContinuation.resume()
|
upstreamContinuation.resume()
|
||||||
|
|
||||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||||
upstreamContinuation.resume(throwing: error)
|
upstreamContinuation.resume(throwing: error)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let element1 = try await base1Iterator.next() {
|
if let element1 = try await base1Iterator.next() {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.elementProduced((element1, nil, nil))
|
stateMachine.elementProduced((element1, nil, nil))
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let downstreamContinuation, let result):
|
case .resumeContinuation(let downstreamContinuation, let result):
|
||||||
downstreamContinuation.resume(returning: result)
|
downstreamContinuation.resume(returning: result)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.upstreamFinished()
|
stateMachine.upstreamFinished()
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
let downstreamContinuation,
|
let downstreamContinuation,
|
||||||
let task,
|
let task,
|
||||||
let upstreamContinuations
|
let upstreamContinuations
|
||||||
):
|
):
|
||||||
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .success(nil))
|
downstreamContinuation.resume(returning: .success(nil))
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
group.addTask {
|
group.addTask {
|
||||||
var base1Iterator = base2.makeAsyncIterator()
|
var base2Iterator = base2.makeAsyncIterator()
|
||||||
|
|
||||||
while true {
|
while true {
|
||||||
// We are creating a continuation before requesting the next
|
// We are creating a continuation before requesting the next
|
||||||
// element from upstream. This continuation is only resumed
|
// element from upstream. This continuation is only resumed
|
||||||
// if the downstream consumer called `next` to signal his demand.
|
// if the downstream consumer called `next` to signal his demand.
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
try await withUnsafeThrowingContinuation { continuation in
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.childTaskSuspended(baseIndex: 1, continuation: continuation)
|
stateMachine.childTaskSuspended(baseIndex: 1, continuation: continuation)
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let upstreamContinuation):
|
case .resumeContinuation(let upstreamContinuation):
|
||||||
upstreamContinuation.resume()
|
upstreamContinuation.resume()
|
||||||
|
|
||||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||||
upstreamContinuation.resume(throwing: error)
|
upstreamContinuation.resume(throwing: error)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let element2 = try await base1Iterator.next() {
|
if let element2 = try await base2Iterator.next() {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.elementProduced((nil, element2, nil))
|
stateMachine.elementProduced((nil, element2, nil))
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let downstreamContinuation, let result):
|
case .resumeContinuation(let downstreamContinuation, let result):
|
||||||
downstreamContinuation.resume(returning: result)
|
downstreamContinuation.resume(returning: result)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.upstreamFinished()
|
stateMachine.upstreamFinished()
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
let downstreamContinuation,
|
let downstreamContinuation,
|
||||||
let task,
|
let task,
|
||||||
let upstreamContinuations
|
let upstreamContinuations
|
||||||
):
|
):
|
||||||
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .success(nil))
|
downstreamContinuation.resume(returning: .success(nil))
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,73 +251,74 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
|||||||
|
|
||||||
if let base3 = base3 {
|
if let base3 = base3 {
|
||||||
group.addTask {
|
group.addTask {
|
||||||
var base1Iterator = base3.makeAsyncIterator()
|
var base3Iterator = base3.makeAsyncIterator()
|
||||||
|
|
||||||
while true {
|
while true {
|
||||||
// We are creating a continuation before requesting the next
|
// We are creating a continuation before requesting the next
|
||||||
// element from upstream. This continuation is only resumed
|
// element from upstream. This continuation is only resumed
|
||||||
// if the downstream consumer called `next` to signal his demand.
|
// if the downstream consumer called `next` to signal his demand.
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
try await withUnsafeThrowingContinuation { continuation in
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.childTaskSuspended(baseIndex: 2, continuation: continuation)
|
stateMachine.childTaskSuspended(baseIndex: 2, continuation: continuation)
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let upstreamContinuation):
|
case .resumeContinuation(let upstreamContinuation):
|
||||||
upstreamContinuation.resume()
|
upstreamContinuation.resume()
|
||||||
|
|
||||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||||
upstreamContinuation.resume(throwing: error)
|
upstreamContinuation.resume(throwing: error)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let element3 = try await base1Iterator.next() {
|
if let element3 = try await base3Iterator.next() {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.elementProduced((nil, nil, element3))
|
stateMachine.elementProduced((nil, nil, element3))
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuation(let downstreamContinuation, let result):
|
case .resumeContinuation(let downstreamContinuation, let result):
|
||||||
downstreamContinuation.resume(returning: result)
|
downstreamContinuation.resume(returning: result)
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
let action = stateMachine.upstreamFinished()
|
stateMachine.upstreamFinished()
|
||||||
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||||
let downstreamContinuation,
|
let downstreamContinuation,
|
||||||
let task,
|
let task,
|
||||||
let upstreamContinuations
|
let upstreamContinuations
|
||||||
):
|
):
|
||||||
|
|
||||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .success(nil))
|
downstreamContinuation.resume(returning: .success(nil))
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
while !group.isEmpty {
|
||||||
try await group.waitForAll()
|
do {
|
||||||
} catch {
|
try await group.next()
|
||||||
// One of the upstream sequences threw an error
|
} catch {
|
||||||
self.stateMachine.withCriticalRegion { stateMachine in
|
// One of the upstream sequences threw an error
|
||||||
let action = stateMachine.upstreamThrew(error)
|
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||||
|
stateMachine.upstreamThrew(error)
|
||||||
|
}
|
||||||
switch action {
|
switch action {
|
||||||
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
|
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
|
||||||
let downstreamContinuation,
|
let downstreamContinuation,
|
||||||
@@ -304,17 +330,16 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
|||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
downstreamContinuation.resume(returning: .failure(error))
|
downstreamContinuation.resume(returning: .failure(error))
|
||||||
|
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
group.cancelAll()
|
group.cancelAll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stateMachine.taskIsStarted(task: task, downstreamContinuation: downStreamContinuation)
|
stateMachine.taskIsStarted(task: task, downstreamContinuation: downstreamContinuation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,16 +15,32 @@ import AsyncSequenceValidation
|
|||||||
|
|
||||||
extension XCTestCase {
|
extension XCTestCase {
|
||||||
func recordFailure(_ description: String, system: Bool = false, at location: AsyncSequenceValidation.SourceLocation) {
|
func recordFailure(_ description: String, system: Bool = false, at location: AsyncSequenceValidation.SourceLocation) {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
let context = XCTSourceCodeContext(location: XCTSourceCodeLocation(filePath: location.file.description, lineNumber: Int(location.line)))
|
let context = XCTSourceCodeContext(
|
||||||
let issue = XCTIssue(type: system ? .system : .assertionFailure, compactDescription: description, detailedDescription: nil, sourceCodeContext: context, associatedError: nil, attachments: [])
|
location: XCTSourceCodeLocation(filePath: location.file.description, lineNumber: Int(location.line))
|
||||||
|
)
|
||||||
|
let issue = XCTIssue(
|
||||||
|
type: system ? .system : .assertionFailure,
|
||||||
|
compactDescription: description,
|
||||||
|
detailedDescription: nil,
|
||||||
|
sourceCodeContext: context,
|
||||||
|
associatedError: nil,
|
||||||
|
attachments: []
|
||||||
|
)
|
||||||
record(issue)
|
record(issue)
|
||||||
#else
|
#else
|
||||||
XCTFail(description, file: location.file, line: location.line)
|
XCTFail(description, file: location.file, line: location.line)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func validate<Test: AsyncSequenceValidationTest, Theme: AsyncSequenceValidationTheme>(theme: Theme, expectedFailures: Set<String>, @AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test, file: StaticString = #file, line: UInt = #line) {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
func validate<Test: AsyncSequenceValidationTest, Theme: AsyncSequenceValidationTheme>(
|
||||||
|
theme: Theme,
|
||||||
|
expectedFailures: Set<String>,
|
||||||
|
@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test,
|
||||||
|
file: StaticString = #file,
|
||||||
|
line: UInt = #line
|
||||||
|
) {
|
||||||
var expectations = expectedFailures
|
var expectations = expectedFailures
|
||||||
var result: AsyncSequenceValidationDiagram.ExpectationResult?
|
var result: AsyncSequenceValidationDiagram.ExpectationResult?
|
||||||
var failures = [AsyncSequenceValidationDiagram.ExpectationFailure]()
|
var failures = [AsyncSequenceValidationDiagram.ExpectationFailure]()
|
||||||
@@ -61,16 +77,33 @@ extension XCTestCase {
|
|||||||
XCTFail("Expected failure: \(expectation) did not occur.", file: file, line: line)
|
XCTFail("Expected failure: \(expectation) did not occur.", file: file, line: line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func validate<Test: AsyncSequenceValidationTest>(expectedFailures: Set<String>, @AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test, file: StaticString = #file, line: UInt = #line) {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
func validate<Test: AsyncSequenceValidationTest>(
|
||||||
|
expectedFailures: Set<String>,
|
||||||
|
@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test,
|
||||||
|
file: StaticString = #file,
|
||||||
|
line: UInt = #line
|
||||||
|
) {
|
||||||
validate(theme: .ascii, expectedFailures: expectedFailures, build, file: file, line: line)
|
validate(theme: .ascii, expectedFailures: expectedFailures, build, file: file, line: line)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func validate<Test: AsyncSequenceValidationTest, Theme: AsyncSequenceValidationTheme>(theme: Theme, @AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test, file: StaticString = #file, line: UInt = #line) {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public func validate<Test: AsyncSequenceValidationTest, Theme: AsyncSequenceValidationTheme>(
|
||||||
|
theme: Theme,
|
||||||
|
@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test,
|
||||||
|
file: StaticString = #file,
|
||||||
|
line: UInt = #line
|
||||||
|
) {
|
||||||
validate(theme: theme, expectedFailures: [], build, file: file, line: line)
|
validate(theme: theme, expectedFailures: [], build, file: file, line: line)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func validate<Test: AsyncSequenceValidationTest>(@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test, file: StaticString = #file, line: UInt = #line) {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public func validate<Test: AsyncSequenceValidationTest>(
|
||||||
|
@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test,
|
||||||
|
file: StaticString = #file,
|
||||||
|
line: UInt = #line
|
||||||
|
) {
|
||||||
validate(theme: .ascii, expectedFailures: [], build, file: file, line: line)
|
validate(theme: .ascii, expectedFailures: [], build, file: file, line: line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,71 +12,115 @@
|
|||||||
import _CAsyncSequenceValidationSupport
|
import _CAsyncSequenceValidationSupport
|
||||||
|
|
||||||
@resultBuilder
|
@resultBuilder
|
||||||
public struct AsyncSequenceValidationDiagram : Sendable {
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
public struct AsyncSequenceValidationDiagram: Sendable {
|
||||||
public struct Component<T> {
|
public struct Component<T> {
|
||||||
var component: T
|
var component: T
|
||||||
var location: SourceLocation
|
var location: SourceLocation
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AccumulatedInputs {
|
public struct AccumulatedInputs {
|
||||||
var inputs: [Specification] = []
|
var inputs: [Specification] = []
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AccumulatedInputsWithOperation<Operation: AsyncSequence> where Operation.Element == String {
|
public struct AccumulatedInputsWithOperation<Operation: AsyncSequence> where Operation.Element == String {
|
||||||
var inputs: [Specification]
|
var inputs: [Specification]
|
||||||
var operation: Operation
|
var operation: Operation
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildExpression(_ expr: String, file: StaticString = #file, line: UInt = #line) -> Component<String> {
|
public static func buildExpression(
|
||||||
|
_ expr: String,
|
||||||
|
file: StaticString = #file,
|
||||||
|
line: UInt = #line
|
||||||
|
) -> Component<String> {
|
||||||
Component(component: expr, location: SourceLocation(file: file, line: line))
|
Component(component: expr, location: SourceLocation(file: file, line: line))
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildExpression<S: AsyncSequence>(_ expr: S, file: StaticString = #file, line: UInt = #line) -> Component<S> {
|
public static func buildExpression<S: AsyncSequence>(
|
||||||
|
_ expr: S,
|
||||||
|
file: StaticString = #file,
|
||||||
|
line: UInt = #line
|
||||||
|
) -> Component<S> {
|
||||||
Component(component: expr, location: SourceLocation(file: file, line: line))
|
Component(component: expr, location: SourceLocation(file: file, line: line))
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildPartialBlock(first input: Component<String>) -> AccumulatedInputs {
|
public static func buildPartialBlock(first input: Component<String>) -> AccumulatedInputs {
|
||||||
return AccumulatedInputs(inputs: [Specification(specification: input.component, location: input.location)])
|
return AccumulatedInputs(inputs: [Specification(specification: input.component, location: input.location)])
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildPartialBlock<Operation: AsyncSequence>(first operation: Component<Operation>) -> AccumulatedInputsWithOperation<Operation> where Operation.Element == String {
|
public static func buildPartialBlock<Operation: AsyncSequence>(
|
||||||
|
first operation: Component<Operation>
|
||||||
|
) -> AccumulatedInputsWithOperation<Operation> where Operation.Element == String {
|
||||||
return AccumulatedInputsWithOperation(inputs: [], operation: operation.component)
|
return AccumulatedInputsWithOperation(inputs: [], operation: operation.component)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildPartialBlock(accumulated: AccumulatedInputs, next input: Component<String>) -> AccumulatedInputs {
|
public static func buildPartialBlock(
|
||||||
return AccumulatedInputs(inputs: accumulated.inputs + [Specification(specification: input.component, location: input.location)])
|
accumulated: AccumulatedInputs,
|
||||||
|
next input: Component<String>
|
||||||
|
) -> AccumulatedInputs {
|
||||||
|
return AccumulatedInputs(
|
||||||
|
inputs: accumulated.inputs + [Specification(specification: input.component, location: input.location)]
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildPartialBlock<Operation: AsyncSequence>(accumulated: AccumulatedInputs, next operation: Component<Operation>) -> AccumulatedInputsWithOperation<Operation> {
|
public static func buildPartialBlock<Operation: AsyncSequence>(
|
||||||
|
accumulated: AccumulatedInputs,
|
||||||
|
next operation: Component<Operation>
|
||||||
|
) -> AccumulatedInputsWithOperation<Operation> {
|
||||||
return AccumulatedInputsWithOperation(inputs: accumulated.inputs, operation: operation.component)
|
return AccumulatedInputsWithOperation(inputs: accumulated.inputs, operation: operation.component)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildPartialBlock<Operation: AsyncSequence>(accumulated: AccumulatedInputsWithOperation<Operation>, next output: Component<String>) -> some AsyncSequenceValidationTest {
|
public static func buildPartialBlock<Operation: AsyncSequence>(
|
||||||
return Test(inputs: accumulated.inputs, sequence: accumulated.operation, output: Specification(specification: output.component, location: output.location))
|
accumulated: AccumulatedInputsWithOperation<Operation>,
|
||||||
|
next output: Component<String>
|
||||||
|
) -> some AsyncSequenceValidationTest {
|
||||||
|
return Test(
|
||||||
|
inputs: accumulated.inputs,
|
||||||
|
sequence: accumulated.operation,
|
||||||
|
output: Specification(specification: output.component, location: output.location)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildBlock<Operation: AsyncSequence>(_ sequence: Component<Operation>, _ output: Component<String>) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
public static func buildBlock<Operation: AsyncSequence>(
|
||||||
|
_ sequence: Component<Operation>,
|
||||||
|
_ output: Component<String>
|
||||||
|
) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
||||||
let part1 = buildPartialBlock(first: sequence)
|
let part1 = buildPartialBlock(first: sequence)
|
||||||
let part2 = buildPartialBlock(accumulated: part1, next: output)
|
let part2 = buildPartialBlock(accumulated: part1, next: output)
|
||||||
return part2
|
return part2
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildBlock<Operation: AsyncSequence>(_ input1: Component<String>, _ sequence: Component<Operation>, _ output: Component<String>) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
public static func buildBlock<Operation: AsyncSequence>(
|
||||||
|
_ input1: Component<String>,
|
||||||
|
_ sequence: Component<Operation>,
|
||||||
|
_ output: Component<String>
|
||||||
|
) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
||||||
let part1 = buildPartialBlock(first: input1)
|
let part1 = buildPartialBlock(first: input1)
|
||||||
let part2 = buildPartialBlock(accumulated: part1, next: sequence)
|
let part2 = buildPartialBlock(accumulated: part1, next: sequence)
|
||||||
let part3 = buildPartialBlock(accumulated: part2, next: output)
|
let part3 = buildPartialBlock(accumulated: part2, next: output)
|
||||||
return part3
|
return part3
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildBlock<Operation: AsyncSequence>(_ input1: Component<String>, _ input2: Component<String>, _ sequence: Component<Operation>, _ output: Component<String>) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
public static func buildBlock<Operation: AsyncSequence>(
|
||||||
|
_ input1: Component<String>,
|
||||||
|
_ input2: Component<String>,
|
||||||
|
_ sequence: Component<Operation>,
|
||||||
|
_ output: Component<String>
|
||||||
|
) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
||||||
let part1 = buildPartialBlock(first: input1)
|
let part1 = buildPartialBlock(first: input1)
|
||||||
let part2 = buildPartialBlock(accumulated: part1, next: input2)
|
let part2 = buildPartialBlock(accumulated: part1, next: input2)
|
||||||
let part3 = buildPartialBlock(accumulated: part2, next: sequence)
|
let part3 = buildPartialBlock(accumulated: part2, next: sequence)
|
||||||
let part4 = buildPartialBlock(accumulated: part3, next: output)
|
let part4 = buildPartialBlock(accumulated: part3, next: output)
|
||||||
return part4
|
return part4
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildBlock<Operation: AsyncSequence>(_ input1: Component<String>, _ input2: Component<String>, _ input3: Component<String>, _ sequence: Component<Operation>, _ output: Component<String>) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
public static func buildBlock<Operation: AsyncSequence>(
|
||||||
|
_ input1: Component<String>,
|
||||||
|
_ input2: Component<String>,
|
||||||
|
_ input3: Component<String>,
|
||||||
|
_ sequence: Component<Operation>,
|
||||||
|
_ output: Component<String>
|
||||||
|
) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
||||||
let part1 = buildPartialBlock(first: input1)
|
let part1 = buildPartialBlock(first: input1)
|
||||||
let part2 = buildPartialBlock(accumulated: part1, next: input2)
|
let part2 = buildPartialBlock(accumulated: part1, next: input2)
|
||||||
let part3 = buildPartialBlock(accumulated: part2, next: input3)
|
let part3 = buildPartialBlock(accumulated: part2, next: input3)
|
||||||
@@ -84,8 +128,15 @@ public struct AsyncSequenceValidationDiagram : Sendable {
|
|||||||
let part5 = buildPartialBlock(accumulated: part4, next: output)
|
let part5 = buildPartialBlock(accumulated: part4, next: output)
|
||||||
return part5
|
return part5
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func buildBlock<Operation: AsyncSequence>(_ input1: Component<String>, _ input2: Component<String>, _ input3: Component<String>, _ input4: Component<String>, _ sequence: Component<Operation>, _ output: Component<String>) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
public static func buildBlock<Operation: AsyncSequence>(
|
||||||
|
_ input1: Component<String>,
|
||||||
|
_ input2: Component<String>,
|
||||||
|
_ input3: Component<String>,
|
||||||
|
_ input4: Component<String>,
|
||||||
|
_ sequence: Component<Operation>,
|
||||||
|
_ output: Component<String>
|
||||||
|
) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
||||||
let part1 = buildPartialBlock(first: input1)
|
let part1 = buildPartialBlock(first: input1)
|
||||||
let part2 = buildPartialBlock(accumulated: part1, next: input2)
|
let part2 = buildPartialBlock(accumulated: part1, next: input2)
|
||||||
let part3 = buildPartialBlock(accumulated: part2, next: input3)
|
let part3 = buildPartialBlock(accumulated: part2, next: input3)
|
||||||
@@ -94,17 +145,17 @@ public struct AsyncSequenceValidationDiagram : Sendable {
|
|||||||
let part6 = buildPartialBlock(accumulated: part5, next: output)
|
let part6 = buildPartialBlock(accumulated: part5, next: output)
|
||||||
return part6
|
return part6
|
||||||
}
|
}
|
||||||
|
|
||||||
let queue: WorkQueue
|
let queue: WorkQueue
|
||||||
let _clock: Clock
|
let _clock: Clock
|
||||||
|
|
||||||
public var inputs: InputList
|
public var inputs: InputList
|
||||||
|
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
public var clock: Clock {
|
public var clock: Clock {
|
||||||
_clock
|
_clock
|
||||||
}
|
}
|
||||||
|
|
||||||
internal init() {
|
internal init() {
|
||||||
let queue = WorkQueue()
|
let queue = WorkQueue()
|
||||||
self.queue = queue
|
self.queue = queue
|
||||||
@@ -112,4 +163,3 @@ public struct AsyncSequenceValidationDiagram : Sendable {
|
|||||||
self._clock = Clock(queue: queue)
|
self._clock = Clock(queue: queue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -11,22 +11,22 @@
|
|||||||
|
|
||||||
import AsyncAlgorithms
|
import AsyncAlgorithms
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequenceValidationDiagram {
|
extension AsyncSequenceValidationDiagram {
|
||||||
public struct Clock {
|
public struct Clock {
|
||||||
let queue: WorkQueue
|
let queue: WorkQueue
|
||||||
|
|
||||||
init(queue: WorkQueue) {
|
init(queue: WorkQueue) {
|
||||||
self.queue = queue
|
self.queue = queue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public protocol TestClock: Sendable {
|
public protocol TestClock: Sendable {
|
||||||
associatedtype Instant: TestInstant
|
associatedtype Instant: TestInstant
|
||||||
|
|
||||||
var now: Instant { get }
|
var now: Instant { get }
|
||||||
|
|
||||||
func sleep(until deadline: Self.Instant, tolerance: Self.Instant.Duration?) async throws
|
func sleep(until deadline: Self.Instant, tolerance: Self.Instant.Duration?) async throws
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,80 +34,81 @@ public protocol TestInstant: Equatable {
|
|||||||
associatedtype Duration
|
associatedtype Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequenceValidationDiagram.Clock {
|
extension AsyncSequenceValidationDiagram.Clock {
|
||||||
public struct Step: DurationProtocol, Hashable, CustomStringConvertible {
|
public struct Step: DurationProtocol, Hashable, CustomStringConvertible {
|
||||||
internal var rawValue: Int
|
internal var rawValue: Int
|
||||||
|
|
||||||
internal init(_ rawValue: Int) {
|
internal init(_ rawValue: Int) {
|
||||||
self.rawValue = rawValue
|
self.rawValue = rawValue
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func + (lhs: Step, rhs: Step) -> Step {
|
public static func + (lhs: Step, rhs: Step) -> Step {
|
||||||
return .init(lhs.rawValue + rhs.rawValue)
|
return .init(lhs.rawValue + rhs.rawValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func - (lhs: Step, rhs: Step) -> Step {
|
public static func - (lhs: Step, rhs: Step) -> Step {
|
||||||
.init(lhs.rawValue - rhs.rawValue)
|
.init(lhs.rawValue - rhs.rawValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func / (lhs: Step, rhs: Int) -> Step {
|
public static func / (lhs: Step, rhs: Int) -> Step {
|
||||||
.init(lhs.rawValue / rhs)
|
.init(lhs.rawValue / rhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func * (lhs: Step, rhs: Int) -> Step {
|
public static func * (lhs: Step, rhs: Int) -> Step {
|
||||||
.init(lhs.rawValue * rhs)
|
.init(lhs.rawValue * rhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func / (lhs: Step, rhs: Step) -> Double {
|
public static func / (lhs: Step, rhs: Step) -> Double {
|
||||||
Double(lhs.rawValue) / Double(rhs.rawValue)
|
Double(lhs.rawValue) / Double(rhs.rawValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func < (lhs: Step, rhs: Step) -> Bool {
|
public static func < (lhs: Step, rhs: Step) -> Bool {
|
||||||
lhs.rawValue < rhs.rawValue
|
lhs.rawValue < rhs.rawValue
|
||||||
}
|
}
|
||||||
|
|
||||||
public static var zero: Step { .init(0) }
|
public static var zero: Step { .init(0) }
|
||||||
|
|
||||||
public static func steps(_ amount: Int) -> Step {
|
public static func steps(_ amount: Int) -> Step {
|
||||||
return Step(amount)
|
return Step(amount)
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return "step \(rawValue)"
|
return "step \(rawValue)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct Instant: CustomStringConvertible {
|
public struct Instant: CustomStringConvertible {
|
||||||
public typealias Duration = Step
|
public typealias Duration = Step
|
||||||
|
|
||||||
let when: Step
|
let when: Step
|
||||||
|
|
||||||
public func advanced(by duration: Step) -> Instant {
|
public func advanced(by duration: Step) -> Instant {
|
||||||
Instant(when: when + duration)
|
Instant(when: when + duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func duration(to other: Instant) -> Step {
|
public func duration(to other: Instant) -> Step {
|
||||||
other.when - when
|
other.when - when
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func < (lhs: Instant, rhs: Instant) -> Bool {
|
public static func < (lhs: Instant, rhs: Instant) -> Bool {
|
||||||
lhs.when < rhs.when
|
lhs.when < rhs.when
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
// the raw value is 1 indexed in execution but we should report it as 0 indexed
|
// the raw value is 1 indexed in execution but we should report it as 0 indexed
|
||||||
return "tick \(when.rawValue - 1)"
|
return "tick \(when.rawValue - 1)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public var now: Instant {
|
public var now: Instant {
|
||||||
queue.now
|
queue.now
|
||||||
}
|
}
|
||||||
|
|
||||||
public var minimumResolution: Step {
|
public var minimumResolution: Step {
|
||||||
.steps(1)
|
.steps(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func sleep(
|
public func sleep(
|
||||||
until deadline: Instant,
|
until deadline: Instant,
|
||||||
tolerance: Step? = nil
|
tolerance: Step? = nil
|
||||||
@@ -115,7 +116,12 @@ extension AsyncSequenceValidationDiagram.Clock {
|
|||||||
let token = queue.prepare()
|
let token = queue.prepare()
|
||||||
try await withTaskCancellationHandler {
|
try await withTaskCancellationHandler {
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
try await withUnsafeThrowingContinuation { continuation in
|
||||||
queue.enqueue(AsyncSequenceValidationDiagram.Context.currentJob, deadline: deadline, continuation: continuation, token: token)
|
queue.enqueue(
|
||||||
|
AsyncSequenceValidationDiagram.Context.currentJob,
|
||||||
|
deadline: deadline,
|
||||||
|
continuation: continuation,
|
||||||
|
token: token
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} onCancel: {
|
} onCancel: {
|
||||||
queue.cancel(token)
|
queue.cancel(token)
|
||||||
@@ -123,12 +129,20 @@ extension AsyncSequenceValidationDiagram.Clock {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AsyncSequenceValidationDiagram.Clock.Instant: TestInstant { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncSequenceValidationDiagram.Clock.Instant: TestInstant {}
|
||||||
|
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
extension AsyncSequenceValidationDiagram.Clock.Instant: InstantProtocol { }
|
extension AsyncSequenceValidationDiagram.Clock.Instant: InstantProtocol {}
|
||||||
|
|
||||||
extension AsyncSequenceValidationDiagram.Clock: TestClock { }
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncSequenceValidationDiagram.Clock: TestClock {}
|
||||||
|
|
||||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||||
extension AsyncSequenceValidationDiagram.Clock: Clock { }
|
extension AsyncSequenceValidationDiagram.Clock: Clock {}
|
||||||
|
|
||||||
|
// placeholders to avoid warnings
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncSequenceValidationDiagram.Clock.Instant: Hashable {}
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
extension AsyncSequenceValidationDiagram.Clock.Instant: Comparable {}
|
||||||
@@ -9,14 +9,15 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequenceValidationDiagram {
|
extension AsyncSequenceValidationDiagram {
|
||||||
struct Failure: Error, Equatable { }
|
struct Failure: Error, Equatable {}
|
||||||
|
|
||||||
enum ParseFailure: Error, CustomStringConvertible, SourceFailure {
|
enum ParseFailure: Error, CustomStringConvertible, SourceFailure {
|
||||||
case stepInGroup(String, String.Index, SourceLocation)
|
case stepInGroup(String, String.Index, SourceLocation)
|
||||||
case nestedGroup(String, String.Index, SourceLocation)
|
case nestedGroup(String, String.Index, SourceLocation)
|
||||||
case unbalancedNesting(String, String.Index, SourceLocation)
|
case unbalancedNesting(String, String.Index, SourceLocation)
|
||||||
|
|
||||||
var location: SourceLocation {
|
var location: SourceLocation {
|
||||||
switch self {
|
switch self {
|
||||||
case .stepInGroup(_, _, let location): return location
|
case .stepInGroup(_, _, let location): return location
|
||||||
@@ -24,7 +25,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
case .unbalancedNesting(_, _, let location): return location
|
case .unbalancedNesting(_, _, let location): return location
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var description: String {
|
var description: String {
|
||||||
switch self {
|
switch self {
|
||||||
case .stepInGroup:
|
case .stepInGroup:
|
||||||
@@ -36,14 +37,14 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Event {
|
enum Event {
|
||||||
case value(String, String.Index)
|
case value(String, String.Index)
|
||||||
case failure(Error, String.Index)
|
case failure(Error, String.Index)
|
||||||
case finish(String.Index)
|
case finish(String.Index)
|
||||||
case delayNext(String.Index)
|
case delayNext(String.Index)
|
||||||
case cancel(String.Index)
|
case cancel(String.Index)
|
||||||
|
|
||||||
var results: [Result<String?, Error>] {
|
var results: [Result<String?, Error>] {
|
||||||
switch self {
|
switch self {
|
||||||
case .value(let value, _): return [.success(value)]
|
case .value(let value, _): return [.success(value)]
|
||||||
@@ -53,7 +54,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
case .cancel: return []
|
case .cancel: return []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var index: String.Index {
|
var index: String.Index {
|
||||||
switch self {
|
switch self {
|
||||||
case .value(_, let index): return index
|
case .value(_, let index): return index
|
||||||
@@ -63,23 +64,26 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
case .cancel(let index): return index
|
case .cancel(let index): return index
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func parse<Theme: AsyncSequenceValidationTheme>(_ dsl: String, theme: Theme, location: SourceLocation) throws -> [(Clock.Instant, Event)] {
|
static func parse<Theme: AsyncSequenceValidationTheme>(
|
||||||
|
_ dsl: String,
|
||||||
|
theme: Theme,
|
||||||
|
location: SourceLocation
|
||||||
|
) throws -> [(Clock.Instant, Event)] {
|
||||||
var emissions = [(Clock.Instant, Event)]()
|
var emissions = [(Clock.Instant, Event)]()
|
||||||
var when = Clock.Instant(when: .steps(0))
|
var when = Clock.Instant(when: .steps(0))
|
||||||
var string: String?
|
var string: String?
|
||||||
var grouping = 0
|
var grouping = 0
|
||||||
|
|
||||||
for index in dsl.indices {
|
for index in dsl.indices {
|
||||||
let ch = dsl[index]
|
let ch = dsl[index]
|
||||||
switch theme.token(dsl[index], inValue: string != nil) {
|
switch theme.token(dsl[index], inValue: string != nil) {
|
||||||
case .step:
|
case .step:
|
||||||
if string == nil {
|
if string == nil {
|
||||||
if grouping == 0 {
|
guard grouping == 0 else {
|
||||||
when = when.advanced(by: .steps(1))
|
|
||||||
} else {
|
|
||||||
throw ParseFailure.stepInGroup(dsl, index, location)
|
throw ParseFailure.stepInGroup(dsl, index, location)
|
||||||
}
|
}
|
||||||
|
when = when.advanced(by: .steps(1))
|
||||||
} else {
|
} else {
|
||||||
string?.append(ch)
|
string?.append(ch)
|
||||||
}
|
}
|
||||||
@@ -130,11 +134,10 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
emissions.append((when, .value(value, index)))
|
emissions.append((when, .value(value, index)))
|
||||||
}
|
}
|
||||||
case .beginGroup:
|
case .beginGroup:
|
||||||
if grouping == 0 {
|
guard grouping == 0 else {
|
||||||
when = when.advanced(by: .steps(1))
|
|
||||||
} else {
|
|
||||||
throw ParseFailure.nestedGroup(dsl, index, location)
|
throw ParseFailure.nestedGroup(dsl, index, location)
|
||||||
}
|
}
|
||||||
|
when = when.advanced(by: .steps(1))
|
||||||
grouping += 1
|
grouping += 1
|
||||||
case .endGroup:
|
case .endGroup:
|
||||||
grouping -= 1
|
grouping -= 1
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequenceValidationDiagram {
|
extension AsyncSequenceValidationDiagram {
|
||||||
public struct ExpectationResult: Sendable {
|
public struct ExpectationResult: Sendable {
|
||||||
public struct Event: Sendable {
|
public struct Event: Sendable {
|
||||||
@@ -18,7 +19,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
}
|
}
|
||||||
public var expected: [Event]
|
public var expected: [Event]
|
||||||
public var actual: [(Clock.Instant, Result<String?, Error>)]
|
public var actual: [(Clock.Instant, Result<String?, Error>)]
|
||||||
|
|
||||||
func reconstitute<Theme: AsyncSequenceValidationTheme>(_ result: Result<String?, Error>, theme: Theme) -> String {
|
func reconstitute<Theme: AsyncSequenceValidationTheme>(_ result: Result<String?, Error>, theme: Theme) -> String {
|
||||||
var reconstituted = ""
|
var reconstituted = ""
|
||||||
switch result {
|
switch result {
|
||||||
@@ -39,9 +40,13 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
}
|
}
|
||||||
return reconstituted
|
return reconstituted
|
||||||
}
|
}
|
||||||
|
|
||||||
func reconstitute<Theme: AsyncSequenceValidationTheme>(_ events: [Clock.Instant : [Result<String?, Error>]], theme: Theme, end: Clock.Instant) -> String {
|
func reconstitute<Theme: AsyncSequenceValidationTheme>(
|
||||||
var now = Clock.Instant(when: .steps(1)) // adjust for the offset index
|
_ events: [Clock.Instant: [Result<String?, Error>]],
|
||||||
|
theme: Theme,
|
||||||
|
end: Clock.Instant
|
||||||
|
) -> String {
|
||||||
|
var now = Clock.Instant(when: .steps(1)) // adjust for the offset index
|
||||||
var reconstituted = ""
|
var reconstituted = ""
|
||||||
while now <= end {
|
while now <= end {
|
||||||
if let results = events[now] {
|
if let results = events[now] {
|
||||||
@@ -61,11 +66,11 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
}
|
}
|
||||||
return reconstituted
|
return reconstituted
|
||||||
}
|
}
|
||||||
|
|
||||||
public func reconstituteExpected<Theme: AsyncSequenceValidationTheme>(theme: Theme) -> String {
|
public func reconstituteExpected<Theme: AsyncSequenceValidationTheme>(theme: Theme) -> String {
|
||||||
var events = [Clock.Instant : [Result<String?, Error>]]()
|
var events = [Clock.Instant: [Result<String?, Error>]]()
|
||||||
var end: Clock.Instant = Clock.Instant(when: .zero)
|
var end: Clock.Instant = Clock.Instant(when: .zero)
|
||||||
|
|
||||||
for expectation in expected {
|
for expectation in expected {
|
||||||
let when = expectation.when
|
let when = expectation.when
|
||||||
let result = expectation.result
|
let result = expectation.result
|
||||||
@@ -74,25 +79,25 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
end = when
|
end = when
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return reconstitute(events, theme: theme, end: end)
|
return reconstitute(events, theme: theme, end: end)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func reconstituteActual<Theme: AsyncSequenceValidationTheme>(theme: Theme) -> String {
|
public func reconstituteActual<Theme: AsyncSequenceValidationTheme>(theme: Theme) -> String {
|
||||||
var events = [Clock.Instant : [Result<String?, Error>]]()
|
var events = [Clock.Instant: [Result<String?, Error>]]()
|
||||||
var end: Clock.Instant = Clock.Instant(when: .zero)
|
var end: Clock.Instant = Clock.Instant(when: .zero)
|
||||||
|
|
||||||
for (when, result) in actual {
|
for (when, result) in actual {
|
||||||
events[when, default: []].append(result)
|
events[when, default: []].append(result)
|
||||||
if when > end {
|
if when > end {
|
||||||
end = when
|
end = when
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return reconstitute(events, theme: theme, end: end)
|
return reconstitute(events, theme: theme, end: end)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct ExpectationFailure: Sendable, CustomStringConvertible {
|
public struct ExpectationFailure: Sendable, CustomStringConvertible {
|
||||||
public enum Kind: Sendable {
|
public enum Kind: Sendable {
|
||||||
case expectedFinishButGotValue(String)
|
case expectedFinishButGotValue(String)
|
||||||
@@ -108,23 +113,23 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
case unexpectedValue(String)
|
case unexpectedValue(String)
|
||||||
case unexpectedFinish
|
case unexpectedFinish
|
||||||
case unexpectedFailure(Error)
|
case unexpectedFailure(Error)
|
||||||
|
|
||||||
case specificationViolationGotValueAfterIteration(String)
|
case specificationViolationGotValueAfterIteration(String)
|
||||||
case specificationViolationGotFailureAfterIteration(Error)
|
case specificationViolationGotFailureAfterIteration(Error)
|
||||||
}
|
}
|
||||||
public var when: Clock.Instant
|
public var when: Clock.Instant
|
||||||
public var kind: Kind
|
public var kind: Kind
|
||||||
|
|
||||||
public var specification: Specification?
|
public var specification: Specification?
|
||||||
public var index: String.Index?
|
public var index: String.Index?
|
||||||
|
|
||||||
init(when: Clock.Instant, kind: Kind, specification: Specification? = nil, index: String.Index? = nil) {
|
init(when: Clock.Instant, kind: Kind, specification: Specification? = nil, index: String.Index? = nil) {
|
||||||
self.when = when
|
self.when = when
|
||||||
self.kind = kind
|
self.kind = kind
|
||||||
self.specification = specification
|
self.specification = specification
|
||||||
self.index = index
|
self.index = index
|
||||||
}
|
}
|
||||||
|
|
||||||
var reason: String {
|
var reason: String {
|
||||||
switch kind {
|
switch kind {
|
||||||
case .expectedFinishButGotValue(let actual):
|
case .expectedFinishButGotValue(let actual):
|
||||||
@@ -159,7 +164,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
return "specification violation got failure after iteration terminated"
|
return "specification violation got failure after iteration terminated"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return reason + " at tick \(when.when.rawValue - 1)"
|
return reason + " at tick \(when.when.rawValue - 1)"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,35 +9,36 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequenceValidationDiagram {
|
extension AsyncSequenceValidationDiagram {
|
||||||
public struct Specification: Sendable {
|
public struct Specification: Sendable {
|
||||||
public let specification: String
|
public let specification: String
|
||||||
public let location: SourceLocation
|
public let location: SourceLocation
|
||||||
|
|
||||||
init(specification: String, location: SourceLocation) {
|
init(specification: String, location: SourceLocation) {
|
||||||
self.specification = specification
|
self.specification = specification
|
||||||
self.location = location
|
self.location = location
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct Input: AsyncSequence, Sendable {
|
public struct Input: AsyncSequence, Sendable {
|
||||||
public typealias Element = String
|
public typealias Element = String
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
var emissions = [(Clock.Instant, Event)]()
|
var emissions = [(Clock.Instant, Event)]()
|
||||||
}
|
}
|
||||||
|
|
||||||
let state = ManagedCriticalState(State())
|
let state = ManagedCriticalState(State())
|
||||||
let queue: WorkQueue
|
let queue: WorkQueue
|
||||||
let index: Int
|
let index: Int
|
||||||
|
|
||||||
public struct Iterator: AsyncIteratorProtocol, Sendable {
|
public struct Iterator: AsyncIteratorProtocol, Sendable {
|
||||||
let state: ManagedCriticalState<State>
|
let state: ManagedCriticalState<State>
|
||||||
let queue: WorkQueue
|
let queue: WorkQueue
|
||||||
let index: Int
|
let index: Int
|
||||||
var active: (Clock.Instant, [Result<String?, Error>])?
|
var active: (Clock.Instant, [Result<String?, Error>])?
|
||||||
var eventIndex = 0
|
var eventIndex = 0
|
||||||
|
|
||||||
mutating func apply(when: Clock.Instant, results: [Result<String?, Error>]) async throws -> Element? {
|
mutating func apply(when: Clock.Instant, results: [Result<String?, Error>]) async throws -> Element? {
|
||||||
let token = queue.prepare()
|
let token = queue.prepare()
|
||||||
if eventIndex + 1 >= results.count {
|
if eventIndex + 1 >= results.count {
|
||||||
@@ -52,17 +53,22 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
}
|
}
|
||||||
return try await withTaskCancellationHandler {
|
return try await withTaskCancellationHandler {
|
||||||
try await withUnsafeThrowingContinuation { continuation in
|
try await withUnsafeThrowingContinuation { continuation in
|
||||||
queue.enqueue(Context.currentJob, deadline: when, continuation: continuation, results[eventIndex], index: index, token: token)
|
queue.enqueue(
|
||||||
|
Context.currentJob,
|
||||||
|
deadline: when,
|
||||||
|
continuation: continuation,
|
||||||
|
results[eventIndex],
|
||||||
|
index: index,
|
||||||
|
token: token
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} onCancel: { [queue] in
|
} onCancel: { [queue] in
|
||||||
queue.cancel(token)
|
queue.cancel(token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public mutating func next() async throws -> Element? {
|
public mutating func next() async throws -> Element? {
|
||||||
if let (when, results) = active {
|
guard let (when, results) = active else {
|
||||||
return try await apply(when: when, results: results)
|
|
||||||
} else {
|
|
||||||
let next = state.withCriticalRegion { state -> (Clock.Instant, Event)? in
|
let next = state.withCriticalRegion { state -> (Clock.Instant, Event)? in
|
||||||
guard state.emissions.count > 0 else {
|
guard state.emissions.count > 0 else {
|
||||||
return nil
|
return nil
|
||||||
@@ -77,36 +83,37 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
active = (when, results)
|
active = (when, results)
|
||||||
return try await apply(when: when, results: results)
|
return try await apply(when: when, results: results)
|
||||||
}
|
}
|
||||||
|
return try await apply(when: when, results: results)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func makeAsyncIterator() -> Iterator {
|
public func makeAsyncIterator() -> Iterator {
|
||||||
Iterator(state: state, queue: queue, index: index)
|
Iterator(state: state, queue: queue, index: index)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parse<Theme: AsyncSequenceValidationTheme>(_ dsl: String, theme: Theme, location: SourceLocation) throws {
|
func parse<Theme: AsyncSequenceValidationTheme>(_ dsl: String, theme: Theme, location: SourceLocation) throws {
|
||||||
let emissions = try Event.parse(dsl, theme: theme, location: location)
|
let emissions = try Event.parse(dsl, theme: theme, location: location)
|
||||||
state.withCriticalRegion { state in
|
state.withCriticalRegion { state in
|
||||||
state.emissions = emissions
|
state.emissions = emissions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var end: Clock.Instant? {
|
var end: Clock.Instant? {
|
||||||
return state.withCriticalRegion { state in
|
return state.withCriticalRegion { state in
|
||||||
state.emissions.map { $0.0 }.sorted().last
|
state.emissions.map { $0.0 }.sorted().last
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct InputList: RandomAccessCollection, Sendable {
|
public struct InputList: RandomAccessCollection, Sendable {
|
||||||
let state = ManagedCriticalState([Input]())
|
let state = ManagedCriticalState([Input]())
|
||||||
let queue: WorkQueue
|
let queue: WorkQueue
|
||||||
|
|
||||||
public var startIndex: Int { return 0 }
|
public var startIndex: Int { return 0 }
|
||||||
public var endIndex: Int {
|
public var endIndex: Int {
|
||||||
state.withCriticalRegion { $0.count }
|
state.withCriticalRegion { $0.count }
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscript(position: Int) -> AsyncSequenceValidationDiagram.Input {
|
public subscript(position: Int) -> AsyncSequenceValidationDiagram.Input {
|
||||||
get {
|
get {
|
||||||
return state.withCriticalRegion { state in
|
return state.withCriticalRegion { state in
|
||||||
|
|||||||
@@ -11,14 +11,15 @@
|
|||||||
|
|
||||||
import _CAsyncSequenceValidationSupport
|
import _CAsyncSequenceValidationSupport
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
struct Job: Hashable, @unchecked Sendable {
|
struct Job: Hashable, @unchecked Sendable {
|
||||||
let job: JobRef
|
let job: JobRef
|
||||||
|
|
||||||
init(_ job: JobRef) {
|
init(_ job: JobRef) {
|
||||||
self.job = job
|
self.job = job
|
||||||
}
|
}
|
||||||
|
|
||||||
func execute() {
|
func execute() {
|
||||||
_swiftJobRun(unsafeBitCast(job, to: UnownedJob.self), AsyncSequenceValidationDiagram.Context.executor.asUnownedSerialExecutor())
|
_swiftJobRun(unsafeBitCast(job, to: UnownedJob.self), AsyncSequenceValidationDiagram.Context.unownedExecutor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,12 +12,12 @@
|
|||||||
public struct SourceLocation: Sendable, CustomStringConvertible {
|
public struct SourceLocation: Sendable, CustomStringConvertible {
|
||||||
public var file: StaticString
|
public var file: StaticString
|
||||||
public var line: UInt
|
public var line: UInt
|
||||||
|
|
||||||
public init(file: StaticString, line: UInt) {
|
public init(file: StaticString, line: UInt) {
|
||||||
self.file = file
|
self.file = file
|
||||||
self.line = line
|
self.line = line
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return "\(file):\(line)"
|
return "\(file):\(line)"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,65 +12,88 @@
|
|||||||
import _CAsyncSequenceValidationSupport
|
import _CAsyncSequenceValidationSupport
|
||||||
|
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
@_implementationOnly import Darwin
|
import Darwin
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc)
|
||||||
@_implementationOnly import Glibc
|
import Glibc
|
||||||
|
#elseif canImport(Musl)
|
||||||
|
import Musl
|
||||||
|
#elseif canImport(Bionic)
|
||||||
|
import Bionic
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
#error("TODO: Port TaskDriver threading to windows")
|
#error("TODO: Port TaskDriver threading to windows")
|
||||||
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
|
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
|
||||||
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
|
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
#elseif canImport(Glibc)
|
#elseif (canImport(Glibc) && !os(Android)) || canImport(Musl)
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
|
func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
|
||||||
Unmanaged<TaskDriver>.fromOpaque(raw!).takeRetainedValue().run()
|
Unmanaged<TaskDriver>.fromOpaque(raw!).takeRetainedValue().run()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
#elseif os(Android)
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
|
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer {
|
||||||
|
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
|
||||||
|
return UnsafeMutableRawPointer(bitPattern: 0xdeadbee)!
|
||||||
|
}
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
#error("TODO: Port TaskDriver threading to windows")
|
#error("TODO: Port TaskDriver threading to windows")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
final class TaskDriver {
|
final class TaskDriver {
|
||||||
let work: (TaskDriver) -> Void
|
let work: (TaskDriver) -> Void
|
||||||
let queue: WorkQueue
|
let queue: WorkQueue
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
var thread: pthread_t?
|
var thread: pthread_t?
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
var thread = pthread_t()
|
var thread = pthread_t()
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
#error("TODO: Port TaskDriver threading to windows")
|
#error("TODO: Port TaskDriver threading to windows")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
init(queue: WorkQueue, _ work: @escaping (TaskDriver) -> Void) {
|
init(queue: WorkQueue, _ work: @escaping (TaskDriver) -> Void) {
|
||||||
self.queue = queue
|
self.queue = queue
|
||||||
self.work = work
|
self.work = work
|
||||||
}
|
}
|
||||||
|
|
||||||
func start() {
|
func start() {
|
||||||
pthread_create(&thread, nil, start_thread,
|
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
Unmanaged.passRetained(self).toOpaque())
|
pthread_create(
|
||||||
|
&thread,
|
||||||
|
nil,
|
||||||
|
start_thread,
|
||||||
|
Unmanaged.passRetained(self).toOpaque()
|
||||||
|
)
|
||||||
|
#elseif canImport(WinSDK)
|
||||||
|
#error("TODO: Port TaskDriver threading to windows")
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func run() {
|
func run() {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
pthread_setname_np("Validation Diagram Clock Driver")
|
pthread_setname_np("Validation Diagram Clock Driver")
|
||||||
#endif
|
#endif
|
||||||
work(self)
|
work(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
func join() {
|
func join() {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
pthread_join(thread!, nil)
|
pthread_join(thread!, nil)
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
pthread_join(thread, nil)
|
pthread_join(thread, nil)
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
#error("TODO: Port TaskDriver threading to windows")
|
#error("TODO: Port TaskDriver threading to windows")
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func enqueue(_ job: JobRef) {
|
func enqueue(_ job: JobRef) {
|
||||||
let job = Job(job)
|
let job = Job(job)
|
||||||
queue.enqueue(AsyncSequenceValidationDiagram.Context.currentJob) {
|
queue.enqueue(AsyncSequenceValidationDiagram.Context.currentJob) {
|
||||||
@@ -81,4 +104,3 @@ final class TaskDriver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -13,51 +13,66 @@ import _CAsyncSequenceValidationSupport
|
|||||||
import AsyncAlgorithms
|
import AsyncAlgorithms
|
||||||
|
|
||||||
@_silgen_name("swift_job_run")
|
@_silgen_name("swift_job_run")
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
internal func _swiftJobRun(
|
internal func _swiftJobRun(
|
||||||
_ job: UnownedJob,
|
_ job: UnownedJob,
|
||||||
_ executor: UnownedSerialExecutor
|
_ executor: UnownedSerialExecutor
|
||||||
) -> ()
|
)
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public protocol AsyncSequenceValidationTest: Sendable {
|
public protocol AsyncSequenceValidationTest: Sendable {
|
||||||
var inputs: [AsyncSequenceValidationDiagram.Specification] { get }
|
var inputs: [AsyncSequenceValidationDiagram.Specification] { get }
|
||||||
var output: AsyncSequenceValidationDiagram.Specification { get }
|
var output: AsyncSequenceValidationDiagram.Specification { get }
|
||||||
|
|
||||||
func test<C: TestClock>(with clock: C, activeTicks: [C.Instant], output: AsyncSequenceValidationDiagram.Specification, _ event: (String) -> Void) async throws
|
func test<C: TestClock>(
|
||||||
|
with clock: C,
|
||||||
|
activeTicks: [C.Instant],
|
||||||
|
output: AsyncSequenceValidationDiagram.Specification,
|
||||||
|
_ event: (String) -> Void
|
||||||
|
) async throws
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequenceValidationDiagram {
|
extension AsyncSequenceValidationDiagram {
|
||||||
struct Test<Operation: AsyncSequence>: AsyncSequenceValidationTest, @unchecked Sendable where Operation.Element == String {
|
struct Test<Operation: AsyncSequence>: AsyncSequenceValidationTest, @unchecked Sendable
|
||||||
|
where Operation.Element == String {
|
||||||
let inputs: [Specification]
|
let inputs: [Specification]
|
||||||
let sequence: Operation
|
let sequence: Operation
|
||||||
let output: Specification
|
let output: Specification
|
||||||
|
|
||||||
func test<C: TestClock>(with clock: C, activeTicks: [C.Instant], output: Specification, _ event: (String) -> Void) async throws {
|
func test<C: TestClock>(
|
||||||
|
with clock: C,
|
||||||
|
activeTicks: [C.Instant],
|
||||||
|
output: Specification,
|
||||||
|
_ event: (String) -> Void
|
||||||
|
) async throws {
|
||||||
var iterator = sequence.makeAsyncIterator()
|
var iterator = sequence.makeAsyncIterator()
|
||||||
do {
|
do {
|
||||||
for tick in activeTicks {
|
for tick in activeTicks {
|
||||||
if tick != clock.now {
|
if tick != clock.now {
|
||||||
try await clock.sleep(until: tick, tolerance: nil)
|
try await clock.sleep(until: tick, tolerance: nil)
|
||||||
}
|
}
|
||||||
if let item = try await iterator.next() {
|
guard let item = try await iterator.next() else {
|
||||||
event(item)
|
|
||||||
} else {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
event(item)
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
if let pastEnd = try await iterator.next(){
|
if let pastEnd = try await iterator.next() {
|
||||||
let failure = ExpectationFailure(
|
let failure = ExpectationFailure(
|
||||||
when: Context.clock!.now,
|
when: Context.clock!.now,
|
||||||
kind: .specificationViolationGotValueAfterIteration(pastEnd),
|
kind: .specificationViolationGotValueAfterIteration(pastEnd),
|
||||||
specification: output)
|
specification: output
|
||||||
|
)
|
||||||
Context.specificationFailures.append(failure)
|
Context.specificationFailures.append(failure)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
let failure = ExpectationFailure(
|
let failure = ExpectationFailure(
|
||||||
when: Context.clock!.now,
|
when: Context.clock!.now,
|
||||||
kind: .specificationViolationGotFailureAfterIteration(error),
|
kind: .specificationViolationGotFailureAfterIteration(error),
|
||||||
specification: output)
|
specification: output
|
||||||
|
)
|
||||||
Context.specificationFailures.append(failure)
|
Context.specificationFailures.append(failure)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@@ -65,34 +80,74 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Context {
|
struct Context {
|
||||||
|
#if swift(<5.9)
|
||||||
final class ClockExecutor: SerialExecutor {
|
final class ClockExecutor: SerialExecutor {
|
||||||
func enqueue(_ job: UnownedJob) {
|
func enqueue(_ job: UnownedJob) {
|
||||||
job._runSynchronously(on: asUnownedSerialExecutor())
|
job._runSynchronously(on: self.asUnownedSerialExecutor())
|
||||||
}
|
}
|
||||||
|
|
||||||
func asUnownedSerialExecutor() -> UnownedSerialExecutor {
|
func asUnownedSerialExecutor() -> UnownedSerialExecutor {
|
||||||
UnownedSerialExecutor(ordinary: self)
|
UnownedSerialExecutor(ordinary: self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static let _executor = ClockExecutor()
|
||||||
|
|
||||||
|
static var unownedExecutor: UnownedSerialExecutor {
|
||||||
|
_executor.asUnownedSerialExecutor()
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
@available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
|
||||||
|
final class ClockExecutor_5_9: SerialExecutor {
|
||||||
|
func enqueue(_ job: __owned ExecutorJob) {
|
||||||
|
job.runSynchronously(on: asUnownedSerialExecutor())
|
||||||
|
}
|
||||||
|
|
||||||
|
func asUnownedSerialExecutor() -> UnownedSerialExecutor {
|
||||||
|
UnownedSerialExecutor(ordinary: self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final class ClockExecutor_Pre5_9: SerialExecutor {
|
||||||
|
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
||||||
|
@available(*, deprecated, message: "Implement 'enqueue(_: __owned ExecutorJob)' instead")
|
||||||
|
func enqueue(_ job: UnownedJob) {
|
||||||
|
job._runSynchronously(on: self.asUnownedSerialExecutor())
|
||||||
|
}
|
||||||
|
|
||||||
|
func asUnownedSerialExecutor() -> UnownedSerialExecutor {
|
||||||
|
UnownedSerialExecutor(ordinary: self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static let _executor: AnyObject = {
|
||||||
|
guard #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) else {
|
||||||
|
return ClockExecutor_Pre5_9()
|
||||||
|
}
|
||||||
|
return ClockExecutor_5_9()
|
||||||
|
}()
|
||||||
|
|
||||||
|
static var unownedExecutor: UnownedSerialExecutor {
|
||||||
|
(_executor as! any SerialExecutor).asUnownedSerialExecutor()
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static var clock: Clock?
|
static var clock: Clock?
|
||||||
|
|
||||||
static let executor = ClockExecutor()
|
|
||||||
|
|
||||||
static var driver: TaskDriver?
|
static var driver: TaskDriver?
|
||||||
|
|
||||||
static var currentJob: Job?
|
static var currentJob: Job?
|
||||||
|
|
||||||
static var specificationFailures = [ExpectationFailure]()
|
static var specificationFailures = [ExpectationFailure]()
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ActualResult {
|
enum ActualResult {
|
||||||
case success(String?)
|
case success(String?)
|
||||||
case failure(Error)
|
case failure(Error)
|
||||||
case none
|
case none
|
||||||
|
|
||||||
init(_ result: Result<String?, Error>?) {
|
init(_ result: Result<String?, Error>?) {
|
||||||
if let result = result {
|
if let result = result {
|
||||||
switch result {
|
switch result {
|
||||||
@@ -106,7 +161,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func validate<Theme: AsyncSequenceValidationTheme>(
|
static func validate<Theme: AsyncSequenceValidationTheme>(
|
||||||
inputs: [Specification],
|
inputs: [Specification],
|
||||||
output: Specification,
|
output: Specification,
|
||||||
@@ -117,21 +172,21 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
let result = ExpectationResult(expected: expected, actual: actual)
|
let result = ExpectationResult(expected: expected, actual: actual)
|
||||||
var failures = Context.specificationFailures
|
var failures = Context.specificationFailures
|
||||||
Context.specificationFailures.removeAll()
|
Context.specificationFailures.removeAll()
|
||||||
|
|
||||||
let actualTimes = actual.map { when, _ in when }
|
let actualTimes = actual.map { when, _ in when }
|
||||||
let expectedTimes = expected.map { $0.when }
|
let expectedTimes = expected.map { $0.when }
|
||||||
|
|
||||||
var expectedMap = [Clock.Instant: [ExpectationResult.Event]]()
|
var expectedMap = [Clock.Instant: [ExpectationResult.Event]]()
|
||||||
var actualMap = [Clock.Instant: [Result<String?, Error>]]()
|
var actualMap = [Clock.Instant: [Result<String?, Error>]]()
|
||||||
|
|
||||||
for event in expected {
|
for event in expected {
|
||||||
expectedMap[event.when, default: []].append(event)
|
expectedMap[event.when, default: []].append(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (when, result) in actual {
|
for (when, result) in actual {
|
||||||
actualMap[when, default: []].append(result)
|
actualMap[when, default: []].append(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
let allTimes = Set(actualTimes + expectedTimes).sorted()
|
let allTimes = Set(actualTimes + expectedTimes).sorted()
|
||||||
for when in allTimes {
|
for when in allTimes {
|
||||||
let expectedResults = expectedMap[when] ?? []
|
let expectedResults = expectedMap[when] ?? []
|
||||||
@@ -149,21 +204,24 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
when: when,
|
when: when,
|
||||||
kind: .expectedMismatch(expected, actual),
|
kind: .expectedMismatch(expected, actual),
|
||||||
specification: output,
|
specification: output,
|
||||||
index: expectedEvent.offset)
|
index: expectedEvent.offset
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
}
|
}
|
||||||
case (.none, .some(let actual)):
|
case (.none, .some(let actual)):
|
||||||
let failure = ExpectationFailure(
|
let failure = ExpectationFailure(
|
||||||
when: when,
|
when: when,
|
||||||
kind: .expectedFinishButGotValue(actual),
|
kind: .expectedFinishButGotValue(actual),
|
||||||
specification: output)
|
specification: output
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
case (.some(let expected), .none):
|
case (.some(let expected), .none):
|
||||||
let failure = ExpectationFailure(
|
let failure = ExpectationFailure(
|
||||||
when: when,
|
when: when,
|
||||||
kind: .expectedValueButGotFinished(expected),
|
kind: .expectedValueButGotFinished(expected),
|
||||||
specification: output,
|
specification: output,
|
||||||
index: expectedEvent.offset)
|
index: expectedEvent.offset
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
case (.none, .none):
|
case (.none, .none):
|
||||||
break
|
break
|
||||||
@@ -174,14 +232,16 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
when: when,
|
when: when,
|
||||||
kind: .expectedValueButGotFailure(expected, actual),
|
kind: .expectedValueButGotFailure(expected, actual),
|
||||||
specification: output,
|
specification: output,
|
||||||
index: expectedEvent.offset)
|
index: expectedEvent.offset
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
} else {
|
} else {
|
||||||
let failure = ExpectationFailure(
|
let failure = ExpectationFailure(
|
||||||
when: when,
|
when: when,
|
||||||
kind: .expectedFinishButGotFailure(actual),
|
kind: .expectedFinishButGotFailure(actual),
|
||||||
specification: output,
|
specification: output,
|
||||||
index: expectedEvent.offset)
|
index: expectedEvent.offset
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
}
|
}
|
||||||
case (.success(let expected), .none):
|
case (.success(let expected), .none):
|
||||||
@@ -191,14 +251,16 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
when: when,
|
when: when,
|
||||||
kind: .expectedValue(expected),
|
kind: .expectedValue(expected),
|
||||||
specification: output,
|
specification: output,
|
||||||
index: expectedEvent.offset)
|
index: expectedEvent.offset
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
case .none:
|
case .none:
|
||||||
let failure = ExpectationFailure(
|
let failure = ExpectationFailure(
|
||||||
when: when,
|
when: when,
|
||||||
kind: .expectedFinish,
|
kind: .expectedFinish,
|
||||||
specification: output,
|
specification: output,
|
||||||
index: expectedEvent.offset)
|
index: expectedEvent.offset
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
}
|
}
|
||||||
case (.failure(let expected), .success(let actual)):
|
case (.failure(let expected), .success(let actual)):
|
||||||
@@ -207,14 +269,16 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
when: when,
|
when: when,
|
||||||
kind: .expectedFailureButGotValue(expected, actual),
|
kind: .expectedFailureButGotValue(expected, actual),
|
||||||
specification: output,
|
specification: output,
|
||||||
index: expectedEvent.offset)
|
index: expectedEvent.offset
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
} else {
|
} else {
|
||||||
let failure = ExpectationFailure(
|
let failure = ExpectationFailure(
|
||||||
when: when,
|
when: when,
|
||||||
kind: .expectedFailureButGotFinish(expected),
|
kind: .expectedFailureButGotFinish(expected),
|
||||||
specification: output,
|
specification: output,
|
||||||
index: expectedEvent.offset)
|
index: expectedEvent.offset
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
}
|
}
|
||||||
case (.failure, .failure):
|
case (.failure, .failure):
|
||||||
@@ -224,7 +288,8 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
when: when,
|
when: when,
|
||||||
kind: .expectedFailure(expected),
|
kind: .expectedFailure(expected),
|
||||||
specification: output,
|
specification: output,
|
||||||
index: expectedEvent.offset)
|
index: expectedEvent.offset
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,28 +301,31 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
let failure = ExpectationFailure(
|
let failure = ExpectationFailure(
|
||||||
when: when,
|
when: when,
|
||||||
kind: .unexpectedValue(actual),
|
kind: .unexpectedValue(actual),
|
||||||
specification: output)
|
specification: output
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
case .none:
|
case .none:
|
||||||
let failure = ExpectationFailure(
|
let failure = ExpectationFailure(
|
||||||
when: when,
|
when: when,
|
||||||
kind: .unexpectedFinish,
|
kind: .unexpectedFinish,
|
||||||
specification: output)
|
specification: output
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
}
|
}
|
||||||
case .failure(let actual):
|
case .failure(let actual):
|
||||||
let failure = ExpectationFailure(
|
let failure = ExpectationFailure(
|
||||||
when: when,
|
when: when,
|
||||||
kind: .unexpectedFailure(actual),
|
kind: .unexpectedFailure(actual),
|
||||||
specification: output)
|
specification: output
|
||||||
|
)
|
||||||
failures.append(failure)
|
failures.append(failure)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (result, failures)
|
return (result, failures)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func test<Test: AsyncSequenceValidationTest, Theme: AsyncSequenceValidationTheme>(
|
public static func test<Test: AsyncSequenceValidationTest, Theme: AsyncSequenceValidationTheme>(
|
||||||
theme: Theme,
|
theme: Theme,
|
||||||
@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test
|
@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test
|
||||||
@@ -269,30 +337,32 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
// fault in all inputs
|
// fault in all inputs
|
||||||
_ = diagram.inputs[index]
|
_ = diagram.inputs[index]
|
||||||
}
|
}
|
||||||
|
|
||||||
for (index, input) in diagram.inputs.enumerated() {
|
for (index, input) in diagram.inputs.enumerated() {
|
||||||
let inputSpecification = test.inputs[index]
|
let inputSpecification = test.inputs[index]
|
||||||
try input.parse(inputSpecification.specification, theme: theme, location: inputSpecification.location)
|
try input.parse(inputSpecification.specification, theme: theme, location: inputSpecification.location)
|
||||||
}
|
}
|
||||||
|
|
||||||
let parsedOutput = try Event.parse(test.output.specification, theme: theme, location: test.output.location)
|
let parsedOutput = try Event.parse(test.output.specification, theme: theme, location: test.output.location)
|
||||||
let cancelEvents = Set(parsedOutput.filter { when, event in
|
let cancelEvents = Set(
|
||||||
switch event {
|
parsedOutput.filter { when, event in
|
||||||
case .cancel: return true
|
switch event {
|
||||||
default: return false
|
case .cancel: return true
|
||||||
}
|
default: return false
|
||||||
}.map { when, _ in return when })
|
}
|
||||||
|
}.map { when, _ in return when }
|
||||||
|
)
|
||||||
|
|
||||||
let activeTicks = parsedOutput.reduce(into: [Clock.Instant.init(when: .zero)]) { events, thisEvent in
|
let activeTicks = parsedOutput.reduce(into: [Clock.Instant.init(when: .zero)]) { events, thisEvent in
|
||||||
switch thisEvent {
|
switch thisEvent {
|
||||||
case (let when, .delayNext(_)):
|
case (let when, .delayNext(_)):
|
||||||
events.removeLast()
|
events.removeLast()
|
||||||
events.append(when.advanced(by: .steps(1)))
|
events.append(when.advanced(by: .steps(1)))
|
||||||
case (let when, _):
|
case (let when, _):
|
||||||
events.append(when)
|
events.append(when)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var expected = [ExpectationResult.Event]()
|
var expected = [ExpectationResult.Event]()
|
||||||
for (when, event) in parsedOutput {
|
for (when, event) in parsedOutput {
|
||||||
for result in event.results {
|
for result in event.results {
|
||||||
@@ -300,7 +370,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
let times = parsedOutput.map { when, _ in when }
|
let times = parsedOutput.map { when, _ in when }
|
||||||
|
|
||||||
guard let end = (times + diagram.inputs.compactMap { $0.end }).max() else {
|
guard let end = (times + diagram.inputs.compactMap { $0.end }).max() else {
|
||||||
return (ExpectationResult(expected: [], actual: []), [])
|
return (ExpectationResult(expected: [], actual: []), [])
|
||||||
}
|
}
|
||||||
@@ -313,7 +383,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
swift_task_enqueueGlobal_hook = { job, original in
|
swift_task_enqueueGlobal_hook = { job, original in
|
||||||
Context.driver?.enqueue(job)
|
Context.driver?.enqueue(job)
|
||||||
}
|
}
|
||||||
|
|
||||||
let runner = Task {
|
let runner = Task {
|
||||||
do {
|
do {
|
||||||
try await test.test(with: clock, activeTicks: activeTicks, output: test.output) { event in
|
try await test.test(with: clock, activeTicks: activeTicks, output: test.output) { event in
|
||||||
@@ -330,7 +400,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drain off any initial work. Work may spawn additional work to be done.
|
// Drain off any initial work. Work may spawn additional work to be done.
|
||||||
// If the driver ever becomes blocked on the clock, exit early out of that
|
// If the driver ever becomes blocked on the clock, exit early out of that
|
||||||
// drain, because the drain cant make any forward progress if it is blocked
|
// drain, because the drain cant make any forward progress if it is blocked
|
||||||
@@ -344,7 +414,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
}
|
}
|
||||||
diagram.queue.advance()
|
diagram.queue.advance()
|
||||||
}
|
}
|
||||||
|
|
||||||
runner.cancel()
|
runner.cancel()
|
||||||
Context.clock = nil
|
Context.clock = nil
|
||||||
swift_task_enqueueGlobal_hook = nil
|
swift_task_enqueueGlobal_hook = nil
|
||||||
@@ -354,15 +424,16 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
// else wise this would cause QoS inversions
|
// else wise this would cause QoS inversions
|
||||||
Context.driver?.join()
|
Context.driver?.join()
|
||||||
Context.driver = nil
|
Context.driver = nil
|
||||||
|
|
||||||
return validate(
|
return validate(
|
||||||
inputs: test.inputs,
|
inputs: test.inputs,
|
||||||
output: test.output,
|
output: test.output,
|
||||||
theme: theme,
|
theme: theme,
|
||||||
expected: expected,
|
expected: expected,
|
||||||
actual: actual.withCriticalRegion { $0 })
|
actual: actual.withCriticalRegion { $0 }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func test<Test: AsyncSequenceValidationTest>(
|
public static func test<Test: AsyncSequenceValidationTest>(
|
||||||
@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test
|
@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test
|
||||||
) throws -> (ExpectationResult, [ExpectationFailure]) {
|
) throws -> (ExpectationResult, [ExpectationFailure]) {
|
||||||
|
|||||||
@@ -9,18 +9,21 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
public protocol AsyncSequenceValidationTheme {
|
public protocol AsyncSequenceValidationTheme {
|
||||||
func token(_ character: Character, inValue: Bool) -> AsyncSequenceValidationDiagram.Token
|
func token(_ character: Character, inValue: Bool) -> AsyncSequenceValidationDiagram.Token
|
||||||
|
|
||||||
func description(for token: AsyncSequenceValidationDiagram.Token) -> String
|
func description(for token: AsyncSequenceValidationDiagram.Token) -> String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequenceValidationTheme where Self == AsyncSequenceValidationDiagram.ASCIITheme {
|
extension AsyncSequenceValidationTheme where Self == AsyncSequenceValidationDiagram.ASCIITheme {
|
||||||
public static var ascii: AsyncSequenceValidationDiagram.ASCIITheme {
|
public static var ascii: AsyncSequenceValidationDiagram.ASCIITheme {
|
||||||
return AsyncSequenceValidationDiagram.ASCIITheme()
|
return AsyncSequenceValidationDiagram.ASCIITheme()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(AsyncAlgorithms 1.0, *)
|
||||||
extension AsyncSequenceValidationDiagram {
|
extension AsyncSequenceValidationDiagram {
|
||||||
public enum Token: Sendable {
|
public enum Token: Sendable {
|
||||||
case step
|
case step
|
||||||
@@ -35,7 +38,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
case skip
|
case skip
|
||||||
case value(String)
|
case value(String)
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct ASCIITheme: AsyncSequenceValidationTheme, Sendable {
|
public struct ASCIITheme: AsyncSequenceValidationTheme, Sendable {
|
||||||
public func token(_ character: Character, inValue: Bool) -> AsyncSequenceValidationDiagram.Token {
|
public func token(_ character: Character, inValue: Bool) -> AsyncSequenceValidationDiagram.Token {
|
||||||
switch character {
|
switch character {
|
||||||
@@ -51,7 +54,7 @@ extension AsyncSequenceValidationDiagram {
|
|||||||
default: return .value(String(character))
|
default: return .value(String(character))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func description(for token: AsyncSequenceValidationDiagram.Token) -> String {
|
public func description(for token: AsyncSequenceValidationDiagram.Token) -> String {
|
||||||
switch token {
|
switch token {
|
||||||
case .step: return "-"
|
case .step: return "-"
|
||||||
|
|||||||
Loaded 100 of 139 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user