mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-22 04:10:40 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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,5 @@
|
||||
version: 1
|
||||
builder:
|
||||
configs:
|
||||
- documentation_targets: [AsyncAlgorithms]
|
||||
swift_version: 5.8
|
||||
@@ -0,0 +1,25 @@
|
||||
# file options
|
||||
|
||||
--swiftversion 5.7
|
||||
--exclude .build
|
||||
|
||||
# format options
|
||||
|
||||
--self insert
|
||||
--patternlet inline
|
||||
--ranges nospace
|
||||
--stripunusedargs unnamed-only
|
||||
--ifdef no-indent
|
||||
--extensionacl on-declarations
|
||||
--disable typeSugar # https://github.com/nicklockwood/SwiftFormat/issues/636
|
||||
--disable andOperator
|
||||
--disable wrapMultilineStatementBraces
|
||||
--disable enumNamespaces
|
||||
--disable redundantExtensionACL
|
||||
--disable redundantReturn
|
||||
--disable preferKeyPath
|
||||
--disable sortedSwitchCases
|
||||
--disable hoistAwait
|
||||
--disable hoistTry
|
||||
|
||||
# rules
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
* Proposal: [SAA-0001](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0001-zip.md)
|
||||
* 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)]
|
||||
* Decision Notes:
|
||||
* Bugs:
|
||||
@@ -45,8 +45,7 @@ public func zip<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncSequence
|
||||
public struct AsyncZip2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Sendable
|
||||
where
|
||||
Base1: Sendable, Base2: Sendable,
|
||||
Base1.Element: Sendable, Base2.Element: Sendable,
|
||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable {
|
||||
Base1.Element: Sendable, Base2.Element: Sendable {
|
||||
public typealias Element = (Base1.Element, Base2.Element)
|
||||
|
||||
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
|
||||
where
|
||||
Base1: Sendable, Base2: Sendable, Base3: Sendable
|
||||
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable
|
||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable, Base3.AsyncIterator: Sendable {
|
||||
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable {
|
||||
public typealias Element = (Base1.Element, Base2.Element, Base3.Element)
|
||||
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
* Proposal: [SAA-0002](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0002-merge.md)
|
||||
* 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)]
|
||||
* Decision Notes:
|
||||
* Bugs:
|
||||
@@ -46,8 +46,7 @@ public struct AsyncMerge2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: S
|
||||
where
|
||||
Base1.Element == Base2.Element,
|
||||
Base1: Sendable, Base2: Sendable,
|
||||
Base1.Element: Sendable, Base2.Element: Sendable,
|
||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable {
|
||||
Base1.Element: Sendable, Base2.Element: Sendable {
|
||||
public typealias Element = Base1.Element
|
||||
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
@@ -61,8 +60,7 @@ public struct AsyncMerge3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Ba
|
||||
where
|
||||
Base1.Element == Base2.Element, Base1.Element == Base3.Element,
|
||||
Base1: Sendable, Base2: Sendable, Base3: Sendable
|
||||
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable
|
||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable, Base3.AsyncIterator: Sendable {
|
||||
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable {
|
||||
public typealias Element = Base1.Element
|
||||
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# 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)
|
||||
* Status: **Implemented**
|
||||
* Status: **Accepted**
|
||||
|
||||
* 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)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* Proposal: [SAA-0004](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0004-joined.md)
|
||||
* Authors: [Philippe Hausler](https://github.com/phausler)
|
||||
* 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) |
|
||||
[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)
|
||||
* Author(s): [László Teveli](https://github.com/tevelee)
|
||||
* 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) |
|
||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestAdjacentPairs.swift)]
|
||||
* Decision Notes:
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
* Proposal: [SAA-0006](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0006-combineLatest.md)
|
||||
* 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)]
|
||||
|
||||
* Decision Notes:
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
## 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
|
||||
|
||||
@@ -47,8 +47,7 @@ public func combineLatest<Base1: AsyncSequence, Base2: AsyncSequence, Base3: Asy
|
||||
public struct AsyncCombineLatest2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Sendable
|
||||
where
|
||||
Base1: Sendable, Base2: Sendable,
|
||||
Base1.Element: Sendable, Base2.Element: Sendable,
|
||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable {
|
||||
Base1.Element: Sendable, Base2.Element: Sendable {
|
||||
public typealias Element = (Base1.Element, Base2.Element)
|
||||
|
||||
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
|
||||
where
|
||||
Base1: Sendable, Base2: Sendable, Base3: Sendable
|
||||
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable
|
||||
Base1.AsyncIterator: Sendable, Base2.AsyncIterator: Sendable, Base3.AsyncIterator: Sendable {
|
||||
Base1.Element: Sendable, Base2.Element: Sendable, Base3.Element: Sendable {
|
||||
public typealias Element = (Base1.Element, Base2.Element, Base3.Element)
|
||||
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
* Proposal: [SAA-0007](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0007-chain.md)
|
||||
* 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) |
|
||||
[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)
|
||||
* 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) |
|
||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestBufferedByteIterator.swift)]
|
||||
|
||||
@@ -33,7 +33,7 @@ struct AsyncBytes: AsyncSequence {
|
||||
## Detailed Design
|
||||
|
||||
```swift
|
||||
public struct AsyncBufferedByteIterator: AsyncIteratorProtocol, Sendable {
|
||||
public struct AsyncBufferedByteIterator: AsyncIteratorProtocol {
|
||||
public typealias Element = UInt8
|
||||
|
||||
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)
|
||||
* Status: **Implemented**
|
||||
|
||||
* 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)
|
||||
|
||||
## 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.
|
||||
|
||||
@@ -22,19 +22,19 @@ let characters = "abcde".async
|
||||
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.
|
||||
|
||||
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
|
||||
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 AsyncLazySequence.Iterator: Sendable where Base.Iterator: Sendable { }
|
||||
extension AsyncSyncSequence: Sendable where Base: Sendable { }
|
||||
extension AsyncSyncSequence.Iterator: Sendable where Base.Iterator: Sendable { }
|
||||
```
|
||||
|
||||
### Naming
|
||||
@@ -45,4 +45,4 @@ succinct name in inspiration from `.lazy`, and the type is named in reference to
|
||||
|
||||
## 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
|
||||
|
||||
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
|
||||
|
||||
|
||||
+34
-24
@@ -1,9 +1,9 @@
|
||||
// swift-tools-version: 5.6
|
||||
// swift-tools-version: 5.8
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "AsyncAlgorithms",
|
||||
name: "swift-async-algorithms",
|
||||
platforms: [
|
||||
.macOS("10.15"),
|
||||
.iOS("13.0"),
|
||||
@@ -12,40 +12,50 @@ let package = Package(
|
||||
],
|
||||
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", .upToNextMajor(from: "1.0.3"))],
|
||||
targets: [
|
||||
.target(
|
||||
name: "AsyncAlgorithms",
|
||||
dependencies: [.product(name: "Collections", package: "swift-collections")]
|
||||
dependencies: [
|
||||
.product(name: "OrderedCollections", package: "swift-collections"),
|
||||
.product(name: "DequeModule", package: "swift-collections"),
|
||||
],
|
||||
swiftSettings: [
|
||||
.enableExperimentalFeature("StrictConcurrency=complete"),
|
||||
]
|
||||
),
|
||||
.target(
|
||||
name: "AsyncSequenceValidation",
|
||||
dependencies: ["_CAsyncSequenceValidationSupport", "AsyncAlgorithms"]),
|
||||
dependencies: ["_CAsyncSequenceValidationSupport", "AsyncAlgorithms"],
|
||||
swiftSettings: [
|
||||
.enableExperimentalFeature("StrictConcurrency=complete"),
|
||||
]
|
||||
),
|
||||
.systemLibrary(name: "_CAsyncSequenceValidationSupport"),
|
||||
.target(
|
||||
name: "AsyncAlgorithms_XCTest",
|
||||
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation"]),
|
||||
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation"],
|
||||
swiftSettings: [
|
||||
.enableExperimentalFeature("StrictConcurrency=complete"),
|
||||
]
|
||||
),
|
||||
.testTarget(
|
||||
name: "AsyncAlgorithmsTests",
|
||||
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation", "AsyncAlgorithms_XCTest"]),
|
||||
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation", "AsyncAlgorithms_XCTest"],
|
||||
swiftSettings: [
|
||||
.enableExperimentalFeature("StrictConcurrency=complete"),
|
||||
]
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
#if canImport(Darwin)
|
||||
import Darwin
|
||||
let buildingDocs = getenv("BUILDING_FOR_DOCUMENTATION_GENERATION") != nil
|
||||
#elseif canImport(Glibc)
|
||||
import Glibc
|
||||
let buildingDocs = getenv("BUILDING_FOR_DOCUMENTATION_GENERATION") != nil
|
||||
#else
|
||||
let buildingDocs = false
|
||||
#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"),
|
||||
] : []
|
||||
if Context.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
|
||||
package.dependencies += [
|
||||
.package(url: "https://github.com/apple/swift-collections.git", from: "1.1.0"),
|
||||
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
|
||||
]
|
||||
} else {
|
||||
package.dependencies += [
|
||||
.package(path: "../swift-collections"),
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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:
|
||||
|
||||
```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:
|
||||
|
||||
@@ -9,6 +9,29 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
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`.
|
||||
@inlinable
|
||||
public func adjacentPairs() -> AsyncAdjacentPairsSequence<Self> {
|
||||
AsyncAdjacentPairsSequence(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// An `AsyncSequence` that iterates over the adjacent pairs of the original
|
||||
/// `AsyncSequence`.
|
||||
@frozen
|
||||
@@ -60,29 +83,6 @@ public struct AsyncAdjacentPairsSequence<Base: AsyncSequence>: AsyncSequence {
|
||||
}
|
||||
}
|
||||
|
||||
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`.
|
||||
@inlinable
|
||||
public func adjacentPairs() -> AsyncAdjacentPairsSequence<Self> {
|
||||
AsyncAdjacentPairsSequence(self)
|
||||
}
|
||||
}
|
||||
|
||||
extension AsyncAdjacentPairsSequence: Sendable where Base: Sendable, Base.Element: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
* 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) |
|
||||
[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/Channels/AsyncThrowingChannel.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.
|
||||
|
||||
```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 {
|
||||
write(packet)
|
||||
}
|
||||
|
||||
@@ -34,8 +34,8 @@
|
||||
| `AsyncInterspersedSequence.Iterator` | rethrows | Not Sendable|
|
||||
| `AsyncJoinedSequence` | rethrows | Conditional |
|
||||
| `AsyncJoinedSequence.Iterator` | rethrows | Not Sendable|
|
||||
| `AsyncLazySequence` | non-throwing | Conditional |
|
||||
| `AsyncLazySequence.Iterator` | non-throwing | Not Sendable|
|
||||
| `AsyncSyncSequence` | non-throwing | Conditional |
|
||||
| `AsyncSyncSequence.Iterator` | non-throwing | Not Sendable|
|
||||
| `AsyncLimitBuffer` | non-throwing | Sendable |
|
||||
| `AsyncMerge2Sequence` | rethrows | Sendable |
|
||||
| `AsyncMerge2Sequence.Iterator` | rethrows | Not Sendable|
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# AsyncLazySequence
|
||||
# AsyncSyncSequence
|
||||
|
||||
[[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)]
|
||||
|
||||
Converts a non-asynchronous sequence into an asynchronous one.
|
||||
@@ -17,19 +17,19 @@ to combine with other `AsyncSequence` types to provide well known sources of dat
|
||||
|
||||
## 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
|
||||
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 AsyncLazySequence.Iterator: Sendable where Base.Iterator: Sendable { }
|
||||
extension AsyncSyncSequence: Sendable where Base: Sendable { }
|
||||
extension AsyncSyncSequence.Iterator: Sendable where Base.Iterator: Sendable { }
|
||||
```
|
||||
|
||||
### Naming
|
||||
|
||||
@@ -42,7 +42,7 @@ extension AsyncTimerSequence: 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
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -95,3 +95,6 @@ extension AsyncChain3Sequence: AsyncSequence {
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -117,4 +117,6 @@ public struct AsyncChunkedByGroupSequence<Base: AsyncSequence, Collected: RangeR
|
||||
}
|
||||
|
||||
extension AsyncChunkedByGroupSequence : Sendable where Base : Sendable, Base.Element : Sendable { }
|
||||
extension AsyncChunkedByGroupSequence.Iterator : Sendable where Base.AsyncIterator : Sendable, Base.Element : Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncChunkedByGroupSequence.Iterator: Sendable { }
|
||||
@@ -97,4 +97,6 @@ public struct AsyncChunkedOnProjectionSequence<Base: AsyncSequence, Subject: Equ
|
||||
}
|
||||
|
||||
extension AsyncChunkedOnProjectionSequence : Sendable where Base : Sendable, Base.Element : Sendable { }
|
||||
extension AsyncChunkedOnProjectionSequence.Iterator : Sendable where Base.AsyncIterator : Sendable, Base.Element : Sendable, Subject : Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncChunkedOnProjectionSequence.Iterator: Sendable { }
|
||||
@@ -70,7 +70,7 @@ public struct AsyncChunksOfCountOrSignalSequence<Base: AsyncSequence, Collected:
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
typealias EitherMappedBase = AsyncMapSequence<Base, Either>
|
||||
typealias EitherMappedSignal = AsyncMapSequence<Signal, Either>
|
||||
typealias ChainedBase = AsyncChain2Sequence<EitherMappedBase, AsyncLazySequence<[Either]>>
|
||||
typealias ChainedBase = AsyncChain2Sequence<EitherMappedBase, AsyncSyncSequence<[Either]>>
|
||||
typealias Merged = AsyncMerge2Sequence<ChainedBase, EitherMappedSignal>
|
||||
|
||||
let count: Int?
|
||||
@@ -128,3 +128,6 @@ public struct AsyncChunksOfCountOrSignalSequence<Base: AsyncSequence, Collected:
|
||||
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 { }
|
||||
@@ -49,6 +49,10 @@ public struct AsyncChunksOfCountSequence<Base: AsyncSequence, Collected: RangeRe
|
||||
return nil
|
||||
}
|
||||
|
||||
if count == 1 {
|
||||
return Collected(CollectionOfOne(first))
|
||||
}
|
||||
|
||||
var result: Collected = .init()
|
||||
result.append(first)
|
||||
|
||||
@@ -83,3 +87,6 @@ public struct AsyncChunksOfCountSequence<Base: AsyncSequence, Collected: RangeRe
|
||||
|
||||
extension AsyncChunksOfCountSequence : Sendable where Base : Sendable, Base.Element : Sendable { }
|
||||
extension AsyncChunksOfCountSequence.Iterator : Sendable where Base.AsyncIterator : Sendable, Base.Element : Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncChunksOfCountSequence.Iterator: Sendable { }
|
||||
@@ -9,6 +9,22 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
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`.
|
||||
@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
|
||||
/// `AsyncSequence`.
|
||||
@frozen
|
||||
@@ -50,22 +66,7 @@ public struct AsyncCompactedSequence<Base: AsyncSequence, Element>: AsyncSequenc
|
||||
}
|
||||
}
|
||||
|
||||
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`.
|
||||
///
|
||||
/// 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 { }
|
||||
@@ -112,3 +112,6 @@ extension AsyncExclusiveReductionsSequence: AsyncSequence {
|
||||
}
|
||||
|
||||
extension AsyncExclusiveReductionsSequence: Sendable where Base: Sendable, Element: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncExclusiveReductionsSequence.Iterator: Sendable { }
|
||||
@@ -58,7 +58,7 @@ extension AsyncInclusiveReductionsSequence: AsyncSequence {
|
||||
internal let transform: @Sendable (Base.Element, Base.Element) async -> Base.Element
|
||||
|
||||
@inlinable
|
||||
internal init(
|
||||
init(
|
||||
_ iterator: Base.AsyncIterator,
|
||||
transform: @Sendable @escaping (Base.Element, Base.Element) async -> Base.Element
|
||||
) {
|
||||
@@ -85,3 +85,6 @@ extension AsyncInclusiveReductionsSequence: AsyncSequence {
|
||||
}
|
||||
|
||||
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 { }
|
||||
@@ -142,3 +142,6 @@ public struct AsyncJoinedBySeparatorSequence<Base: AsyncSequence, Separator: Asy
|
||||
|
||||
extension AsyncJoinedBySeparatorSequence: Sendable
|
||||
where Base: Sendable, Base.Element: Sendable, Base.Element.Element: Sendable, Separator: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncJoinedBySeparatorSequence.Iterator: Sendable { }
|
||||
@@ -92,3 +92,6 @@ public struct AsyncJoinedSequence<Base: AsyncSequence>: AsyncSequence where Base
|
||||
|
||||
extension AsyncJoinedSequence: Sendable
|
||||
where Base: Sendable, Base.Element: Sendable, Base.Element.Element: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncJoinedSequence.Iterator: Sendable { }
|
||||
@@ -143,3 +143,9 @@ public struct AsyncThrowingRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncS
|
||||
|
||||
extension AsyncRemoveDuplicatesSequence: Sendable where Base: Sendable, Base.Element: Sendable { }
|
||||
extension AsyncThrowingRemoveDuplicatesSequence: Sendable where Base: Sendable, Base.Element: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncRemoveDuplicatesSequence.Iterator: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncThrowingRemoveDuplicatesSequence.Iterator: Sendable { }
|
||||
+7
-4
@@ -14,8 +14,8 @@ extension Sequence {
|
||||
/// but on which operations, such as `map` and `filter`, are
|
||||
/// implemented asynchronously.
|
||||
@inlinable
|
||||
public var async: AsyncLazySequence<Self> {
|
||||
AsyncLazySequence(self)
|
||||
public var async: AsyncSyncSequence<Self> {
|
||||
AsyncSyncSequence(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ extension Sequence {
|
||||
/// This functions similarly to `LazySequence` by accessing elements sequentially
|
||||
/// in the iterator's `next()` method.
|
||||
@frozen
|
||||
public struct AsyncLazySequence<Base: Sequence>: AsyncSequence {
|
||||
public struct AsyncSyncSequence<Base: Sequence>: AsyncSequence {
|
||||
public typealias Element = Base.Element
|
||||
|
||||
@frozen
|
||||
@@ -66,4 +66,7 @@ public struct AsyncLazySequence<Base: Sequence>: AsyncSequence {
|
||||
}
|
||||
}
|
||||
|
||||
extension AsyncLazySequence: Sendable where Base: Sendable { }
|
||||
extension AsyncSyncSequence: Sendable where Base: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncSyncSequence.Iterator: Sendable { }
|
||||
@@ -12,20 +12,20 @@
|
||||
extension AsyncSequence {
|
||||
/// 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, *)
|
||||
public func throttle<C: Clock, Reduced>(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)
|
||||
public func _throttle<C: Clock, Reduced>(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.
|
||||
@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> {
|
||||
throttle(for: interval, clock: .continuous, reducing: reducing)
|
||||
public func _throttle<Reduced>(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.
|
||||
@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> {
|
||||
throttle(for: interval, clock: clock) { previous, element in
|
||||
public func _throttle<C: Clock>(for interval: C.Instant.Duration, clock: C, latest: Bool = true) -> _AsyncThrottleSequence<Self, C, Element> {
|
||||
_throttle(for: interval, clock: clock) { previous, element in
|
||||
if latest {
|
||||
return element
|
||||
} else {
|
||||
@@ -36,14 +36,14 @@ extension AsyncSequence {
|
||||
|
||||
/// 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, *)
|
||||
public func throttle(for interval: Duration, latest: Bool = true) -> AsyncThrottleSequence<Self, ContinuousClock, Element> {
|
||||
throttle(for: interval, clock: .continuous, latest: latest)
|
||||
public func _throttle(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.
|
||||
@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 interval: C.Instant.Duration
|
||||
let clock: C
|
||||
@@ -58,7 +58,7 @@ public struct AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
|
||||
}
|
||||
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
extension AsyncThrottleSequence: AsyncSequence {
|
||||
extension _AsyncThrottleSequence: AsyncSequence {
|
||||
public typealias Element = Reduced
|
||||
|
||||
/// The iterator for an `AsyncThrottleSequence` instance.
|
||||
@@ -81,7 +81,16 @@ extension AsyncThrottleSequence: AsyncSequence {
|
||||
let start = last ?? clock.now
|
||||
repeat {
|
||||
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 now = clock.now
|
||||
@@ -101,4 +110,7 @@ extension AsyncThrottleSequence: AsyncSequence {
|
||||
}
|
||||
|
||||
@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)
|
||||
}
|
||||
}
|
||||
@@ -119,3 +119,6 @@ extension AsyncThrowingExclusiveReductionsSequence: AsyncSequence {
|
||||
}
|
||||
|
||||
extension AsyncThrowingExclusiveReductionsSequence: Sendable where Base: Sendable, Element: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncThrowingExclusiveReductionsSequence.Iterator: Sendable { }
|
||||
@@ -1,3 +1,14 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 the accumulated results of combining the
|
||||
/// elements of the asynchronous sequence using the given error-throwing closure.
|
||||
@@ -57,7 +68,7 @@ extension AsyncThrowingInclusiveReductionsSequence: AsyncSequence {
|
||||
internal let transform: @Sendable (Base.Element, Base.Element) async throws -> Base.Element
|
||||
|
||||
@inlinable
|
||||
internal init(
|
||||
init(
|
||||
_ iterator: Base.AsyncIterator,
|
||||
transform: @Sendable @escaping (Base.Element, Base.Element) async throws -> Base.Element
|
||||
) {
|
||||
@@ -89,3 +100,6 @@ extension AsyncThrowingInclusiveReductionsSequence: AsyncSequence {
|
||||
}
|
||||
|
||||
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, *)
|
||||
public struct AsyncTimerSequence<C: Clock>: AsyncSequence {
|
||||
public typealias Element = C.Instant
|
||||
|
||||
|
||||
/// The iterator for an `AsyncTimerSequence` instance.
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
var clock: C?
|
||||
let interval: C.Instant.Duration
|
||||
let tolerance: C.Instant.Duration?
|
||||
var last: C.Instant?
|
||||
|
||||
|
||||
init(interval: C.Instant.Duration, tolerance: C.Instant.Duration?, clock: C) {
|
||||
self.clock = clock
|
||||
self.interval = interval
|
||||
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? {
|
||||
guard let clock = clock else {
|
||||
guard let clock = self.clock else {
|
||||
return nil
|
||||
}
|
||||
let next = nextDeadline(clock)
|
||||
|
||||
let next = (self.last ?? clock.now).advanced(by: self.interval)
|
||||
do {
|
||||
try await clock.sleep(until: next, tolerance: tolerance)
|
||||
try await clock.sleep(until: next, tolerance: self.tolerance)
|
||||
} catch {
|
||||
self.clock = nil
|
||||
return nil
|
||||
}
|
||||
let now = clock.now
|
||||
last = next
|
||||
self.last = next
|
||||
return now
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let clock: C
|
||||
let interval: C.Instant.Duration
|
||||
let tolerance: C.Instant.Duration?
|
||||
|
||||
|
||||
/// Create an `AsyncTimerSequence` with a given repeating interval.
|
||||
public init(interval: C.Instant.Duration, tolerance: C.Instant.Duration? = nil, clock: C) {
|
||||
self.clock = clock
|
||||
self.interval = interval
|
||||
self.tolerance = tolerance
|
||||
}
|
||||
|
||||
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
Iterator(interval: interval, tolerance: tolerance, clock: clock)
|
||||
}
|
||||
@@ -89,3 +79,6 @@ extension AsyncTimerSequence where C == SuspendingClock {
|
||||
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
extension AsyncTimerSequence: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncTimerSequence.Iterator: Sendable { }
|
||||
@@ -0,0 +1,131 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 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.
|
||||
public func buffer(
|
||||
policy: AsyncBufferSequencePolicy
|
||||
) -> AsyncBufferSequence<Self> {
|
||||
AsyncBufferSequence<Self>(base: self, policy: policy)
|
||||
}
|
||||
}
|
||||
|
||||
/// A policy dictating the buffering behaviour of an ``AsyncBufferSequence``
|
||||
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.
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension AsyncBufferSequence: Sendable where Base: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncBufferSequence.Iterator: Sendable { }
|
||||
@@ -0,0 +1,316 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
|
||||
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
|
||||
if buffer.count < limit {
|
||||
return .resumeProducer
|
||||
} else {
|
||||
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: continuation, suspendedConsumer: nil)
|
||||
return .none
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BoundedBufferStateMachine: Sendable where Base: Sendable { }
|
||||
extension BoundedBufferStateMachine.State: Sendable where Base: Sendable { }
|
||||
@@ -0,0 +1,155 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
final class 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,258 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension UnboundedBufferStateMachine: Sendable where Base: Sendable { }
|
||||
extension UnboundedBufferStateMachine.State: Sendable where Base: Sendable { }
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
final class 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,63 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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.
|
||||
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,66 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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.
|
||||
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,344 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if suspendedConsumers.isEmpty {
|
||||
// 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
|
||||
} 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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if suspendedProducers.isEmpty {
|
||||
// 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
|
||||
} 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
|
||||
)
|
||||
}
|
||||
|
||||
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,142 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
struct 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public struct AsyncCombineLatest2Sequence<
|
||||
let base1: Base1
|
||||
let base2: Base2
|
||||
|
||||
public init(_ base1: Base1, _ base2: Base2) {
|
||||
init(_ base1: Base1, _ base2: Base2) {
|
||||
self.base1 = base1
|
||||
self.base2 = base2
|
||||
}
|
||||
@@ -87,3 +87,6 @@ public struct AsyncCombineLatest2Sequence<
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncCombineLatest2Sequence.Iterator: Sendable { }
|
||||
@@ -97,3 +97,6 @@ public struct AsyncCombineLatest3Sequence<
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncCombineLatest3Sequence.Iterator: Sendable { }
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import DequeModule
|
||||
import DequeModule
|
||||
|
||||
/// State machine for combine latest
|
||||
struct CombineLatestStateMachine<
|
||||
@@ -530,7 +530,8 @@ struct CombineLatestStateMachine<
|
||||
preconditionFailure("Internal inconsistency current state \(self.state) and received upstreamThrew()")
|
||||
|
||||
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, _):
|
||||
// An upstream threw. We can cancel everything now and transition to finished.
|
||||
@@ -585,7 +586,9 @@ struct CombineLatestStateMachine<
|
||||
mutating func cancelled() -> CancelledAction? {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")
|
||||
state = .finished
|
||||
|
||||
return .none
|
||||
|
||||
case .waitingForDemand(let task, let upstreams, _):
|
||||
// The downstream task got cancelled so we need to cancel our upstream Task
|
||||
|
||||
@@ -48,7 +48,7 @@ final class CombineLatestStorage<
|
||||
func next() async rethrows -> (Base1.Element, Base2.Element, Base3.Element?)? {
|
||||
try await withTaskCancellationHandler {
|
||||
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)
|
||||
switch action {
|
||||
case .startTask(let base1, let base2, let base3):
|
||||
@@ -58,47 +58,67 @@ final class CombineLatestStorage<
|
||||
base1: base1,
|
||||
base2: base2,
|
||||
base3: base3,
|
||||
downStreamContinuation: continuation
|
||||
downstreamContinuation: continuation
|
||||
)
|
||||
return nil
|
||||
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
case .resumeContinuation:
|
||||
return action
|
||||
|
||||
case .resumeUpstreamContinuations(let upstreamContinuations):
|
||||
// bases can be iterated over for 1 iteration so their next value can be retrieved
|
||||
upstreamContinuations.forEach { $0.resume() }
|
||||
case .resumeUpstreamContinuations:
|
||||
return action
|
||||
|
||||
case .resumeDownstreamContinuationWithNil(let continuation):
|
||||
// the async sequence is already finished, immediately resuming
|
||||
continuation.resume(returning: .success(nil))
|
||||
case .resumeDownstreamContinuationWithNil:
|
||||
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 .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()
|
||||
|
||||
} onCancel: {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.cancelled()
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.cancelled()
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
switch action {
|
||||
case .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
|
||||
downstreamContinuation.resume(returning: .success(nil))
|
||||
downstreamContinuation.resume(returning: .success(nil))
|
||||
|
||||
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,7 +128,7 @@ final class CombineLatestStorage<
|
||||
base1: Base1,
|
||||
base2: Base2,
|
||||
base3: Base3?,
|
||||
downStreamContinuation: StateMachine.DownstreamContinuation
|
||||
downstreamContinuation: StateMachine.DownstreamContinuation
|
||||
) {
|
||||
// This creates a new `Task` that is iterating the upstream
|
||||
// sequences. We must store it to cancel it at the right times.
|
||||
@@ -124,33 +144,33 @@ final class CombineLatestStorage<
|
||||
// element from upstream. This continuation is only resumed
|
||||
// if the downstream consumer called `next` to signal his demand.
|
||||
try await withUnsafeThrowingContinuation { continuation in
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.childTaskSuspended(baseIndex: 0, continuation: continuation)
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.childTaskSuspended(baseIndex: 0, continuation: continuation)
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if let element1 = try await base1Iterator.next() {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.elementProduced((element1, nil, nil))
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.elementProduced((element1, nil, nil))
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
} else {
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
@@ -191,33 +211,33 @@ final class CombineLatestStorage<
|
||||
// element from upstream. This continuation is only resumed
|
||||
// if the downstream consumer called `next` to signal his demand.
|
||||
try await withUnsafeThrowingContinuation { continuation in
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.childTaskSuspended(baseIndex: 1, continuation: continuation)
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.childTaskSuspended(baseIndex: 1, continuation: continuation)
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if let element2 = try await base1Iterator.next() {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.elementProduced((nil, element2, nil))
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.elementProduced((nil, element2, nil))
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
} else {
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
@@ -259,33 +279,33 @@ final class CombineLatestStorage<
|
||||
// element from upstream. This continuation is only resumed
|
||||
// if the downstream consumer called `next` to signal his demand.
|
||||
try await withUnsafeThrowingContinuation { continuation in
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.childTaskSuspended(baseIndex: 2, continuation: continuation)
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.childTaskSuspended(baseIndex: 2, continuation: continuation)
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if let element3 = try await base1Iterator.next() {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.elementProduced((nil, nil, element3))
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.elementProduced((nil, nil, element3))
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
} else {
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
@@ -319,39 +339,38 @@ final class CombineLatestStorage<
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
try await group.waitForAll()
|
||||
} catch {
|
||||
// One of the upstream sequences threw an error
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.upstreamThrew(error)
|
||||
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 .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
switch action {
|
||||
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let error,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
downstreamContinuation.resume(returning: .failure(error))
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
|
||||
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let error,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
|
||||
downstreamContinuation.resume(returning: .failure(error))
|
||||
|
||||
case .none:
|
||||
break
|
||||
group.cancelAll()
|
||||
}
|
||||
}
|
||||
|
||||
group.cancelAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stateMachine.taskIsStarted(task: task, downstreamContinuation: downStreamContinuation)
|
||||
stateMachine.taskIsStarted(task: task, downstreamContinuation: downstreamContinuation)
|
||||
}
|
||||
}
|
||||
@@ -13,14 +13,14 @@ extension AsyncSequence {
|
||||
/// Creates an asynchronous sequence that emits the latest element after a given quiescence period
|
||||
/// has elapsed by using a specified Clock.
|
||||
@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>(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
|
||||
/// has elapsed.
|
||||
@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(for interval: Duration, tolerance: Duration? = nil) -> AsyncDebounceSequence<Self, ContinuousClock> where Self: Sendable, Self.Element: Sendable {
|
||||
self.debounce(for: interval, tolerance: tolerance, clock: .continuous)
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ extension AsyncSequence {
|
||||
/// An `AsyncSequence` that emits the latest element after a given quiescence period
|
||||
/// has elapsed.
|
||||
@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 clock: C
|
||||
private let interval: C.Instant.Duration
|
||||
@@ -41,7 +41,7 @@ public struct AsyncDebounceSequence<Base: AsyncSequence, C: Clock>: Sendable whe
|
||||
/// - interval: The interval to debounce.
|
||||
/// - tolerance: The tolerance of 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.interval = interval
|
||||
self.tolerance = tolerance
|
||||
@@ -53,20 +53,20 @@ public struct AsyncDebounceSequence<Base: AsyncSequence, C: Clock>: Sendable whe
|
||||
extension AsyncDebounceSequence: AsyncSequence {
|
||||
public typealias Element = Base.Element
|
||||
|
||||
public func makeAsyncIterator() -> AsyncIterator {
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
let storage = DebounceStorage(
|
||||
base: self.base,
|
||||
interval: self.interval,
|
||||
tolerance: self.tolerance,
|
||||
clock: self.clock
|
||||
)
|
||||
return AsyncIterator(storage: storage)
|
||||
return Iterator(storage: storage)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
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.
|
||||
///
|
||||
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
|
||||
@@ -97,3 +97,6 @@ extension AsyncDebounceSequence {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncDebounceSequence.Iterator: Sendable { }
|
||||
@@ -10,10 +10,10 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
|
||||
struct DebounceStateMachine<Base: AsyncSequence & Sendable, C: Clock>: Sendable where Base.Element: Sendable {
|
||||
typealias Element = Base.Element
|
||||
|
||||
private enum State {
|
||||
private enum State: Sendable {
|
||||
/// The initial state before a call to `next` happened.
|
||||
case initial(base: Base)
|
||||
|
||||
@@ -390,8 +390,8 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
|
||||
preconditionFailure("Internal inconsistency current state \(self.state) and received upstreamFinished()")
|
||||
|
||||
case .upstreamFailure:
|
||||
// The upstream already failed so it should never have throw again.
|
||||
preconditionFailure("Internal inconsistency current state \(self.state) and received childTaskSuspended()")
|
||||
// We need to tolerate multiple upstreams failing
|
||||
return .none
|
||||
|
||||
case .waitingForDemand(let task, .none, let clockContinuation, .none):
|
||||
// We don't have any buffered element so we can just go ahead
|
||||
@@ -517,8 +517,8 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
|
||||
/// Actions returned by `clockSleepFinished()`.
|
||||
enum ClockSleepFinishedAction {
|
||||
/// Indicates that the downstream continuation should be resumed with the given element.
|
||||
case resumeDownStreamContinuation(
|
||||
downStreamContinuation: UnsafeContinuation<Result<Element?, Error>, Never>,
|
||||
case resumeDownstreamContinuation(
|
||||
downstreamContinuation: UnsafeContinuation<Result<Element?, Error>, Never>,
|
||||
element: Element
|
||||
)
|
||||
}
|
||||
@@ -547,8 +547,8 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
|
||||
bufferedElement: nil
|
||||
)
|
||||
|
||||
return .resumeDownStreamContinuation(
|
||||
downStreamContinuation: downstreamContinuation,
|
||||
return .resumeDownstreamContinuation(
|
||||
downstreamContinuation: downstreamContinuation,
|
||||
element: currentElement.element
|
||||
)
|
||||
} else {
|
||||
@@ -583,9 +583,8 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
|
||||
mutating func cancelled() -> CancelledAction? {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
// Since we are transitioning to `merging` before we return from `makeAsyncIterator`
|
||||
// this can never happen
|
||||
preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")
|
||||
state = .finished
|
||||
return .none
|
||||
|
||||
case .waitingForDemand:
|
||||
// We got cancelled before we event got any demand. This can happen if a cancelled task
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@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
|
||||
|
||||
/// The state machine protected with a lock.
|
||||
@@ -55,36 +55,59 @@ final class DebounceStorage<Base: AsyncSequence, C: Clock>: @unchecked Sendable
|
||||
// We always suspend since we can never return an element right away
|
||||
|
||||
let result: Result<Element?, Error> = await withUnsafeContinuation { continuation in
|
||||
self.stateMachine.withCriticalRegion {
|
||||
let action = $0.next(for: continuation)
|
||||
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
|
||||
)
|
||||
switch action {
|
||||
case .startTask(let base):
|
||||
self.startTask(
|
||||
stateMachine: &$0,
|
||||
base: base,
|
||||
downstreamContinuation: continuation
|
||||
)
|
||||
return nil
|
||||
|
||||
case .resumeUpstreamContinuation(let upstreamContinuation):
|
||||
// This is signalling the upstream task that is consuming the upstream
|
||||
// sequence to signal demand.
|
||||
upstreamContinuation?.resume(returning: ())
|
||||
case .resumeUpstreamContinuation:
|
||||
return action
|
||||
|
||||
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 .resumeUpstreamAndClockContinuation:
|
||||
return action
|
||||
|
||||
case .resumeDownstreamContinuationWithNil(let continuation):
|
||||
continuation.resume(returning: .success(nil))
|
||||
case .resumeDownstreamContinuationWithNil:
|
||||
return action
|
||||
|
||||
case .resumeDownstreamContinuationWithError(let continuation, let error):
|
||||
continuation.resume(returning: .failure(error))
|
||||
}
|
||||
case .resumeDownstreamContinuationWithError:
|
||||
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 .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:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return try result._rethrowGet()
|
||||
} onCancel: {
|
||||
@@ -238,8 +261,8 @@ final class DebounceStorage<Base: AsyncSequence, C: Clock>: @unchecked Sendable
|
||||
let action = self.stateMachine.withCriticalRegion { $0.clockSleepFinished() }
|
||||
|
||||
switch action {
|
||||
case .resumeDownStreamContinuation(let downStreamContinuation, let element):
|
||||
downStreamContinuation.resume(returning: .success(element))
|
||||
case .resumeDownstreamContinuation(let downstreamContinuation, let element):
|
||||
downstreamContinuation.resume(returning: .success(element))
|
||||
|
||||
case .none:
|
||||
break
|
||||
@@ -254,36 +277,39 @@ final class DebounceStorage<Base: AsyncSequence, C: Clock>: @unchecked Sendable
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
try await group.waitForAll()
|
||||
} catch {
|
||||
// The upstream sequence threw an error
|
||||
let action = self.stateMachine.withCriticalRegion { $0.upstreamThrew(error) }
|
||||
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())
|
||||
switch action {
|
||||
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuation(
|
||||
let downstreamContinuation,
|
||||
let error,
|
||||
let task,
|
||||
let upstreamContinuation,
|
||||
let clockContinuation
|
||||
):
|
||||
upstreamContinuation?.resume(throwing: CancellationError())
|
||||
clockContinuation?.resume(throwing: CancellationError())
|
||||
|
||||
task.cancel()
|
||||
task.cancel()
|
||||
|
||||
downstreamContinuation.resume(returning: .failure(error))
|
||||
downstreamContinuation.resume(returning: .failure(error))
|
||||
|
||||
case .cancelTaskAndClockContinuation(
|
||||
let task,
|
||||
let clockContinuation
|
||||
):
|
||||
clockContinuation?.resume(throwing: CancellationError())
|
||||
task.cancel()
|
||||
|
||||
case .none:
|
||||
break
|
||||
case .cancelTaskAndClockContinuation(
|
||||
let task,
|
||||
let clockContinuation
|
||||
):
|
||||
clockContinuation?.resume(throwing: CancellationError())
|
||||
task.cancel()
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
group.cancelAll()
|
||||
|
||||
@@ -0,0 +1,444 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 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
|
||||
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.
|
||||
@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.
|
||||
@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.
|
||||
@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)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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? {
|
||||
switch self.state {
|
||||
case .start(var element):
|
||||
do {
|
||||
if element == nil {
|
||||
element = try await self.iterator.next()
|
||||
}
|
||||
|
||||
if let element = element {
|
||||
if self.every == 1 {
|
||||
self.state = .separator
|
||||
} else {
|
||||
self.state = .element(1)
|
||||
}
|
||||
return element
|
||||
} else {
|
||||
self.state = .finished
|
||||
return nil
|
||||
}
|
||||
} catch {
|
||||
self.state = .finished
|
||||
throw error
|
||||
}
|
||||
|
||||
case .separator:
|
||||
do {
|
||||
if let element = try await iterator.next() {
|
||||
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()
|
||||
}
|
||||
} else {
|
||||
self.state = .finished
|
||||
return nil
|
||||
}
|
||||
} catch {
|
||||
self.state = .finished
|
||||
throw error
|
||||
}
|
||||
|
||||
case .element(let count):
|
||||
do {
|
||||
if let element = try await iterator.next() {
|
||||
let newCount = count + 1
|
||||
if self.every == newCount {
|
||||
self.state = .separator
|
||||
} else {
|
||||
self.state = .element(newCount)
|
||||
}
|
||||
return element
|
||||
} else {
|
||||
self.state = .finished
|
||||
return nil
|
||||
}
|
||||
} catch {
|
||||
self.state = .finished
|
||||
throw error
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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.
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
extension AsyncThrowingInterspersedSequence: 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 throws -> Base.Element? {
|
||||
switch self.state {
|
||||
case .start(var element):
|
||||
do {
|
||||
if element == nil {
|
||||
element = try await self.iterator.next()
|
||||
}
|
||||
|
||||
if let element = element {
|
||||
if self.every == 1 {
|
||||
self.state = .separator
|
||||
} else {
|
||||
self.state = .element(1)
|
||||
}
|
||||
return element
|
||||
} else {
|
||||
self.state = .finished
|
||||
return nil
|
||||
}
|
||||
} catch {
|
||||
self.state = .finished
|
||||
throw error
|
||||
}
|
||||
|
||||
case .separator:
|
||||
do {
|
||||
if let element = try await iterator.next() {
|
||||
self.state = .start(element)
|
||||
switch self.separator {
|
||||
case .syncClosure(let closure):
|
||||
return try closure()
|
||||
|
||||
case .asyncClosure(let closure):
|
||||
return try await closure()
|
||||
}
|
||||
} else {
|
||||
self.state = .finished
|
||||
return nil
|
||||
}
|
||||
} catch {
|
||||
self.state = .finished
|
||||
throw error
|
||||
}
|
||||
|
||||
case .element(let count):
|
||||
do {
|
||||
if let element = try await iterator.next() {
|
||||
let newCount = count + 1
|
||||
if self.every == newCount {
|
||||
self.state = .separator
|
||||
} else {
|
||||
self.state = .element(newCount)
|
||||
}
|
||||
return element
|
||||
} else {
|
||||
self.state = .finished
|
||||
return nil
|
||||
}
|
||||
} catch {
|
||||
self.state = .finished
|
||||
throw error
|
||||
}
|
||||
|
||||
case .finished:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
Iterator(self.base.makeAsyncIterator(), every: self.every, separator: self.separator)
|
||||
}
|
||||
}
|
||||
|
||||
extension AsyncInterspersedSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||
extension AsyncInterspersedSequence.Separator: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||
|
||||
extension AsyncThrowingInterspersedSequence: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||
extension AsyncThrowingInterspersedSequence.Separator: Sendable where Base: Sendable, Base.Element: Sendable {}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncInterspersedSequence.Iterator: Sendable {}
|
||||
@available(*, unavailable)
|
||||
extension AsyncThrowingInterspersedSequence.Iterator: Sendable {}
|
||||
@@ -10,20 +10,28 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(Darwin)
|
||||
@_implementationOnly import Darwin
|
||||
import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
@_implementationOnly import Glibc
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import Musl
|
||||
#elseif canImport(WinSDK)
|
||||
@_implementationOnly import WinSDK
|
||||
import WinSDK
|
||||
#elseif canImport(Bionic)
|
||||
import Bionic
|
||||
#else
|
||||
#error("Unsupported platform")
|
||||
#endif
|
||||
|
||||
internal struct Lock {
|
||||
#if canImport(Darwin)
|
||||
typealias Primitive = os_unfair_lock
|
||||
#elseif canImport(Glibc)
|
||||
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
typealias Primitive = pthread_mutex_t
|
||||
#elseif canImport(WinSDK)
|
||||
typealias Primitive = SRWLOCK
|
||||
#else
|
||||
#error("Unsupported platform")
|
||||
#endif
|
||||
|
||||
typealias PlatformLock = UnsafeMutablePointer<Primitive>
|
||||
@@ -36,16 +44,20 @@ internal struct Lock {
|
||||
fileprivate static func initialize(_ platformLock: PlatformLock) {
|
||||
#if canImport(Darwin)
|
||||
platformLock.initialize(to: os_unfair_lock())
|
||||
#elseif canImport(Glibc)
|
||||
pthread_mutex_init(platformLock, nil)
|
||||
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
let result = pthread_mutex_init(platformLock, nil)
|
||||
precondition(result == 0, "pthread_mutex_init failed")
|
||||
#elseif canImport(WinSDK)
|
||||
InitializeSRWLock(platformLock)
|
||||
#else
|
||||
#error("Unsupported platform")
|
||||
#endif
|
||||
}
|
||||
|
||||
fileprivate static func deinitialize(_ platformLock: PlatformLock) {
|
||||
#if canImport(Glibc)
|
||||
pthread_mutex_destroy(platformLock)
|
||||
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
let result = pthread_mutex_destroy(platformLock)
|
||||
precondition(result == 0, "pthread_mutex_destroy failed")
|
||||
#endif
|
||||
platformLock.deinitialize(count: 1)
|
||||
}
|
||||
@@ -53,20 +65,25 @@ internal struct Lock {
|
||||
fileprivate static func lock(_ platformLock: PlatformLock) {
|
||||
#if canImport(Darwin)
|
||||
os_unfair_lock_lock(platformLock)
|
||||
#elseif canImport(Glibc)
|
||||
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
pthread_mutex_lock(platformLock)
|
||||
#elseif canImport(WinSDK)
|
||||
AcquireSRWLockExclusive(platformLock)
|
||||
#else
|
||||
#error("Unsupported platform")
|
||||
#endif
|
||||
}
|
||||
|
||||
fileprivate static func unlock(_ platformLock: PlatformLock) {
|
||||
#if canImport(Darwin)
|
||||
os_unfair_lock_unlock(platformLock)
|
||||
#elseif canImport(Glibc)
|
||||
pthread_mutex_unlock(platformLock)
|
||||
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
let result = pthread_mutex_unlock(platformLock)
|
||||
precondition(result == 0, "pthread_mutex_unlock failed")
|
||||
#elseif canImport(WinSDK)
|
||||
ReleaseSRWLockExclusive(platformLock)
|
||||
#else
|
||||
#error("Unsupported platform")
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import DequeModule
|
||||
import DequeModule
|
||||
|
||||
/// 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>
|
||||
@@ -40,7 +40,7 @@ public struct AsyncMerge2Sequence<
|
||||
/// - Parameters:
|
||||
/// - base1: The first upstream ``Swift/AsyncSequence``.
|
||||
/// - base2: The second upstream ``Swift/AsyncSequence``.
|
||||
public init(
|
||||
init(
|
||||
_ base1: Base1,
|
||||
_ base2: Base2
|
||||
) {
|
||||
@@ -50,18 +50,18 @@ public struct AsyncMerge2Sequence<
|
||||
}
|
||||
|
||||
extension AsyncMerge2Sequence: AsyncSequence {
|
||||
public func makeAsyncIterator() -> AsyncIterator {
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
let storage = MergeStorage<Base1, Base2, Base1>(
|
||||
base1: base1,
|
||||
base2: base2,
|
||||
base3: nil
|
||||
)
|
||||
return AsyncIterator(storage: storage)
|
||||
return Iterator(storage: storage)
|
||||
}
|
||||
}
|
||||
|
||||
extension AsyncMerge2Sequence {
|
||||
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.
|
||||
///
|
||||
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
|
||||
@@ -92,3 +92,6 @@ extension AsyncMerge2Sequence {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncMerge2Sequence.Iterator: Sendable { }
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import DequeModule
|
||||
import DequeModule
|
||||
|
||||
/// Creates an asynchronous sequence of elements from two underlying asynchronous sequences
|
||||
public func merge<
|
||||
@@ -49,7 +49,7 @@ public struct AsyncMerge3Sequence<
|
||||
/// - base1: The first upstream ``Swift/AsyncSequence``.
|
||||
/// - base2: The second upstream ``Swift/AsyncSequence``.
|
||||
/// - base3: The third upstream ``Swift/AsyncSequence``.
|
||||
public init(
|
||||
init(
|
||||
_ base1: Base1,
|
||||
_ base2: Base2,
|
||||
_ base3: Base3
|
||||
@@ -61,18 +61,18 @@ public struct AsyncMerge3Sequence<
|
||||
}
|
||||
|
||||
extension AsyncMerge3Sequence: AsyncSequence {
|
||||
public func makeAsyncIterator() -> AsyncIterator {
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
let storage = MergeStorage(
|
||||
base1: base1,
|
||||
base2: base2,
|
||||
base3: base3
|
||||
)
|
||||
return AsyncIterator(storage: storage)
|
||||
return Iterator(storage: storage)
|
||||
}
|
||||
}
|
||||
|
||||
public extension AsyncMerge3Sequence {
|
||||
struct AsyncIterator: AsyncIteratorProtocol {
|
||||
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.
|
||||
@@ -103,3 +103,6 @@ public extension AsyncMerge3Sequence {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncMerge3Sequence.Iterator: Sendable { }
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import DequeModule
|
||||
import DequeModule
|
||||
|
||||
/// The state machine for any of the `merge` operator.
|
||||
///
|
||||
@@ -465,9 +465,11 @@ struct MergeStateMachine<
|
||||
mutating func cancelled() -> CancelledAction {
|
||||
switch state {
|
||||
case .initial:
|
||||
// Since we are transitioning to `merging` before we return from `makeAsyncIterator`
|
||||
// this can never happen
|
||||
preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")
|
||||
// Since we are only transitioning to merging when the task is started we
|
||||
// can be cancelled already.
|
||||
state = .finished
|
||||
|
||||
return .none
|
||||
|
||||
case let .merging(task, _, upstreamContinuations, _, .some(downstreamContinuation)):
|
||||
// The downstream Task got cancelled so we need to cancel our upstream Task
|
||||
|
||||
@@ -147,297 +147,46 @@ final class MergeStorage<
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.iterateAsyncSequence(base1, in: &group)
|
||||
self.iterateAsyncSequence(base2, in: &group)
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.iterateAsyncSequence(base3, in: &group)
|
||||
}
|
||||
|
||||
do {
|
||||
try await group.waitForAll()
|
||||
} catch {
|
||||
|
||||
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)
|
||||
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()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -445,5 +194,95 @@ final class MergeStorage<
|
||||
// We need to inform our state machine that we started the Task
|
||||
stateMachine.taskStarted(task)
|
||||
}
|
||||
}
|
||||
|
||||
private func iterateAsyncSequence<AsyncSequence: _Concurrency.AsyncSequence>(
|
||||
_ 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()
|
||||
|
||||
// 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 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,
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 { }
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -69,3 +69,6 @@ public struct AsyncZip2Sequence<Base1: AsyncSequence, Base2: AsyncSequence>: Asy
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncZip2Sequence.Iterator: Sendable { }
|
||||
@@ -74,3 +74,6 @@ public struct AsyncZip3Sequence<Base1: AsyncSequence, Base2: AsyncSequence, Base
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncZip3Sequence.Iterator: Sendable { }
|
||||
@@ -460,7 +460,9 @@ struct ZipStateMachine<
|
||||
mutating func cancelled() -> CancelledAction? {
|
||||
switch self.state {
|
||||
case .initial:
|
||||
preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")
|
||||
state = .finished
|
||||
|
||||
return .none
|
||||
|
||||
case .waitingForDemand(let task, let upstreams):
|
||||
// The downstream task got cancelled so we need to cancel our upstream Task
|
||||
|
||||
@@ -39,7 +39,7 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
||||
func next() async rethrows -> (Base1.Element, Base2.Element, Base3.Element?)? {
|
||||
try await withTaskCancellationHandler {
|
||||
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)
|
||||
switch action {
|
||||
case .startTask(let base1, let base2, let base3):
|
||||
@@ -49,44 +49,61 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
||||
base1: base1,
|
||||
base2: base2,
|
||||
base3: base3,
|
||||
downStreamContinuation: continuation
|
||||
downstreamContinuation: continuation
|
||||
)
|
||||
return nil
|
||||
|
||||
case .resumeUpstreamContinuations(let upstreamContinuations):
|
||||
// bases can be iterated over for 1 iteration so their next value can be retrieved
|
||||
upstreamContinuations.forEach { $0.resume() }
|
||||
case .resumeUpstreamContinuations:
|
||||
return action
|
||||
|
||||
case .resumeDownstreamContinuationWithNil(let continuation):
|
||||
// the async sequence is already finished, immediately resuming
|
||||
continuation.resume(returning: .success(nil))
|
||||
case .resumeDownstreamContinuationWithNil:
|
||||
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 .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()
|
||||
|
||||
} onCancel: {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.cancelled()
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.cancelled()
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
switch action {
|
||||
case .resumeDownstreamContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
|
||||
downstreamContinuation.resume(returning: .success(nil))
|
||||
downstreamContinuation.resume(returning: .success(nil))
|
||||
|
||||
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,7 +113,7 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
||||
base1: Base1,
|
||||
base2: Base2,
|
||||
base3: Base3?,
|
||||
downStreamContinuation: StateMachine.DownstreamContinuation
|
||||
downstreamContinuation: StateMachine.DownstreamContinuation
|
||||
) {
|
||||
// This creates a new `Task` that is iterating the upstream
|
||||
// sequences. We must store it to cancel it at the right times.
|
||||
@@ -112,113 +129,113 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
||||
// element from upstream. This continuation is only resumed
|
||||
// if the downstream consumer called `next` to signal his demand.
|
||||
try await withUnsafeThrowingContinuation { continuation in
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.childTaskSuspended(baseIndex: 0, continuation: continuation)
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.childTaskSuspended(baseIndex: 0, continuation: continuation)
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if let element1 = try await base1Iterator.next() {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.elementProduced((element1, nil, nil))
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.elementProduced((element1, nil, nil))
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
} else {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.upstreamFinished()
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.upstreamFinished()
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
switch action {
|
||||
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
|
||||
downstreamContinuation.resume(returning: .success(nil))
|
||||
downstreamContinuation.resume(returning: .success(nil))
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
var base1Iterator = base2.makeAsyncIterator()
|
||||
var base2Iterator = base2.makeAsyncIterator()
|
||||
|
||||
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
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.childTaskSuspended(baseIndex: 1, continuation: continuation)
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.childTaskSuspended(baseIndex: 1, continuation: continuation)
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if let element2 = try await base1Iterator.next() {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.elementProduced((nil, element2, nil))
|
||||
if let element2 = try await base2Iterator.next() {
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.elementProduced((nil, element2, nil))
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
} else {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.upstreamFinished()
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.upstreamFinished()
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
switch action {
|
||||
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
|
||||
downstreamContinuation.resume(returning: .success(nil))
|
||||
downstreamContinuation.resume(returning: .success(nil))
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -226,95 +243,95 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
|
||||
|
||||
if let base3 = base3 {
|
||||
group.addTask {
|
||||
var base1Iterator = base3.makeAsyncIterator()
|
||||
var base3Iterator = base3.makeAsyncIterator()
|
||||
|
||||
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
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.childTaskSuspended(baseIndex: 2, continuation: continuation)
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.childTaskSuspended(baseIndex: 2, continuation: continuation)
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
switch action {
|
||||
case .resumeContinuation(let upstreamContinuation):
|
||||
upstreamContinuation.resume()
|
||||
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
case .resumeContinuationWithError(let upstreamContinuation, let error):
|
||||
upstreamContinuation.resume(throwing: error)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if let element3 = try await base1Iterator.next() {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.elementProduced((nil, nil, element3))
|
||||
if let element3 = try await base3Iterator.next() {
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.elementProduced((nil, nil, element3))
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
switch action {
|
||||
case .resumeContinuation(let downstreamContinuation, let result):
|
||||
downstreamContinuation.resume(returning: result)
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
} else {
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.upstreamFinished()
|
||||
let action = self.stateMachine.withCriticalRegion { stateMachine in
|
||||
stateMachine.upstreamFinished()
|
||||
}
|
||||
|
||||
switch action {
|
||||
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
switch action {
|
||||
case .resumeContinuationWithNilAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
|
||||
downstreamContinuation.resume(returning: .success(nil))
|
||||
downstreamContinuation.resume(returning: .success(nil))
|
||||
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
try await group.waitForAll()
|
||||
} catch {
|
||||
// One of the upstream sequences threw an error
|
||||
self.stateMachine.withCriticalRegion { stateMachine in
|
||||
let action = stateMachine.upstreamThrew(error)
|
||||
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 .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let error,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
|
||||
switch action {
|
||||
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
|
||||
let downstreamContinuation,
|
||||
let error,
|
||||
let task,
|
||||
let upstreamContinuations
|
||||
):
|
||||
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
|
||||
task.cancel()
|
||||
downstreamContinuation.resume(returning: .failure(error))
|
||||
case .none:
|
||||
break
|
||||
}
|
||||
|
||||
downstreamContinuation.resume(returning: .failure(error))
|
||||
|
||||
case .none:
|
||||
break
|
||||
group.cancelAll()
|
||||
}
|
||||
}
|
||||
|
||||
group.cancelAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stateMachine.taskIsStarted(task: task, downstreamContinuation: downStreamContinuation)
|
||||
stateMachine.taskIsStarted(task: task, downstreamContinuation: downstreamContinuation)
|
||||
}
|
||||
}
|
||||
@@ -132,3 +132,7 @@ extension AsyncSequenceValidationDiagram.Clock: TestClock { }
|
||||
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
extension AsyncSequenceValidationDiagram.Clock: Clock { }
|
||||
|
||||
// placeholders to avoid warnings
|
||||
extension AsyncSequenceValidationDiagram.Clock.Instant: Hashable { }
|
||||
extension AsyncSequenceValidationDiagram.Clock.Instant: Comparable { }
|
||||
@@ -19,6 +19,6 @@ struct Job: Hashable, @unchecked Sendable {
|
||||
}
|
||||
|
||||
func execute() {
|
||||
_swiftJobRun(unsafeBitCast(job, to: UnownedJob.self), AsyncSequenceValidationDiagram.Context.executor.asUnownedSerialExecutor())
|
||||
_swiftJobRun(unsafeBitCast(job, to: UnownedJob.self), AsyncSequenceValidationDiagram.Context.unownedExecutor)
|
||||
}
|
||||
}
|
||||
@@ -12,11 +12,17 @@
|
||||
import _CAsyncSequenceValidationSupport
|
||||
|
||||
#if canImport(Darwin)
|
||||
@_implementationOnly import Darwin
|
||||
import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
@_implementationOnly import Glibc
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import Musl
|
||||
#elseif canImport(Bionic)
|
||||
import Bionic
|
||||
#elseif canImport(WinSDK)
|
||||
#error("TODO: Port TaskDriver threading to windows")
|
||||
#else
|
||||
#error("Unsupported platform")
|
||||
#endif
|
||||
|
||||
#if canImport(Darwin)
|
||||
@@ -24,11 +30,16 @@ func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
|
||||
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
|
||||
return nil
|
||||
}
|
||||
#elseif canImport(Glibc)
|
||||
#elseif (canImport(Glibc) && !os(Android)) || canImport(Musl)
|
||||
func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
|
||||
Unmanaged<TaskDriver>.fromOpaque(raw!).takeRetainedValue().run()
|
||||
return nil
|
||||
}
|
||||
#elseif os(Android)
|
||||
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer {
|
||||
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
|
||||
return UnsafeMutableRawPointer(bitPattern: 0xdeadbee)!
|
||||
}
|
||||
#elseif canImport(WinSDK)
|
||||
#error("TODO: Port TaskDriver threading to windows")
|
||||
#endif
|
||||
@@ -38,7 +49,7 @@ final class TaskDriver {
|
||||
let queue: WorkQueue
|
||||
#if canImport(Darwin)
|
||||
var thread: pthread_t?
|
||||
#elseif canImport(Glibc)
|
||||
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
var thread = pthread_t()
|
||||
#elseif canImport(WinSDK)
|
||||
#error("TODO: Port TaskDriver threading to windows")
|
||||
@@ -50,8 +61,12 @@ final class TaskDriver {
|
||||
}
|
||||
|
||||
func start() {
|
||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
pthread_create(&thread, nil, start_thread,
|
||||
Unmanaged.passRetained(self).toOpaque())
|
||||
#elseif canImport(WinSDK)
|
||||
#error("TODO: Port TaskDriver threading to windows")
|
||||
#endif
|
||||
}
|
||||
|
||||
func run() {
|
||||
@@ -64,7 +79,7 @@ final class TaskDriver {
|
||||
func join() {
|
||||
#if canImport(Darwin)
|
||||
pthread_join(thread!, nil)
|
||||
#elseif canImport(Glibc)
|
||||
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
pthread_join(thread, nil)
|
||||
#elseif canImport(WinSDK)
|
||||
#error("TODO: Port TaskDriver threading to windows")
|
||||
|
||||
@@ -67,9 +67,27 @@ extension AsyncSequenceValidationDiagram {
|
||||
}
|
||||
|
||||
struct Context {
|
||||
#if swift(<5.9)
|
||||
final class ClockExecutor: SerialExecutor {
|
||||
func enqueue(_ job: UnownedJob) {
|
||||
job._runSynchronously(on: asUnownedSerialExecutor())
|
||||
job._runSynchronously(on: self.asUnownedSerialExecutor())
|
||||
}
|
||||
|
||||
func asUnownedSerialExecutor() -> UnownedSerialExecutor {
|
||||
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 {
|
||||
@@ -77,9 +95,34 @@ extension AsyncSequenceValidationDiagram {
|
||||
}
|
||||
}
|
||||
|
||||
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 = {
|
||||
if #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) {
|
||||
return ClockExecutor_5_9()
|
||||
} else {
|
||||
return ClockExecutor_Pre5_9()
|
||||
}
|
||||
}()
|
||||
|
||||
static var unownedExecutor: UnownedSerialExecutor {
|
||||
(_executor as! any SerialExecutor).asUnownedSerialExecutor()
|
||||
}
|
||||
#endif
|
||||
|
||||
static var clock: Clock?
|
||||
|
||||
static let executor = ClockExecutor()
|
||||
|
||||
|
||||
static var driver: TaskDriver?
|
||||
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 AsyncAlgorithms
|
||||
import XCTest
|
||||
|
||||
final class TestInterspersed: XCTestCase {
|
||||
func test_interspersed() async {
|
||||
let source = [1, 2, 3, 4, 5]
|
||||
let expected = [1, 0, 2, 0, 3, 0, 4, 0, 5]
|
||||
let sequence = source.async.interspersed(with: 0)
|
||||
var actual = [Int]()
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
while let item = await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_every() async {
|
||||
let source = [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
let expected = [1, 2, 3, 0, 4, 5, 6, 0, 7, 8]
|
||||
let sequence = source.async.interspersed(every: 3, with: 0)
|
||||
var actual = [Int]()
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
while let item = await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_closure() async {
|
||||
let source = [1, 2, 3, 4, 5]
|
||||
let expected = [1, 0, 2, 0, 3, 0, 4, 0, 5]
|
||||
let sequence = source.async.interspersed(with: { 0 })
|
||||
var actual = [Int]()
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
while let item = await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_async_closure() async {
|
||||
let source = [1, 2, 3, 4, 5]
|
||||
let expected = [1, 0, 2, 0, 3, 0, 4, 0, 5]
|
||||
let sequence = source.async.interspersed {
|
||||
try! await Task.sleep(nanoseconds: 1000)
|
||||
return 0
|
||||
}
|
||||
var actual = [Int]()
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
while let item = await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_throwing_closure() async {
|
||||
let source = [1, 2]
|
||||
let expected = [1]
|
||||
var actual = [Int]()
|
||||
let sequence = source.async.interspersed(with: { throw Failure() })
|
||||
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
do {
|
||||
while let item = try await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
XCTFail()
|
||||
} catch {
|
||||
XCTAssertEqual(Failure(), error as? Failure)
|
||||
}
|
||||
let pastEnd = try! await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_async_throwing_closure() async {
|
||||
let source = [1, 2]
|
||||
let expected = [1]
|
||||
var actual = [Int]()
|
||||
let sequence = source.async.interspersed {
|
||||
try await Task.sleep(nanoseconds: 1000)
|
||||
throw Failure()
|
||||
}
|
||||
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
do {
|
||||
while let item = try await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
XCTFail()
|
||||
} catch {
|
||||
XCTAssertEqual(Failure(), error as? Failure)
|
||||
}
|
||||
let pastEnd = try! await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_empty() async {
|
||||
let source = [Int]()
|
||||
let expected = [Int]()
|
||||
let sequence = source.async.interspersed(with: 0)
|
||||
var actual = [Int]()
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
while let item = await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_with_throwing_upstream() async {
|
||||
let source = [1, 2, 3, -1, 4, 5]
|
||||
let expected = [1, 0, 2, 0, 3]
|
||||
var actual = [Int]()
|
||||
let sequence = source.async.map {
|
||||
try throwOn(-1, $0)
|
||||
}.interspersed(with: 0)
|
||||
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
do {
|
||||
while let item = try await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
XCTFail()
|
||||
} catch {
|
||||
XCTAssertEqual(Failure(), error as? Failure)
|
||||
}
|
||||
let pastEnd = try! await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_cancellation() async {
|
||||
let source = Indefinite(value: "test")
|
||||
let sequence = source.async.interspersed(with: "sep")
|
||||
let lockStepChannel = AsyncChannel<Void>()
|
||||
|
||||
await withTaskGroup(of: Void.self) { group in
|
||||
group.addTask {
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
let _ = await iterator.next()
|
||||
|
||||
// Information the parent task that we are consuming
|
||||
await lockStepChannel.send(())
|
||||
|
||||
while let _ = await iterator.next() {}
|
||||
|
||||
await lockStepChannel.send(())
|
||||
}
|
||||
|
||||
// Waiting until the child task started consuming
|
||||
_ = await lockStepChannel.first { _ in true }
|
||||
|
||||
// Now we cancel the child
|
||||
group.cancelAll()
|
||||
|
||||
await group.waitForAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,32 @@ import AsyncAlgorithms
|
||||
|
||||
#if canImport(Darwin)
|
||||
final class TestThroughput: XCTestCase {
|
||||
func test_channel() async {
|
||||
await measureChannelThroughput(output: 1)
|
||||
}
|
||||
func test_throwingChannel() async {
|
||||
await measureThrowingChannelThroughput(output: 1)
|
||||
}
|
||||
func test_buffer_bounded() async {
|
||||
await measureSequenceThroughput(output: 1) {
|
||||
$0.buffer(policy: .bounded(5))
|
||||
}
|
||||
}
|
||||
func test_buffer_unbounded() async {
|
||||
await measureSequenceThroughput(output: 1) {
|
||||
$0.buffer(policy: .unbounded)
|
||||
}
|
||||
}
|
||||
func test_buffer_bufferingNewest() async {
|
||||
await measureSequenceThroughput(output: 1) {
|
||||
$0.buffer(policy: .bufferingLatest(5))
|
||||
}
|
||||
}
|
||||
func test_buffer_bufferingOldest() async {
|
||||
await measureSequenceThroughput(output: 1) {
|
||||
$0.buffer(policy: .bufferingOldest(5))
|
||||
}
|
||||
}
|
||||
func test_chain2() async {
|
||||
await measureSequenceThroughput(firstOutput: 1, secondOutput: 2) {
|
||||
chain($0, $1)
|
||||
@@ -64,11 +90,12 @@ final class TestThroughput: XCTestCase {
|
||||
zip($0, $1, $2)
|
||||
}
|
||||
}
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
func test_debounce() async {
|
||||
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
|
||||
await measureSequenceThroughput(source: (1...).async) {
|
||||
$0.debounce(for: .zero, clock: ContinuousClock())
|
||||
$0.debounce(for: .zero, clock: ContinuousClock())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -56,6 +56,64 @@ final class _ThroughputMetric: NSObject, XCTMetric, @unchecked Sendable {
|
||||
}
|
||||
|
||||
extension XCTestCase {
|
||||
public func measureChannelThroughput<Output: Sendable>(output: @Sendable @escaping @autoclosure () -> Output) async {
|
||||
let metric = _ThroughputMetric()
|
||||
let sampleTime: Double = 0.1
|
||||
|
||||
measure(metrics: [metric]) {
|
||||
let channel = AsyncChannel<Output>()
|
||||
|
||||
let exp = self.expectation(description: "Finished")
|
||||
let iterTask = Task<Int, Error> {
|
||||
var eventCount = 0
|
||||
for try await _ in channel {
|
||||
eventCount += 1
|
||||
}
|
||||
metric.eventCount = eventCount
|
||||
exp.fulfill()
|
||||
return eventCount
|
||||
}
|
||||
let sendTask = Task<Void, Never> {
|
||||
while !Task.isCancelled {
|
||||
await channel.send(output())
|
||||
}
|
||||
}
|
||||
usleep(UInt32(sampleTime * Double(USEC_PER_SEC)))
|
||||
iterTask.cancel()
|
||||
sendTask.cancel()
|
||||
self.wait(for: [exp], timeout: sampleTime * 2)
|
||||
}
|
||||
}
|
||||
|
||||
public func measureThrowingChannelThroughput<Output: Sendable>(output: @Sendable @escaping @autoclosure () -> Output) async {
|
||||
let metric = _ThroughputMetric()
|
||||
let sampleTime: Double = 0.1
|
||||
|
||||
measure(metrics: [metric]) {
|
||||
let channel = AsyncThrowingChannel<Output, Error>()
|
||||
|
||||
let exp = self.expectation(description: "Finished")
|
||||
let iterTask = Task<Int, Error> {
|
||||
var eventCount = 0
|
||||
for try await _ in channel {
|
||||
eventCount += 1
|
||||
}
|
||||
metric.eventCount = eventCount
|
||||
exp.fulfill()
|
||||
return eventCount
|
||||
}
|
||||
let sendTask = Task<Void, Never> {
|
||||
while !Task.isCancelled {
|
||||
await channel.send(output())
|
||||
}
|
||||
}
|
||||
usleep(UInt32(sampleTime * Double(USEC_PER_SEC)))
|
||||
iterTask.cancel()
|
||||
sendTask.cancel()
|
||||
self.wait(for: [exp], timeout: sampleTime * 2)
|
||||
}
|
||||
}
|
||||
|
||||
public func measureSequenceThroughput<S: AsyncSequence, Output>( output: @autoclosure () -> Output, _ sequenceBuilder: (InfiniteAsyncSequence<Output>) -> S) async where S: Sendable {
|
||||
let metric = _ThroughputMetric()
|
||||
let sampleTime: Double = 0.1
|
||||
|
||||
@@ -165,3 +165,47 @@ internal func XCTAssertThrowsError<T>(
|
||||
verify(error)
|
||||
}
|
||||
}
|
||||
|
||||
class WaiterDelegate: NSObject, XCTWaiterDelegate {
|
||||
let state: ManagedCriticalState<UnsafeContinuation<Void, Never>?> = ManagedCriticalState(nil)
|
||||
|
||||
init(_ continuation: UnsafeContinuation<Void, Never>) {
|
||||
state.withCriticalRegion { $0 = continuation }
|
||||
}
|
||||
|
||||
func waiter(_ waiter: XCTWaiter, didFulfillInvertedExpectation expectation: XCTestExpectation) {
|
||||
resume()
|
||||
}
|
||||
|
||||
func waiter(_ waiter: XCTWaiter, didTimeoutWithUnfulfilledExpectations unfulfilledExpectations: [XCTestExpectation]) {
|
||||
resume()
|
||||
}
|
||||
|
||||
func waiter(_ waiter: XCTWaiter, fulfillmentDidViolateOrderingConstraintsFor expectation: XCTestExpectation, requiredExpectation: XCTestExpectation) {
|
||||
resume()
|
||||
}
|
||||
|
||||
func nestedWaiter(_ waiter: XCTWaiter, wasInterruptedByTimedOutWaiter outerWaiter: XCTWaiter) {
|
||||
|
||||
}
|
||||
|
||||
func resume() {
|
||||
let continuation = state.withCriticalRegion { continuation in
|
||||
defer { continuation = nil }
|
||||
return continuation
|
||||
}
|
||||
continuation?.resume()
|
||||
}
|
||||
}
|
||||
|
||||
extension XCTestCase {
|
||||
@_disfavoredOverload
|
||||
func fulfillment(of expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false, file: StaticString = #file, line: Int = #line) async {
|
||||
return await withUnsafeContinuation { continuation in
|
||||
let delegate = WaiterDelegate(continuation)
|
||||
let waiter = XCTWaiter(delegate: delegate)
|
||||
waiter.wait(for: expectations, timeout: timeout, enforceOrder: enforceOrder)
|
||||
delegate.resume()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,10 +88,11 @@ final class TestAdjacentPairs: XCTestCase {
|
||||
}
|
||||
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
@@ -13,219 +13,102 @@ import XCTest
|
||||
import AsyncAlgorithms
|
||||
|
||||
final class TestBuffer: XCTestCase {
|
||||
func test_buffering() async {
|
||||
var gated = GatedSequence([1, 2, 3, 4, 5])
|
||||
let sequence = gated.buffer(policy: .unbounded)
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
|
||||
gated.advance()
|
||||
func test_given_a_base_sequence_when_buffering_with_unbounded_then_the_buffer_is_filled_in() async {
|
||||
// Given
|
||||
var base = GatedSequence([1, 2, 3, 4, 5])
|
||||
let buffered = base.buffer(policy: .unbounded)
|
||||
var iterator = buffered.makeAsyncIterator()
|
||||
|
||||
// When
|
||||
base.advance()
|
||||
|
||||
// Then
|
||||
var value = await iterator.next()
|
||||
XCTAssertEqual(value, 1)
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
|
||||
// When
|
||||
base.advance()
|
||||
base.advance()
|
||||
base.advance()
|
||||
|
||||
// Then
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, 2)
|
||||
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, 3)
|
||||
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, 4)
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
|
||||
// When
|
||||
base.advance()
|
||||
base.advance()
|
||||
|
||||
// Then
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, 5)
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, nil)
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, nil)
|
||||
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertEqual(value, pastEnd)
|
||||
}
|
||||
|
||||
func test_buffering_withError() async {
|
||||
func test_given_a_failable_base_sequence_when_buffering_with_unbounded_then_the_failure_is_forwarded() async {
|
||||
// Given
|
||||
var gated = GatedSequence([1, 2, 3, 4, 5, 6, 7])
|
||||
let gated_map = gated.map { try throwOn(3, $0) }
|
||||
let sequence = gated_map.buffer(policy: .unbounded)
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
let base = gated.map { try throwOn(3, $0) }
|
||||
let buffered = base.buffer(policy: .unbounded)
|
||||
var iterator = buffered.makeAsyncIterator()
|
||||
|
||||
// When
|
||||
gated.advance()
|
||||
|
||||
// Then
|
||||
var value = try! await iterator.next()
|
||||
XCTAssertEqual(value, 1)
|
||||
|
||||
// When
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
|
||||
// Then
|
||||
value = try! await iterator.next()
|
||||
XCTAssertEqual(value, 2)
|
||||
|
||||
// When
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
|
||||
// Then
|
||||
do {
|
||||
value = try await iterator.next()
|
||||
XCTFail("next() should have thrown.")
|
||||
} catch { }
|
||||
|
||||
value = try! await iterator.next()
|
||||
XCTAssertNil(value)
|
||||
|
||||
value = try! await iterator.next()
|
||||
XCTAssertNil(value)
|
||||
|
||||
value = try! await iterator.next()
|
||||
XCTAssertNil(value)
|
||||
|
||||
value = try! await iterator.next()
|
||||
XCTAssertNil(value)
|
||||
|
||||
value = try! await iterator.next()
|
||||
XCTAssertNil(value)
|
||||
}
|
||||
|
||||
func test_buffer_delegation() async {
|
||||
actor BufferDelegate: AsyncBuffer {
|
||||
var buffer = [Int]()
|
||||
var pushed = [Int]()
|
||||
|
||||
func push(_ element: Int) async {
|
||||
buffer.append(element)
|
||||
pushed.append(element)
|
||||
}
|
||||
|
||||
func pop() async -> Int? {
|
||||
if buffer.count > 0 {
|
||||
return buffer.removeFirst()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
} catch {
|
||||
XCTAssert(error is Failure)
|
||||
}
|
||||
let delegate = BufferDelegate()
|
||||
var gated = GatedSequence([1, 2, 3, 4, 5])
|
||||
let sequence = gated.buffer {
|
||||
delegate
|
||||
}
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
|
||||
gated.advance()
|
||||
var value = await iterator.next()
|
||||
var pushed = await delegate.pushed
|
||||
XCTAssertEqual(pushed, [1])
|
||||
XCTAssertEqual(value, 1)
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, 2)
|
||||
value = await iterator.next()
|
||||
pushed = await delegate.pushed
|
||||
XCTAssertEqual(value, 3)
|
||||
value = await iterator.next()
|
||||
pushed = await delegate.pushed
|
||||
XCTAssertEqual(pushed, [1, 2, 3, 4])
|
||||
XCTAssertEqual(value, 4)
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
value = await iterator.next()
|
||||
pushed = await delegate.pushed
|
||||
XCTAssertEqual(pushed, [1, 2, 3, 4, 5])
|
||||
XCTAssertEqual(value, 5)
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, nil)
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, nil)
|
||||
|
||||
var pastFailure = try! await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
|
||||
pastFailure = try! await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
|
||||
pastFailure = try! await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
|
||||
pastFailure = try! await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
|
||||
pastFailure = try! await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
}
|
||||
|
||||
func test_delegatedBuffer_withError() async {
|
||||
actor BufferDelegate: AsyncBuffer {
|
||||
var buffer = [Int]()
|
||||
var pushed = [Int]()
|
||||
|
||||
func push(_ element: Int) async {
|
||||
buffer.append(element)
|
||||
pushed.append(element)
|
||||
}
|
||||
|
||||
func pop() async throws -> Int? {
|
||||
if buffer.count > 0 {
|
||||
let value = buffer.removeFirst()
|
||||
if value == 3 {
|
||||
throw Failure()
|
||||
}
|
||||
return value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
let delegate = BufferDelegate()
|
||||
|
||||
var gated = GatedSequence([1, 2, 3, 4, 5, 6, 7])
|
||||
let sequence = gated.buffer { delegate }
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
|
||||
gated.advance()
|
||||
var value = try! await iterator.next()
|
||||
XCTAssertEqual(value, 1)
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
value = try! await iterator.next()
|
||||
XCTAssertEqual(value, 2)
|
||||
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
do {
|
||||
value = try await iterator.next()
|
||||
XCTFail("next() should have thrown.")
|
||||
} catch { }
|
||||
|
||||
value = try! await iterator.next()
|
||||
XCTAssertNil(value)
|
||||
|
||||
value = try! await iterator.next()
|
||||
XCTAssertNil(value)
|
||||
|
||||
value = try! await iterator.next()
|
||||
XCTAssertNil(value)
|
||||
|
||||
value = try! await iterator.next()
|
||||
XCTAssertNil(value)
|
||||
|
||||
value = try! await iterator.next()
|
||||
XCTAssertNil(value)
|
||||
}
|
||||
|
||||
func test_byteBuffer() async {
|
||||
actor ByteBuffer: AsyncBuffer {
|
||||
var buffer: [UInt8]?
|
||||
|
||||
func push(_ element: UInt8) async {
|
||||
if buffer == nil {
|
||||
buffer = [UInt8]()
|
||||
}
|
||||
buffer?.append(element)
|
||||
}
|
||||
|
||||
func pop() async -> [UInt8]? {
|
||||
defer { buffer = nil }
|
||||
return buffer
|
||||
}
|
||||
}
|
||||
|
||||
var data = Data()
|
||||
for _ in 0..<4096 {
|
||||
data.append(UInt8.random(in: 0..<UInt8.max))
|
||||
}
|
||||
let buffered = data.async.buffer {
|
||||
ByteBuffer()
|
||||
}
|
||||
var collected = Data()
|
||||
for await segment in buffered {
|
||||
collected.append(contentsOf: segment)
|
||||
}
|
||||
XCTAssertEqual(data, collected)
|
||||
}
|
||||
|
||||
func test_bufferingOldest() async {
|
||||
func test_given_a_base_sequence_when_bufferingOldest_then_the_policy_is_applied() async {
|
||||
validate {
|
||||
"X-12- 34- 5 |"
|
||||
$0.inputs[0].buffer(policy: .bufferingOldest(2))
|
||||
@@ -233,7 +116,15 @@ final class TestBuffer: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_bufferingOldest_noDrops() async {
|
||||
func test_given_a_base_sequence_when_bufferingOldest_with_0_limit_then_the_policy_is_transparent() async {
|
||||
validate {
|
||||
"X-12- 34- 5 |"
|
||||
$0.inputs[0].buffer(policy: .bufferingOldest(0))
|
||||
"X-12- 34- 5 |"
|
||||
}
|
||||
}
|
||||
|
||||
func test_given_a_base_sequence_when_bufferingOldest_at_slow_pace_then_no_element_is_dropped() async {
|
||||
validate {
|
||||
"X-12 3 4 5 |"
|
||||
$0.inputs[0].buffer(policy: .bufferingOldest(2))
|
||||
@@ -241,7 +132,7 @@ final class TestBuffer: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_bufferingOldest_error() async {
|
||||
func test_given_a_failable_base_sequence_when_bufferingOldest_then_the_failure_is_forwarded() async {
|
||||
validate {
|
||||
"X-12345^"
|
||||
$0.inputs[0].buffer(policy: .bufferingOldest(2))
|
||||
@@ -249,27 +140,190 @@ final class TestBuffer: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_bufferingNewest() async {
|
||||
func test_given_a_base_sequence_when_bufferingNewest_then_the_policy_is_applied() async {
|
||||
validate {
|
||||
"X-12- 34 -5|"
|
||||
$0.inputs[0].buffer(policy: .bufferingNewest(2))
|
||||
$0.inputs[0].buffer(policy: .bufferingLatest(2))
|
||||
"X,,,[1,],,[3,],[4,][5,]|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_bufferingNewest_noDrops() async {
|
||||
func test_given_a_base_sequence_when_bufferingNewest_with_limit_0_then_the_policy_is_transparent() async {
|
||||
validate {
|
||||
"X-12- 34 -5|"
|
||||
$0.inputs[0].buffer(policy: .bufferingLatest(0))
|
||||
"X-12- 34 -5|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_given_a_base_sequence_when_bufferingNewest_at_slow_pace_then_no_element_is_dropped() async {
|
||||
validate {
|
||||
"X-12 3 4 5 |"
|
||||
$0.inputs[0].buffer(policy: .bufferingNewest(2))
|
||||
$0.inputs[0].buffer(policy: .bufferingLatest(2))
|
||||
"X,,[1,][2,][3,][45]|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_bufferingNewest_error() async {
|
||||
func test_given_a_failable_base_sequence_when_bufferingNewest_then_the_failure_is_forwarded() async {
|
||||
validate {
|
||||
"X-12345^"
|
||||
$0.inputs[0].buffer(policy: .bufferingNewest(2))
|
||||
$0.inputs[0].buffer(policy: .bufferingLatest(2))
|
||||
"X,,,,,,[45^]"
|
||||
}
|
||||
}
|
||||
|
||||
func test_given_a_buffered_with_unbounded_sequence_when_cancelling_consumer_then_the_iteration_finishes_and_the_base_is_cancelled() async {
|
||||
// Given
|
||||
let buffered = Indefinite(value: 1).async.buffer(policy: .unbounded)
|
||||
|
||||
let finished = expectation(description: "finished")
|
||||
let iterated = expectation(description: "iterated")
|
||||
|
||||
let task = Task {
|
||||
var firstIteration = false
|
||||
for await _ in buffered {
|
||||
if !firstIteration {
|
||||
firstIteration = true
|
||||
iterated.fulfill()
|
||||
}
|
||||
}
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the task actually starts
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
|
||||
// When
|
||||
task.cancel()
|
||||
|
||||
// Then
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
|
||||
func test_given_a_base_sequence_when_buffering_with_bounded_then_the_buffer_is_filled_in_and_suspends() async {
|
||||
// Gicen
|
||||
var base = GatedSequence([1, 2, 3, 4, 5])
|
||||
let buffered = base.buffer(policy: .bounded(2))
|
||||
var iterator = buffered.makeAsyncIterator()
|
||||
|
||||
// When
|
||||
base.advance()
|
||||
|
||||
// Then
|
||||
var value = await iterator.next()
|
||||
XCTAssertEqual(value, 1)
|
||||
|
||||
// When
|
||||
base.advance()
|
||||
base.advance()
|
||||
base.advance()
|
||||
|
||||
// Then
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, 2)
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, 3)
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, 4)
|
||||
|
||||
// When
|
||||
base.advance()
|
||||
base.advance()
|
||||
|
||||
// Then
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, 5)
|
||||
value = await iterator.next()
|
||||
XCTAssertEqual(value, nil)
|
||||
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertEqual(value, pastEnd)
|
||||
}
|
||||
|
||||
func test_given_a_failable_base_sequence_when_buffering_with_bounded_then_the_failure_is_forwarded() async {
|
||||
// Given
|
||||
var gated = GatedSequence([1, 2, 3, 4, 5, 6, 7])
|
||||
let base = gated.map { try throwOn(3, $0) }
|
||||
let buffered = base.buffer(policy: .bounded(5))
|
||||
var iterator = buffered.makeAsyncIterator()
|
||||
|
||||
// When
|
||||
gated.advance()
|
||||
|
||||
// Then
|
||||
var value = try! await iterator.next()
|
||||
XCTAssertEqual(value, 1)
|
||||
|
||||
// When
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
|
||||
// Then
|
||||
value = try! await iterator.next()
|
||||
XCTAssertEqual(value, 2)
|
||||
|
||||
// When
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
gated.advance()
|
||||
|
||||
// Then
|
||||
do {
|
||||
value = try await iterator.next()
|
||||
XCTFail("next() should have thrown.")
|
||||
} catch {
|
||||
XCTAssert(error is Failure)
|
||||
}
|
||||
|
||||
var pastFailure = try! await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
|
||||
pastFailure = try! await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
|
||||
pastFailure = try! await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
|
||||
pastFailure = try! await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
|
||||
pastFailure = try! await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
}
|
||||
|
||||
func test_given_a_buffered_bounded_sequence_when_cancelling_consumer_then_the_iteration_finishes_and_the_base_is_cancelled() async {
|
||||
// Given
|
||||
let buffered = Indefinite(value: 1).async.buffer(policy: .bounded(3))
|
||||
|
||||
let finished = expectation(description: "finished")
|
||||
let iterated = expectation(description: "iterated")
|
||||
|
||||
let task = Task {
|
||||
var firstIteration = false
|
||||
for await _ in buffered {
|
||||
if !firstIteration {
|
||||
firstIteration = true
|
||||
iterated.fulfill()
|
||||
}
|
||||
}
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
|
||||
// When
|
||||
task.cancel()
|
||||
|
||||
// Then
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
|
||||
func test_given_a_base_sequence_when_bounded_with_limit_0_then_the_policy_is_transparent() async {
|
||||
validate {
|
||||
"X-12- 34 -5|"
|
||||
$0.inputs[0].buffer(policy: .bounded(0))
|
||||
"X-12- 34 -5|"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -199,10 +199,10 @@ final class TestBufferedByteIterator: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
@@ -87,13 +87,13 @@ final class TestChain2: XCTestCase {
|
||||
}
|
||||
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,12 +192,12 @@ final class TestChain3: XCTestCase {
|
||||
}
|
||||
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
@@ -13,265 +13,164 @@ import XCTest
|
||||
import AsyncAlgorithms
|
||||
|
||||
final class TestChannel: XCTestCase {
|
||||
func test_asyncChannel_delivers_values_when_two_producers_and_two_consumers() async {
|
||||
let (sentFromProducer1, sentFromProducer2) = ("test1", "test2")
|
||||
let expected = Set([sentFromProducer1, sentFromProducer2])
|
||||
func test_asyncChannel_delivers_elements_when_several_producers_and_several_consumers() async {
|
||||
let sents = (1...10)
|
||||
let expected = Set(sents)
|
||||
|
||||
let channel = AsyncChannel<String>()
|
||||
// Given: an AsyncChannel
|
||||
let sut = AsyncChannel<Int>()
|
||||
|
||||
// When: sending elements from tasks in a group
|
||||
Task {
|
||||
await channel.send(sentFromProducer1)
|
||||
await withTaskGroup(of: Void.self) { group in
|
||||
for sent in sents {
|
||||
group.addTask {
|
||||
await sut.send(sent)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Task {
|
||||
await channel.send(sentFromProducer2)
|
||||
}
|
||||
|
||||
let t: Task<String?, Never> = Task {
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
let value = await iterator.next()
|
||||
return value
|
||||
}
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
|
||||
let (collectedFromConsumer1, collectedFromConsumer2) = (await t.value, await iterator.next())
|
||||
let collected = Set([collectedFromConsumer1, collectedFromConsumer2])
|
||||
// When: receiving those elements from tasks in a group
|
||||
let collected = await withTaskGroup(of: Int.self, returning: Set<Int>.self) { group in
|
||||
for _ in sents {
|
||||
group.addTask {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
let received = await iterator.next()
|
||||
return received!
|
||||
}
|
||||
}
|
||||
|
||||
var collected = Set<Int>()
|
||||
for await element in group {
|
||||
collected.update(with: element)
|
||||
}
|
||||
return collected
|
||||
}
|
||||
|
||||
// Then: all elements are sent and received
|
||||
XCTAssertEqual(collected, expected)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_delivers_values_when_two_producers_and_two_consumers() async throws {
|
||||
let (sentFromProducer1, sentFromProducer2) = ("test1", "test2")
|
||||
let expected = Set([sentFromProducer1, sentFromProducer2])
|
||||
|
||||
let channel = AsyncThrowingChannel<String, Error>()
|
||||
Task {
|
||||
await channel.send("test1")
|
||||
}
|
||||
Task {
|
||||
await channel.send("test2")
|
||||
}
|
||||
|
||||
let t: Task<String?, Error> = Task {
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
let value = try await iterator.next()
|
||||
return value
|
||||
}
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
func test_asyncChannel_resumes_producers_and_discards_additional_elements_when_finish_is_called() async {
|
||||
// Given: an AsyncChannel
|
||||
let sut = AsyncChannel<Int>()
|
||||
|
||||
let (collectedFromConsumer1, collectedFromConsumer2) = (try await t.value, try await iterator.next())
|
||||
let collected = Set([collectedFromConsumer1, collectedFromConsumer2])
|
||||
|
||||
XCTAssertEqual(collected, expected)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_throws_and_discards_additional_sent_values_when_fail_is_called() async {
|
||||
let sendImmediatelyResumes = expectation(description: "Send immediately resumes after fail")
|
||||
|
||||
let channel = AsyncThrowingChannel<String, Error>()
|
||||
channel.fail(Failure())
|
||||
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
do {
|
||||
let _ = try await iterator.next()
|
||||
XCTFail("The AsyncThrowingChannel should have thrown")
|
||||
} catch {
|
||||
XCTAssertEqual(error as? Failure, Failure())
|
||||
// Given: 2 suspended send operations
|
||||
let task1 = Task {
|
||||
await sut.send(1)
|
||||
}
|
||||
|
||||
do {
|
||||
let pastFailure = try await iterator.next()
|
||||
XCTAssertNil(pastFailure)
|
||||
} catch {
|
||||
XCTFail("The AsyncThrowingChannel should not fail when failure has already been fired")
|
||||
let task2 = Task {
|
||||
await sut.send(2)
|
||||
}
|
||||
|
||||
await channel.send("send")
|
||||
sendImmediatelyResumes.fulfill()
|
||||
wait(for: [sendImmediatelyResumes], timeout: 1.0)
|
||||
// When: finishing the channel
|
||||
sut.finish()
|
||||
|
||||
// Then: the send operations are resumed
|
||||
_ = await (task1.value, task2.value)
|
||||
|
||||
// When: sending an extra value
|
||||
await sut.send(3)
|
||||
|
||||
// Then: the operation and the iteration are immediately resumed
|
||||
var collected = [Int]()
|
||||
for await element in sut {
|
||||
collected.append(element)
|
||||
}
|
||||
XCTAssertTrue(collected.isEmpty)
|
||||
}
|
||||
|
||||
func test_asyncChannel_ends_alls_iterators_and_discards_additional_sent_values_when_finish_is_called() async {
|
||||
let channel = AsyncChannel<String>()
|
||||
let complete = ManagedCriticalState(false)
|
||||
let finished = expectation(description: "finished")
|
||||
func test_asyncChannel_resumes_consumers_when_finish_is_called() async {
|
||||
// Given: an AsyncChannel
|
||||
let sut = AsyncChannel<Int>()
|
||||
|
||||
Task {
|
||||
channel.finish()
|
||||
complete.withCriticalRegion { $0 = true }
|
||||
finished.fulfill()
|
||||
}
|
||||
|
||||
let valueFromConsumer1 = ManagedCriticalState<String?>(nil)
|
||||
let valueFromConsumer2 = ManagedCriticalState<String?>(nil)
|
||||
|
||||
let received = expectation(description: "received")
|
||||
received.expectedFulfillmentCount = 2
|
||||
|
||||
let pastEnd = expectation(description: "pastEnd")
|
||||
pastEnd.expectedFulfillmentCount = 2
|
||||
|
||||
Task {
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
let ending = await iterator.next()
|
||||
valueFromConsumer1.withCriticalRegion { $0 = ending }
|
||||
received.fulfill()
|
||||
let item = await iterator.next()
|
||||
XCTAssertNil(item)
|
||||
pastEnd.fulfill()
|
||||
}
|
||||
|
||||
Task {
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
let ending = await iterator.next()
|
||||
valueFromConsumer2.withCriticalRegion { $0 = ending }
|
||||
received.fulfill()
|
||||
let item = await iterator.next()
|
||||
XCTAssertNil(item)
|
||||
pastEnd.fulfill()
|
||||
}
|
||||
|
||||
wait(for: [finished, received], timeout: 1.0)
|
||||
|
||||
XCTAssertTrue(complete.withCriticalRegion { $0 })
|
||||
XCTAssertEqual(valueFromConsumer1.withCriticalRegion { $0 }, nil)
|
||||
XCTAssertEqual(valueFromConsumer2.withCriticalRegion { $0 }, nil)
|
||||
|
||||
wait(for: [pastEnd], timeout: 1.0)
|
||||
let additionalSend = expectation(description: "additional send")
|
||||
Task {
|
||||
await channel.send("test")
|
||||
additionalSend.fulfill()
|
||||
}
|
||||
wait(for: [additionalSend], timeout: 1.0)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_ends_alls_iterators_and_discards_additional_sent_values_when_finish_is_called() async {
|
||||
let channel = AsyncThrowingChannel<String, Error>()
|
||||
let complete = ManagedCriticalState(false)
|
||||
let finished = expectation(description: "finished")
|
||||
|
||||
Task {
|
||||
channel.finish()
|
||||
complete.withCriticalRegion { $0 = true }
|
||||
finished.fulfill()
|
||||
}
|
||||
|
||||
let valueFromConsumer1 = ManagedCriticalState<String?>(nil)
|
||||
let valueFromConsumer2 = ManagedCriticalState<String?>(nil)
|
||||
|
||||
let received = expectation(description: "received")
|
||||
received.expectedFulfillmentCount = 2
|
||||
|
||||
let pastEnd = expectation(description: "pastEnd")
|
||||
pastEnd.expectedFulfillmentCount = 2
|
||||
|
||||
Task {
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
let ending = try await iterator.next()
|
||||
valueFromConsumer1.withCriticalRegion { $0 = ending }
|
||||
received.fulfill()
|
||||
let item = try await iterator.next()
|
||||
XCTAssertNil(item)
|
||||
pastEnd.fulfill()
|
||||
}
|
||||
|
||||
Task {
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
let ending = try await iterator.next()
|
||||
valueFromConsumer2.withCriticalRegion { $0 = ending }
|
||||
received.fulfill()
|
||||
let item = try await iterator.next()
|
||||
XCTAssertNil(item)
|
||||
pastEnd.fulfill()
|
||||
}
|
||||
|
||||
wait(for: [finished, received], timeout: 1.0)
|
||||
|
||||
XCTAssertTrue(complete.withCriticalRegion { $0 })
|
||||
XCTAssertEqual(valueFromConsumer1.withCriticalRegion { $0 }, nil)
|
||||
XCTAssertEqual(valueFromConsumer2.withCriticalRegion { $0 }, nil)
|
||||
|
||||
wait(for: [pastEnd], timeout: 1.0)
|
||||
let additionalSend = expectation(description: "additional send")
|
||||
Task {
|
||||
await channel.send("test")
|
||||
additionalSend.fulfill()
|
||||
}
|
||||
wait(for: [additionalSend], timeout: 1.0)
|
||||
}
|
||||
|
||||
func test_asyncChannel_ends_iterator_when_task_is_cancelled() async {
|
||||
let channel = AsyncChannel<String>()
|
||||
let ready = expectation(description: "ready")
|
||||
let task: Task<String?, Never> = Task {
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
ready.fulfill()
|
||||
// Given: 2 suspended iterations
|
||||
let task1 = Task<Int?, Never> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
return await iterator.next()
|
||||
}
|
||||
wait(for: [ready], timeout: 1.0)
|
||||
task.cancel()
|
||||
let value = await task.value
|
||||
XCTAssertNil(value)
|
||||
|
||||
let task2 = Task<Int?, Never> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
return await iterator.next()
|
||||
}
|
||||
|
||||
// When: finishing the channel
|
||||
sut.finish()
|
||||
|
||||
// Then: the iterations are resumed with nil values
|
||||
let (collected1, collected2) = await (task1.value, task2.value)
|
||||
XCTAssertNil(collected1)
|
||||
XCTAssertNil(collected2)
|
||||
|
||||
// When: requesting a next value
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
let pastEnd = await iterator.next()
|
||||
|
||||
// Then: the past end is nil
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_ends_iterator_when_task_is_cancelled() async throws {
|
||||
let channel = AsyncThrowingChannel<String, Error>()
|
||||
let ready = expectation(description: "ready")
|
||||
let task: Task<String?, Error> = Task {
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
ready.fulfill()
|
||||
return try await iterator.next()
|
||||
func test_asyncChannel_resumes_producer_when_task_is_cancelled() async {
|
||||
let send1IsResumed = expectation(description: "The first send operation is resumed")
|
||||
|
||||
// Given: an AsyncChannel
|
||||
let sut = AsyncChannel<Int>()
|
||||
|
||||
// Given: 2 suspended send operations
|
||||
let task1 = Task {
|
||||
await sut.send(1)
|
||||
send1IsResumed.fulfill()
|
||||
}
|
||||
wait(for: [ready], timeout: 1.0)
|
||||
task.cancel()
|
||||
let value = try await task.value
|
||||
XCTAssertNil(value)
|
||||
|
||||
let task2 = Task {
|
||||
await sut.send(2)
|
||||
}
|
||||
|
||||
// When: cancelling the first task
|
||||
task1.cancel()
|
||||
|
||||
// Then: the first sending operation is resumed
|
||||
await fulfillment(of: [send1IsResumed], timeout: 1.0)
|
||||
|
||||
// When: collecting elements
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
let collected = await iterator.next()
|
||||
|
||||
// Then: the second operation resumes and the iteration receives the element
|
||||
_ = await task2.value
|
||||
XCTAssertEqual(collected, 2)
|
||||
}
|
||||
|
||||
func test_asyncChannel_resumes_send_when_task_is_cancelled_and_continue_remaining_send_tasks() async {
|
||||
let channel = AsyncChannel<Int>()
|
||||
let notYetDone = expectation(description: "not yet done")
|
||||
notYetDone.isInverted = true
|
||||
let done = expectation(description: "done")
|
||||
let task = Task {
|
||||
await channel.send(1)
|
||||
notYetDone.fulfill()
|
||||
done.fulfill()
|
||||
|
||||
func test_asyncChannel_resumes_consumer_when_task_is_cancelled() async {
|
||||
// Given: an AsyncChannel
|
||||
let sut = AsyncChannel<Int>()
|
||||
|
||||
// Given: 2 suspended iterations
|
||||
let task1 = Task<Int?, Never> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
return await iterator.next()
|
||||
}
|
||||
|
||||
Task {
|
||||
await channel.send(2)
|
||||
let task2 = Task<Int?, Never> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
return await iterator.next()
|
||||
}
|
||||
|
||||
wait(for: [notYetDone], timeout: 0.1)
|
||||
task.cancel()
|
||||
wait(for: [done], timeout: 1.0)
|
||||
// When: cancelling the first task
|
||||
task1.cancel()
|
||||
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
let received = await iterator.next()
|
||||
XCTAssertEqual(received, 2)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_resumes_send_when_task_is_cancelled_and_continue_remaining_send_tasks() async throws {
|
||||
let channel = AsyncThrowingChannel<Int, Error>()
|
||||
let notYetDone = expectation(description: "not yet done")
|
||||
notYetDone.isInverted = true
|
||||
let done = expectation(description: "done")
|
||||
let task = Task {
|
||||
await channel.send(1)
|
||||
notYetDone.fulfill()
|
||||
done.fulfill()
|
||||
}
|
||||
// Then: the iteration is resumed with a nil element
|
||||
let collected1 = await task1.value
|
||||
XCTAssertNil(collected1)
|
||||
|
||||
Task {
|
||||
await channel.send(2)
|
||||
}
|
||||
// When: sending an element
|
||||
await sut.send(1)
|
||||
|
||||
wait(for: [notYetDone], timeout: 0.1)
|
||||
task.cancel()
|
||||
wait(for: [done], timeout: 1.0)
|
||||
|
||||
var iterator = channel.makeAsyncIterator()
|
||||
let received = try await iterator.next()
|
||||
XCTAssertEqual(received, 2)
|
||||
// Then: the second iteration is resumed with the element
|
||||
let collected2 = await task2.value
|
||||
XCTAssertEqual(collected2, 1)
|
||||
}
|
||||
}
|
||||
@@ -13,15 +13,25 @@ import XCTest
|
||||
import AsyncSequenceValidation
|
||||
import AsyncAlgorithms
|
||||
|
||||
@Sendable
|
||||
func sumCharacters(_ array: [String]) -> String {
|
||||
return "\(array.reduce(into: 0) { $0 = $0 + Int($1)! })"
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func concatCharacters(_ array: [String]) -> String {
|
||||
return array.joined()
|
||||
}
|
||||
|
||||
final class TestChunk: XCTestCase {
|
||||
func test_count_one() {
|
||||
validate {
|
||||
"ABCDE|"
|
||||
$0.inputs[0].chunks(ofCount: 1).map(concatCharacters)
|
||||
"ABCDE|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_signal_equalChunks() {
|
||||
validate {
|
||||
"ABC- DEF- GHI- |"
|
||||
|
||||
@@ -84,7 +84,7 @@ final class TestCombineLatest2: XCTestCase {
|
||||
value = await validator.validate()
|
||||
XCTAssertEqual(value, [(1, "a"), (2, "a"), (2, "b"), (3, "b"), (3, "c")])
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
value = validator.current
|
||||
XCTAssertEqual(value, [(1, "a"), (2, "a"), (2, "b"), (3, "b"), (3, "c")])
|
||||
}
|
||||
@@ -126,7 +126,7 @@ final class TestCombineLatest2: XCTestCase {
|
||||
value = await validator.validate()
|
||||
XCTAssertEqual(value, [(1, "a"), (1, "b"), (2, "b"), (2, "c"), (3, "c")])
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
value = validator.current
|
||||
XCTAssertEqual(value, [(1, "a"), (1, "b"), (2, "b"), (2, "c"), (3, "c")])
|
||||
}
|
||||
@@ -168,7 +168,7 @@ final class TestCombineLatest2: XCTestCase {
|
||||
value = await validator.validate()
|
||||
XCTAssertEqual(value, [(1, "a"), (2, "a"), (3, "a"), (3, "b"), (3, "c")])
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
value = validator.current
|
||||
XCTAssertEqual(value, [(1, "a"), (2, "a"), (3, "a"), (3, "b"), (3, "c")])
|
||||
}
|
||||
@@ -210,7 +210,7 @@ final class TestCombineLatest2: XCTestCase {
|
||||
value = await validator.validate()
|
||||
XCTAssertEqual(value, [(1, "a"), (1, "b"), (1, "c"), (2, "c"), (3, "c")])
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
value = validator.current
|
||||
XCTAssertEqual(value, [(1, "a"), (1, "b"), (1, "c"), (2, "c"), (3, "c")])
|
||||
}
|
||||
@@ -250,7 +250,7 @@ final class TestCombineLatest2: XCTestCase {
|
||||
|
||||
XCTAssertEqual(validator.failure as? Failure, Failure())
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
value = validator.current
|
||||
XCTAssertEqual(value, [(1, "a"), (1, "b")])
|
||||
}
|
||||
@@ -290,7 +290,7 @@ final class TestCombineLatest2: XCTestCase {
|
||||
|
||||
XCTAssertEqual(validator.failure as? Failure, Failure())
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
value = validator.current
|
||||
XCTAssertEqual(value, [(1, "a"), (2, "a")])
|
||||
}
|
||||
@@ -312,12 +312,22 @@ final class TestCombineLatest2: XCTestCase {
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
|
||||
func test_combineLatest_when_cancelled() async {
|
||||
let t = Task {
|
||||
try? await Task.sleep(nanoseconds: 1_000_000_000)
|
||||
let c1 = Indefinite(value: "test1").async
|
||||
let c2 = Indefinite(value: "test1").async
|
||||
for await _ in combineLatest(c1, c2) {}
|
||||
}
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
final class TestCombineLatest3: XCTestCase {
|
||||
@@ -379,7 +389,7 @@ final class TestCombineLatest3: XCTestCase {
|
||||
value = await validator.validate()
|
||||
XCTAssertEqual(value, [(1, "a", 4), (2, "a", 4), (2, "b", 4), (2, "b", 5), (3, "b", 5), (3, "c", 5), (3, "c", 6)])
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
value = validator.current
|
||||
XCTAssertEqual(value, [(1, "a", 4), (2, "a", 4), (2, "b", 4), (2, "b", 5), (3, "b", 5), (3, "c", 5), (3, "c", 6)])
|
||||
}
|
||||
|
||||
@@ -83,10 +83,10 @@ final class TestCompacted: XCTestCase {
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
@@ -68,4 +68,15 @@ final class TestDebounce: XCTestCase {
|
||||
let throwingDebounce = [1].async.map { try throwOn(2, $0) }.debounce(for: .zero, clock: ContinuousClock())
|
||||
for try await _ in throwingDebounce {}
|
||||
}
|
||||
|
||||
func test_debounce_when_cancelled() async throws {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
|
||||
let t = Task {
|
||||
try? await Task.sleep(nanoseconds: 1_000_000_000)
|
||||
let c1 = Indefinite(value: "test1").async
|
||||
for await _ in c1.debounce(for: .seconds(1), clock: .continuous) {}
|
||||
}
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
@@ -1,91 +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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import AsyncAlgorithms
|
||||
|
||||
final class TestInterspersed: XCTestCase {
|
||||
func test_interspersed() async {
|
||||
let source = [1, 2, 3, 4, 5]
|
||||
let expected = [1, 0, 2, 0, 3, 0, 4, 0, 5]
|
||||
let sequence = source.async.interspersed(with: 0)
|
||||
var actual = [Int]()
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
while let item = await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_empty() async {
|
||||
let source = [Int]()
|
||||
let expected = [Int]()
|
||||
let sequence = source.async.interspersed(with: 0)
|
||||
var actual = [Int]()
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
while let item = await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_with_throwing_upstream() async {
|
||||
let source = [1, 2, 3, -1, 4, 5]
|
||||
let expected = [1, 0, 2, 0, 3, 0]
|
||||
var actual = [Int]()
|
||||
let sequence = source.async.map {
|
||||
try throwOn(-1, $0)
|
||||
}.interspersed(with: 0)
|
||||
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
do {
|
||||
while let item = try await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
XCTFail()
|
||||
} catch {
|
||||
XCTAssertEqual(Failure(), error as? Failure)
|
||||
}
|
||||
let pastEnd = try! await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_cancellation() async {
|
||||
let source = Indefinite(value: "test")
|
||||
let sequence = source.async.interspersed(with: "sep")
|
||||
let finished = expectation(description: "finished")
|
||||
let iterated = expectation(description: "iterated")
|
||||
let task = Task {
|
||||
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
let _ = await iterator.next()
|
||||
iterated.fulfill()
|
||||
|
||||
while let _ = await iterator.next() { }
|
||||
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,8 @@ import XCTest
|
||||
import AsyncAlgorithms
|
||||
|
||||
extension Sequence where Element: Sequence, Element.Element: Equatable & Sendable {
|
||||
func nestedAsync(throwsOn bad: Element.Element) -> AsyncLazySequence<[AsyncThrowingMapSequence<AsyncLazySequence<Element>,Element.Element>]> {
|
||||
let array: [AsyncThrowingMapSequence<AsyncLazySequence<Element>,Element.Element>] = self.map { $0.async }.map {
|
||||
func nestedAsync(throwsOn bad: Element.Element) -> AsyncSyncSequence<[AsyncThrowingMapSequence<AsyncSyncSequence<Element>,Element.Element>]> {
|
||||
let array: [AsyncThrowingMapSequence<AsyncSyncSequence<Element>,Element.Element>] = self.map { $0.async }.map {
|
||||
$0.map { try throwOn(bad, $0) }
|
||||
}
|
||||
return array.async
|
||||
@@ -22,7 +22,7 @@ extension Sequence where Element: Sequence, Element.Element: Equatable & Sendabl
|
||||
}
|
||||
|
||||
extension Sequence where Element: Sequence, Element.Element: Sendable {
|
||||
var nestedAsync : AsyncLazySequence<[AsyncLazySequence<Element>]> {
|
||||
var nestedAsync : AsyncSyncSequence<[AsyncSyncSequence<Element>]> {
|
||||
return self.map { $0.async }.async
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@ final class TestJoinedBySeparator: XCTestCase {
|
||||
}
|
||||
|
||||
func test_join_empty() async {
|
||||
let sequences = [AsyncLazySequence<[Int]>]().async
|
||||
let sequences = [AsyncSyncSequence<[Int]>]().async
|
||||
var iterator = sequences.joined(separator: [-1, -2, -3].async).makeAsyncIterator()
|
||||
let expected = [Int]()
|
||||
var actual = [Int]()
|
||||
@@ -105,7 +105,7 @@ final class TestJoinedBySeparator: XCTestCase {
|
||||
}
|
||||
|
||||
func test_cancellation() async {
|
||||
let source : AsyncLazySequence<[AsyncLazySequence<Indefinite<String>>]> = [Indefinite(value: "test").async].async
|
||||
let source : AsyncSyncSequence<[AsyncSyncSequence<Indefinite<String>>]> = [Indefinite(value: "test").async].async
|
||||
let sequence = source.joined(separator: ["past indefinite"].async)
|
||||
let finished = expectation(description: "finished")
|
||||
let iterated = expectation(description: "iterated")
|
||||
@@ -122,11 +122,11 @@ final class TestJoinedBySeparator: XCTestCase {
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ final class TestJoined: XCTestCase {
|
||||
}
|
||||
|
||||
func test_join_empty() async {
|
||||
let sequences = [AsyncLazySequence<[Int]>]().async
|
||||
let sequences = [AsyncSyncSequence<[Int]>]().async
|
||||
var iterator = sequences.joined().makeAsyncIterator()
|
||||
let expected = [Int]()
|
||||
var actual = [Int]()
|
||||
@@ -189,7 +189,7 @@ final class TestJoined: XCTestCase {
|
||||
}
|
||||
|
||||
func test_cancellation() async {
|
||||
let source : AsyncLazySequence<[AsyncLazySequence<Indefinite<String>>]> = [Indefinite(value: "test").async].async
|
||||
let source : AsyncSyncSequence<[AsyncSyncSequence<Indefinite<String>>]> = [Indefinite(value: "test").async].async
|
||||
let sequence = source.joined()
|
||||
let finished = expectation(description: "finished")
|
||||
let iterated = expectation(description: "iterated")
|
||||
@@ -206,10 +206,10 @@ final class TestJoined: XCTestCase {
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
@@ -121,12 +121,12 @@ final class TestLazy: XCTestCase {
|
||||
}
|
||||
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ final class TestManualClock: XCTestCase {
|
||||
clock.advance()
|
||||
XCTAssertFalse(state.withCriticalRegion { $0 })
|
||||
clock.advance()
|
||||
wait(for: [afterSleep], timeout: 1.0)
|
||||
await fulfillment(of: [afterSleep], timeout: 1.0)
|
||||
XCTAssertTrue(state.withCriticalRegion { $0 })
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ final class TestManualClock: XCTestCase {
|
||||
XCTAssertFalse(state.withCriticalRegion { $0 })
|
||||
clock.advance()
|
||||
task.cancel()
|
||||
wait(for: [afterSleep], timeout: 1.0)
|
||||
await fulfillment(of: [afterSleep], timeout: 1.0)
|
||||
XCTAssertTrue(state.withCriticalRegion { $0 })
|
||||
XCTAssertTrue(failure.withCriticalRegion { $0 is CancellationError })
|
||||
}
|
||||
@@ -73,7 +73,7 @@ final class TestManualClock: XCTestCase {
|
||||
}
|
||||
XCTAssertFalse(state.withCriticalRegion { $0 })
|
||||
task.cancel()
|
||||
wait(for: [afterSleep], timeout: 1.0)
|
||||
await fulfillment(of: [afterSleep], timeout: 1.0)
|
||||
XCTAssertTrue(state.withCriticalRegion { $0 })
|
||||
XCTAssertTrue(failure.withCriticalRegion { $0 is CancellationError })
|
||||
}
|
||||
|
||||
@@ -185,12 +185,22 @@ final class TestMerge2: XCTestCase {
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
|
||||
func test_merge_when_cancelled() async {
|
||||
let t = Task {
|
||||
try? await Task.sleep(nanoseconds: 1_000_000_000)
|
||||
let c1 = Indefinite(value: "test1").async
|
||||
let c2 = Indefinite(value: "test1").async
|
||||
for await _ in merge(c1, c2) {}
|
||||
}
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
final class TestMerge3: XCTestCase {
|
||||
@@ -499,11 +509,11 @@ final class TestMerge3: XCTestCase {
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
|
||||
// MARK: - IteratorInitialized
|
||||
|
||||
@@ -217,10 +217,10 @@ final class TestReductions: XCTestCase {
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
@@ -95,10 +95,10 @@ final class TestRemoveDuplicates: XCTestCase {
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(0), clock: $0.clock)
|
||||
$0.inputs[0]._throttle(for: .steps(0), clock: $0.clock)
|
||||
"abcdefghijk|"
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(0), clock: $0.clock, latest: false)
|
||||
$0.inputs[0]._throttle(for: .steps(0), clock: $0.clock, latest: false)
|
||||
"abcdefghijk|"
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock)
|
||||
$0.inputs[0]._throttle(for: .steps(1), clock: $0.clock)
|
||||
"abcdefghijk|"
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock, latest: false)
|
||||
$0.inputs[0]._throttle(for: .steps(1), clock: $0.clock, latest: false)
|
||||
"abcdefghijk|"
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
|
||||
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
|
||||
"a-c-e-g-i-k|"
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
|
||||
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock, latest: false)
|
||||
"a-b-d-f-h-j|"
|
||||
}
|
||||
}
|
||||
@@ -71,8 +71,8 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
|
||||
"a--d--g--j-|"
|
||||
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock)
|
||||
"a--d--g--j--[k|]"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,8 +80,8 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: false)
|
||||
"a--b--e--h-|"
|
||||
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock, latest: false)
|
||||
"a--b--e--h--[k|]"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdef^hijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
|
||||
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
|
||||
"a-c-e-^"
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdef^hijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
|
||||
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock, latest: false)
|
||||
"a-b-d-^"
|
||||
}
|
||||
}
|
||||
@@ -107,7 +107,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"-a-b-c-d-e-f-g-h-i-j-k-|"
|
||||
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock)
|
||||
$0.inputs[0]._throttle(for: .steps(1), clock: $0.clock)
|
||||
"-a-b-c-d-e-f-g-h-i-j-k-|"
|
||||
}
|
||||
}
|
||||
@@ -116,7 +116,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"-a-b-c-d-e-f-g-h-i-j-k-|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
|
||||
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
|
||||
"-a-b-c-d-e-f-g-h-i-j-k-|"
|
||||
}
|
||||
}
|
||||
@@ -125,7 +125,7 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"--a--b--c--d--e--f--g|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
|
||||
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
|
||||
"--a--b--c--d--e--f--g|"
|
||||
}
|
||||
}
|
||||
@@ -134,8 +134,26 @@ final class TestThrottle: XCTestCase {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"-a-b-c-d-e-f-g-h-i-j-k-|"
|
||||
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
|
||||
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock)
|
||||
"-a---c---e---g---i---k-|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_trailing_delay_without_latest() throws {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdefghijkl|"
|
||||
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock, latest: false)
|
||||
"a--b--e--h--[k|]"
|
||||
}
|
||||
}
|
||||
|
||||
func test_trailing_delay_with_latest() throws {
|
||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
||||
validate {
|
||||
"abcdefghijkl|"
|
||||
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock, latest: true)
|
||||
"a--d--g--j--[l|]"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 XCTest
|
||||
import AsyncAlgorithms
|
||||
|
||||
final class TestThrowingChannel: XCTestCase {
|
||||
func test_asyncThrowingChannel_delivers_elements_when_several_producers_and_several_consumers() async throws {
|
||||
let sents = (1...10)
|
||||
let expected = Set(sents)
|
||||
|
||||
// Given: an AsyncThrowingChannel
|
||||
let sut = AsyncThrowingChannel<Int, Error>()
|
||||
|
||||
// When: sending elements from tasks in a group
|
||||
Task {
|
||||
await withTaskGroup(of: Void.self) { group in
|
||||
for sent in sents {
|
||||
group.addTask {
|
||||
await sut.send(sent)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// When: receiving those elements from tasks in a group
|
||||
let collected = try await withThrowingTaskGroup(of: Int.self, returning: Set<Int>.self) { group in
|
||||
for _ in sents {
|
||||
group.addTask {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
let received = try await iterator.next()
|
||||
return received!
|
||||
}
|
||||
}
|
||||
|
||||
var collected = Set<Int>()
|
||||
for try await element in group {
|
||||
collected.update(with: element)
|
||||
}
|
||||
return collected
|
||||
}
|
||||
|
||||
// Then: all elements are sent and received
|
||||
XCTAssertEqual(collected, expected)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_resumes_producers_and_discards_additional_elements_when_finish_is_called() async throws {
|
||||
// Given: an AsyncThrowingChannel
|
||||
let sut = AsyncThrowingChannel<Int, Error>()
|
||||
|
||||
// Given: 2 suspended send operations
|
||||
let task1 = Task {
|
||||
await sut.send(1)
|
||||
}
|
||||
|
||||
let task2 = Task {
|
||||
await sut.send(2)
|
||||
}
|
||||
|
||||
// When: finishing the channel
|
||||
sut.finish()
|
||||
|
||||
// Then: the send operations are resumed
|
||||
_ = await (task1.value, task2.value)
|
||||
|
||||
// When: sending an extra value
|
||||
await sut.send(3)
|
||||
|
||||
// Then: the operation and the iteration are immediately resumed
|
||||
var collected = [Int]()
|
||||
for try await element in sut {
|
||||
collected.append(element)
|
||||
}
|
||||
XCTAssertTrue(collected.isEmpty)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_resumes_producers_and_discards_additional_elements_when_fail_is_called() async throws {
|
||||
// Given: an AsyncThrowingChannel
|
||||
let sut = AsyncThrowingChannel<Int, Error>()
|
||||
|
||||
// Given: 2 suspended send operations
|
||||
let task1 = Task {
|
||||
await sut.send(1)
|
||||
}
|
||||
|
||||
let task2 = Task {
|
||||
await sut.send(2)
|
||||
}
|
||||
|
||||
// When: failing the channel
|
||||
sut.fail(Failure())
|
||||
|
||||
// Then: the send operations are resumed
|
||||
_ = await (task1.value, task2.value)
|
||||
|
||||
// When: sending an extra value
|
||||
await sut.send(3)
|
||||
|
||||
// Then: the send operation is resumed
|
||||
// Then: the iteration is resumed with a failure
|
||||
var collected = [Int]()
|
||||
do {
|
||||
for try await element in sut {
|
||||
collected.append(element)
|
||||
}
|
||||
} catch {
|
||||
XCTAssertTrue(collected.isEmpty)
|
||||
XCTAssertEqual(error as? Failure, Failure())
|
||||
}
|
||||
|
||||
// When: requesting a next value
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
let pastFailure = try await iterator.next()
|
||||
|
||||
// Then: the past failure is nil
|
||||
XCTAssertNil(pastFailure)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_resumes_consumers_when_finish_is_called() async throws {
|
||||
// Given: an AsyncThrowingChannel
|
||||
let sut = AsyncThrowingChannel<Int, Error>()
|
||||
|
||||
// Given: 2 suspended iterations
|
||||
let task1 = Task<Int?, Error> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
return try await iterator.next()
|
||||
}
|
||||
|
||||
let task2 = Task<Int?, Error> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
return try await iterator.next()
|
||||
}
|
||||
|
||||
|
||||
// When: finishing the channel
|
||||
sut.finish()
|
||||
|
||||
// Then: the iterations are resumed with nil values
|
||||
let (collected1, collected2) = try await (task1.value, task2.value)
|
||||
XCTAssertNil(collected1)
|
||||
XCTAssertNil(collected2)
|
||||
|
||||
// When: requesting a next value
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
let pastEnd = try await iterator.next()
|
||||
|
||||
// Then: the past end is nil
|
||||
XCTAssertNil(pastEnd)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_resumes_consumer_when_fail_is_called() async throws {
|
||||
// Given: an AsyncThrowingChannel
|
||||
let sut = AsyncThrowingChannel<Int, Error>()
|
||||
|
||||
// Given: suspended iteration
|
||||
let task = Task<Int?, Error> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
|
||||
do {
|
||||
_ = try await iterator.next()
|
||||
XCTFail("We expect the above call to throw")
|
||||
} catch {
|
||||
XCTAssertEqual(error as? Failure, Failure())
|
||||
}
|
||||
|
||||
return try await iterator.next()
|
||||
}
|
||||
|
||||
// When: failing the channel
|
||||
sut.fail(Failure())
|
||||
|
||||
// Then: the iterations are resumed with the error and the next element is nil
|
||||
do {
|
||||
let collected = try await task.value
|
||||
XCTAssertNil(collected)
|
||||
} catch {
|
||||
XCTFail("The task should not fail, the past failure element should be nil, not a failure.")
|
||||
}
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_resumes_consumers_when_fail_is_called() async throws {
|
||||
// Given: an AsyncThrowingChannel
|
||||
let sut = AsyncThrowingChannel<Int, Error>()
|
||||
|
||||
// Given: 2 suspended iterations
|
||||
let task1 = Task<Int?, Error> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
return try await iterator.next()
|
||||
}
|
||||
|
||||
let task2 = Task<Int?, Error> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
return try await iterator.next()
|
||||
}
|
||||
|
||||
// When: failing the channel
|
||||
sut.fail(Failure())
|
||||
|
||||
// Then: the iterations are resumed with the error
|
||||
do {
|
||||
_ = try await task1.value
|
||||
} catch {
|
||||
XCTAssertEqual(error as? Failure, Failure())
|
||||
}
|
||||
|
||||
do {
|
||||
_ = try await task2.value
|
||||
} catch {
|
||||
XCTAssertEqual(error as? Failure, Failure())
|
||||
}
|
||||
|
||||
// When: requesting a next value
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
let pastFailure = try await iterator.next()
|
||||
|
||||
// Then: the past failure is nil
|
||||
XCTAssertNil(pastFailure)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_resumes_consumer_with_error_when_already_failed() async throws {
|
||||
// Given: an AsyncThrowingChannel that is failed
|
||||
let sut = AsyncThrowingChannel<Int, Error>()
|
||||
sut.fail(Failure())
|
||||
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
|
||||
// When: requesting the next element
|
||||
do {
|
||||
_ = try await iterator.next()
|
||||
} catch {
|
||||
// Then: the iteration is resumed with the error
|
||||
XCTAssertEqual(error as? Failure, Failure())
|
||||
}
|
||||
|
||||
// When: requesting the next element past failure
|
||||
do {
|
||||
let pastFailure = try await iterator.next()
|
||||
// Then: the past failure is nil
|
||||
XCTAssertNil(pastFailure)
|
||||
} catch {
|
||||
XCTFail("The past failure should not throw")
|
||||
}
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_resumes_producer_when_task_is_cancelled() async throws {
|
||||
let send1IsResumed = expectation(description: "The first send operation is resumed")
|
||||
|
||||
// Given: an AsyncThrowingChannel
|
||||
let sut = AsyncThrowingChannel<Int, Error>()
|
||||
|
||||
// Given: 2 suspended send operations
|
||||
let task1 = Task {
|
||||
await sut.send(1)
|
||||
send1IsResumed.fulfill()
|
||||
}
|
||||
|
||||
let task2 = Task {
|
||||
await sut.send(2)
|
||||
}
|
||||
|
||||
// When: cancelling the first task
|
||||
task1.cancel()
|
||||
|
||||
// Then: the first sending operation is resumed
|
||||
await fulfillment(of: [send1IsResumed], timeout: 1.0)
|
||||
|
||||
// When: collecting elements
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
let collected = try await iterator.next()
|
||||
|
||||
// Then: the second operation resumes and the iteration receives the element
|
||||
_ = await task2.value
|
||||
XCTAssertEqual(collected, 2)
|
||||
}
|
||||
|
||||
func test_asyncThrowingChannel_resumes_consumer_when_task_is_cancelled() async throws {
|
||||
// Given: an AsyncThrowingChannel
|
||||
let sut = AsyncThrowingChannel<Int, Error>()
|
||||
|
||||
// Given: 2 suspended iterations
|
||||
let task1 = Task<Int?, Error> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
return try await iterator.next()
|
||||
}
|
||||
|
||||
let task2 = Task<Int?, Error> {
|
||||
var iterator = sut.makeAsyncIterator()
|
||||
return try await iterator.next()
|
||||
}
|
||||
|
||||
// When: cancelling the first task
|
||||
task1.cancel()
|
||||
|
||||
// Then: the first iteration is resumed with a nil element
|
||||
let collected1 = try await task1.value
|
||||
XCTAssertNil(collected1)
|
||||
|
||||
// When: sending an element
|
||||
await sut.send(1)
|
||||
|
||||
// Then: the second iteration is resumed with the element
|
||||
let collected2 = try await task2.value
|
||||
XCTAssertEqual(collected2, 1)
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ final class TestValidator: XCTestCase {
|
||||
}
|
||||
XCTAssertFalse(state.withCriticalRegion { $0 })
|
||||
gate.open()
|
||||
wait(for: [entered], timeout: 1.0)
|
||||
await fulfillment(of: [entered], timeout: 1.0)
|
||||
XCTAssertTrue(state.withCriticalRegion { $0 })
|
||||
}
|
||||
|
||||
@@ -52,18 +52,18 @@ final class TestValidator: XCTestCase {
|
||||
}
|
||||
finished.fulfill()
|
||||
}
|
||||
wait(for: [started], timeout: 1.0)
|
||||
await fulfillment(of: [started], timeout: 1.0)
|
||||
XCTAssertEqual(state.withCriticalRegion { $0 }, [])
|
||||
gated.advance()
|
||||
wait(for: [expectations[0]], timeout: 1.0)
|
||||
await fulfillment(of: [expectations[0]], timeout: 1.0)
|
||||
XCTAssertEqual(state.withCriticalRegion { $0 }, [1])
|
||||
gated.advance()
|
||||
wait(for: [expectations[1]], timeout: 1.0)
|
||||
await fulfillment(of: [expectations[1]], timeout: 1.0)
|
||||
XCTAssertEqual(state.withCriticalRegion { $0 }, [1, 2])
|
||||
gated.advance()
|
||||
wait(for: [expectations[2]], timeout: 1.0)
|
||||
await fulfillment(of: [expectations[2]], timeout: 1.0)
|
||||
XCTAssertEqual(state.withCriticalRegion { $0 }, [1, 2, 3])
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
|
||||
func test_gatedSequence_throwing() async {
|
||||
@@ -93,14 +93,14 @@ final class TestValidator: XCTestCase {
|
||||
}
|
||||
finished.fulfill()
|
||||
}
|
||||
wait(for: [started], timeout: 1.0)
|
||||
await fulfillment(of: [started], timeout: 1.0)
|
||||
XCTAssertEqual(state.withCriticalRegion { $0 }, [])
|
||||
gated.advance()
|
||||
wait(for: [expectations[0]], timeout: 1.0)
|
||||
await fulfillment(of: [expectations[0]], timeout: 1.0)
|
||||
XCTAssertEqual(state.withCriticalRegion { $0 }, [1])
|
||||
gated.advance()
|
||||
XCTAssertEqual(state.withCriticalRegion { $0 }, [1])
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
XCTAssertEqual(state.withCriticalRegion { $0 }, [1])
|
||||
XCTAssertEqual(failure.withCriticalRegion { $0 as? Failure }, Failure())
|
||||
}
|
||||
@@ -131,7 +131,7 @@ final class TestValidator: XCTestCase {
|
||||
XCTAssertEqual(value, [2, 3, 4])
|
||||
a.advance()
|
||||
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
value = validator.current
|
||||
XCTAssertEqual(value, [2, 3, 4])
|
||||
}
|
||||
|
||||
@@ -152,11 +152,21 @@ final class TestZip2: XCTestCase {
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
|
||||
func test_zip_when_cancelled() async {
|
||||
let t = Task {
|
||||
try? await Task.sleep(nanoseconds: 1_000_000_000)
|
||||
let c1 = Indefinite(value: "test1").async
|
||||
let c2 = Indefinite(value: "test1").async
|
||||
for await _ in zip(c1, c2) {}
|
||||
}
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -360,10 +370,10 @@ final class TestZip3: XCTestCase {
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
await fulfillment(of: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
await fulfillment(of: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
Loaded 100 of 106 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user