mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-26 04:37:48 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
963eecf5ee | ||
|
|
088f92a30c | ||
|
|
74a48bddc6 | ||
|
|
ba5872bfcb | ||
|
|
cbde9aed74 | ||
|
|
d9c26817af | ||
|
|
822cfb8331 |
No files matched your search
@@ -38,7 +38,7 @@ for await (first, second) in (1...5).async.adjacentPairs() {
|
|||||||
// First: 4, Second: 5
|
// First: 4, Second: 5
|
||||||
```
|
```
|
||||||
|
|
||||||
It composes well with the [Dictionary.init(_:uniquingKeysWith:)](https://github.com/apple/swift-async-algorithms/blob/main/Guides/Collections.md) API that deals with `AsyncSequence` of tuples.
|
It composes well with the [Dictionary.init(_:uniquingKeysWith:)](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncAlgorithms.docc/Guides/Collections.md) API that deals with `AsyncSequence` of tuples.
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
Dictionary(uniqueKeysWithValues: url.lines.adjacentPairs())
|
Dictionary(uniqueKeysWithValues: url.lines.adjacentPairs())
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
* Author(s): [Philippe Hausler](https://github.com/phausler)
|
* Author(s): [Philippe Hausler](https://github.com/phausler)
|
||||||
|
|
||||||
[
|
[
|
||||||
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChannel.swift),
|
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Channels/AsyncChannel.swift),
|
||||||
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncThrowingChannel.swift) |
|
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Channels/AsyncThrowingChannel.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChannel.swift)
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChannel.swift)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
* Implementation: [
|
* Implementation: [
|
||||||
[By group](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChunkedByGroupSequence.swift),
|
[By group](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChunkedByGroupSequence.swift),
|
||||||
[On projection](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChunkedOnProjectionSequence.swift),
|
[On projection](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChunkedOnProjectionSequence.swift),
|
||||||
[Count and signal](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChunksOfCountAndSignalSequence.swift)
|
[Count and signal](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChunksOfCountOrSignalSequence.swift)
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChunk.swift)
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChunk.swift)
|
||||||
]
|
]
|
||||||
[
|
[
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
* Status: **Accepted**
|
* Status: **Accepted**
|
||||||
* Implementation:
|
* Implementation:
|
||||||
[
|
[
|
||||||
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncDebounceSequence.swift) |
|
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Debounce/AsyncDebounceSequence.swift) |
|
||||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestDebounce.swift)
|
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestDebounce.swift)
|
||||||
]
|
]
|
||||||
[
|
[
|
||||||
|
|||||||
+1
-1
@@ -14,7 +14,7 @@ let AsyncAlgorithms_v1_3 =
|
|||||||
#else
|
#else
|
||||||
let AsyncAlgorithms_v1_1 = "AvailabilityMacro=AsyncAlgorithms 1.1:macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0"
|
let AsyncAlgorithms_v1_1 = "AvailabilityMacro=AsyncAlgorithms 1.1:macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0"
|
||||||
let AsyncAlgorithms_v1_2 = "AvailabilityMacro=AsyncAlgorithms 1.2:macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0"
|
let AsyncAlgorithms_v1_2 = "AvailabilityMacro=AsyncAlgorithms 1.2:macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0"
|
||||||
let AsyncAlgorithms_v1_3 = "AvailabilityMacro=AsyncAlgorithms 1.2:macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0"
|
let AsyncAlgorithms_v1_3 = "AvailabilityMacro=AsyncAlgorithms 1.3:macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
let availabilityMacros: [SwiftSetting] = [
|
let availabilityMacros: [SwiftSetting] = [
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ for await (first, second) in (1...5).async.adjacentPairs() {
|
|||||||
// First: 4, Second: 5
|
// First: 4, Second: 5
|
||||||
```
|
```
|
||||||
|
|
||||||
It composes well with the [Dictionary.init(_:uniquingKeysWith:)](https://github.com/apple/swift-async-algorithms/blob/main/Guides/Collections.md) API that deals with `AsyncSequence` of tuples.
|
It composes well with the [Dictionary.init(_:uniquingKeysWith:)](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncAlgorithms.docc/Guides/Collections.md) API that deals with `AsyncSequence` of tuples.
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
Dictionary(uniqueKeysWithValues: url.lines.adjacentPairs())
|
Dictionary(uniqueKeysWithValues: url.lines.adjacentPairs())
|
||||||
|
|||||||
@@ -399,9 +399,9 @@ struct DebounceStateMachine<Base: AsyncSequence & Sendable, C: Clock>: Sendable
|
|||||||
return .none
|
return .none
|
||||||
|
|
||||||
case .waitingForDemand(let task, .none, let clockContinuation, .none):
|
case .waitingForDemand(let task, .none, let clockContinuation, .none):
|
||||||
// We don't have any buffered element so we can just go ahead
|
// We don't have outstanding demand to deliver the error to, so store it
|
||||||
// and transition to finished and cancel everything
|
// until the downstream calls next and cancel everything.
|
||||||
self.state = .finished
|
self.state = .upstreamFailure(error: error)
|
||||||
|
|
||||||
return .cancelTaskAndClockContinuation(
|
return .cancelTaskAndClockContinuation(
|
||||||
task: task,
|
task: task,
|
||||||
|
|||||||
+4
-4
@@ -37,13 +37,13 @@ public struct MultiProducerSingleConsumerAsyncChannelAlreadyFinishedError: Error
|
|||||||
/// Afterwards, you can pass the source to the producer and the channel to the consumer.
|
/// Afterwards, you can pass the source to the producer and the channel to the consumer.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let channelAndSource = MultiProducerSingleConsumerAsyncChannel.makeChannel(
|
/// var channelAndSource = MultiProducerSingleConsumerAsyncChannel.makeChannel(
|
||||||
/// of: Int.self,
|
/// of: Int.self,
|
||||||
/// backpressureStrategy: .watermark(low: 2, high: 4)
|
/// backpressureStrategy: .watermark(low: 2, high: 4)
|
||||||
/// )
|
/// )
|
||||||
///
|
///
|
||||||
/// // The channel and source can be extracted from the returned type
|
/// // The channel and source can be extracted from the returned type
|
||||||
/// let channel = consume channelAndSource.channel
|
/// let channel = channelAndSource.takeChannel()
|
||||||
/// let source = consume channelAndSource.source
|
/// let source = consume channelAndSource.source
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@@ -127,11 +127,11 @@ public struct MultiProducerSingleConsumerAsyncChannel<Element, Failure: Error>:
|
|||||||
/// components from it.
|
/// components from it.
|
||||||
///
|
///
|
||||||
/// ```swift
|
/// ```swift
|
||||||
/// let channelAndSource = MultiProducerSingleConsumerAsyncChannel.makeChannel(
|
/// var channelAndSource = MultiProducerSingleConsumerAsyncChannel.makeChannel(
|
||||||
/// of: Int.self,
|
/// of: Int.self,
|
||||||
/// backpressureStrategy: .watermark(low: 5, high: 10)
|
/// backpressureStrategy: .watermark(low: 5, high: 10)
|
||||||
/// )
|
/// )
|
||||||
/// var channel = consume channelAndSource.channel
|
/// var channel = channelAndSource.takeChannel()
|
||||||
/// var source = consume channelAndSource.source
|
/// var source = consume channelAndSource.source
|
||||||
/// ```
|
/// ```
|
||||||
@frozen
|
@frozen
|
||||||
|
|||||||
@@ -386,7 +386,9 @@ extension DuplexAsyncChannel {
|
|||||||
buffer: inout Buffer,
|
buffer: inout Buffer,
|
||||||
finalElement: consuming FinalElement
|
finalElement: consuming FinalElement
|
||||||
) async throws {
|
) async throws {
|
||||||
try await self.write(buffer: &buffer)
|
if !buffer.isEmpty {
|
||||||
|
try await self.write(buffer: &buffer)
|
||||||
|
}
|
||||||
self._storage.finish(throwing: nil, finalElement: finalElement)
|
self._storage.finish(throwing: nil, finalElement: finalElement)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,10 @@
|
|||||||
import XCTest
|
import XCTest
|
||||||
import AsyncAlgorithms
|
import AsyncAlgorithms
|
||||||
|
|
||||||
|
private struct DebounceTestError: Error, Equatable, Sendable {
|
||||||
|
let value: Int
|
||||||
|
}
|
||||||
|
|
||||||
final class TestDebounce: XCTestCase {
|
final class TestDebounce: XCTestCase {
|
||||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
|
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
|
||||||
func test_delayingValues() throws {
|
func test_delayingValues() throws {
|
||||||
@@ -58,6 +62,72 @@ final class TestDebounce: XCTestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func test_upstreamFailureWithoutOutstandingDemand() 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 {
|
||||||
|
"a^"
|
||||||
|
$0.inputs[0].debounce(for: .steps(0), clock: $0.clock)
|
||||||
|
"a,,,^"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_upstreamFailureWithoutElementsPreservesError() 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 expectedError = DebounceTestError(value: 1)
|
||||||
|
let (stream, source) = AsyncThrowingStream<Int, Error>.makeStream()
|
||||||
|
source.finish(throwing: expectedError)
|
||||||
|
|
||||||
|
var iterator = stream.debounce(for: .milliseconds(10), clock: .continuous).makeAsyncIterator()
|
||||||
|
do {
|
||||||
|
_ = try await iterator.next()
|
||||||
|
XCTFail("Expected the upstream error")
|
||||||
|
} catch let error as DebounceTestError {
|
||||||
|
XCTAssertEqual(error, expectedError)
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = try await iterator.next()
|
||||||
|
XCTAssertNil(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_upstreamFailureAfterElementAndWithoutDemandPreservesError() 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 expectedError = DebounceTestError(value: 2)
|
||||||
|
let (stream, source) = AsyncThrowingStream<Int, Error>.makeStream()
|
||||||
|
source.yield(1)
|
||||||
|
let finisher = Task {
|
||||||
|
try? await Task.sleep(for: .milliseconds(100))
|
||||||
|
source.finish(throwing: expectedError)
|
||||||
|
}
|
||||||
|
defer { finisher.cancel() }
|
||||||
|
|
||||||
|
var iterator = stream.debounce(for: .milliseconds(10), clock: .continuous).makeAsyncIterator()
|
||||||
|
let first = try await iterator.next()
|
||||||
|
XCTAssertEqual(first, 1)
|
||||||
|
|
||||||
|
// The upstream fails while the consumer has no outstanding demand.
|
||||||
|
try await Task.sleep(for: .milliseconds(300))
|
||||||
|
|
||||||
|
do {
|
||||||
|
_ = try await iterator.next()
|
||||||
|
XCTFail("Expected the upstream error")
|
||||||
|
} catch let error as DebounceTestError {
|
||||||
|
XCTAssertEqual(error, expectedError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consuming the failure completes the iterator; it must not produce another value
|
||||||
|
// or resume completion more than once.
|
||||||
|
let result = try await iterator.next()
|
||||||
|
XCTAssertNil(result)
|
||||||
|
}
|
||||||
|
|
||||||
func test_noValues() throws {
|
func test_noValues() throws {
|
||||||
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else {
|
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else {
|
||||||
throw XCTSkip("Skipped due to Clock/Instant/Duration availability")
|
throw XCTSkip("Skipped due to Clock/Instant/Duration availability")
|
||||||
|
|||||||
@@ -387,6 +387,93 @@ struct DuplexAsyncChannelTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Finishes `writer` through the `CallerAsyncWriter` protocol, where there is
|
||||||
|
/// no concrete `finish()` overload to pick instead.
|
||||||
|
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
||||||
|
private static func finishThroughProtocol<Writer: CallerAsyncWriter & ~Copyable>(
|
||||||
|
_ writer: consuming Writer
|
||||||
|
) async throws(Writer.WriteFailure) where Writer.FinalElement == Void {
|
||||||
|
try await writer.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
||||||
|
@Test
|
||||||
|
func cloneFinishesThroughGenericCallerAsyncWriterAfterOtherWriterFinished() async throws {
|
||||||
|
try await DuplexAsyncChannel<Int, Void, Never>.withDuplex(
|
||||||
|
of: Int.self,
|
||||||
|
backpressureStrategy: .watermark(low: 5, high: 50)
|
||||||
|
) { writerA, readerA, writerB, readerB in
|
||||||
|
var writerA = writerA
|
||||||
|
var readerB = readerB
|
||||||
|
_ = readerA
|
||||||
|
_ = writerB
|
||||||
|
|
||||||
|
let clone = writerA.clone()
|
||||||
|
var buffer = UniqueArray<Int>(minimumCapacity: 3)
|
||||||
|
for value in 0..<3 { buffer.append(value) }
|
||||||
|
try await writerA.write(buffer: &buffer)
|
||||||
|
|
||||||
|
try await writerA.finish()
|
||||||
|
try await Self.finishThroughProtocol(clone)
|
||||||
|
|
||||||
|
var collected: [Int] = []
|
||||||
|
var done = false
|
||||||
|
while !done {
|
||||||
|
try await readerB.read { buffer, finalElement in
|
||||||
|
var consumer = buffer.consumeAll()
|
||||||
|
while let value = consumer.next() { collected.append(value) }
|
||||||
|
if finalElement != nil { done = true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#expect(collected == [0, 1, 2])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
||||||
|
@Test
|
||||||
|
func cloneFinishesThroughAsyncWriterAdapterAfterOtherWriterFinished() async throws {
|
||||||
|
try await DuplexAsyncChannel<Int, Void, Never>.withDuplex(
|
||||||
|
of: Int.self,
|
||||||
|
backpressureStrategy: .watermark(low: 5, high: 50)
|
||||||
|
) { writerA, readerA, writerB, readerB in
|
||||||
|
var writerA = writerA
|
||||||
|
_ = readerA
|
||||||
|
_ = writerB
|
||||||
|
_ = readerB
|
||||||
|
|
||||||
|
let clone = writerA.clone()
|
||||||
|
try await writerA.finish()
|
||||||
|
try await clone.asAsyncWriter().finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
||||||
|
@Test
|
||||||
|
func cloneFinishWithElementsAfterOtherWriterFinishedStillThrows() async throws {
|
||||||
|
try await DuplexAsyncChannel<Int, Void, Never>.withDuplex(
|
||||||
|
of: Int.self,
|
||||||
|
backpressureStrategy: .watermark(low: 5, high: 50)
|
||||||
|
) { writerA, readerA, writerB, readerB in
|
||||||
|
var writerA = writerA
|
||||||
|
_ = readerA
|
||||||
|
_ = writerB
|
||||||
|
_ = readerB
|
||||||
|
|
||||||
|
let clone = writerA.clone()
|
||||||
|
try await writerA.finish()
|
||||||
|
|
||||||
|
// Elements that can no longer be delivered must not be dropped silently.
|
||||||
|
var buffer = UniqueArray<Int>(minimumCapacity: 1)
|
||||||
|
buffer.append(1)
|
||||||
|
do {
|
||||||
|
try await clone.finish(buffer: &buffer, finalElement: ())
|
||||||
|
Issue.record("Expected error")
|
||||||
|
} catch {
|
||||||
|
#expect(error is MultiProducerSingleConsumerAsyncChannelAlreadyFinishedError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Body-error wrapping
|
// MARK: - Body-error wrapping
|
||||||
|
|
||||||
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
||||||
|
|||||||
Reference in new issue
Block a user