The channel and debounce sources moved into Channels/ and Debounce/,
AsyncChunksOfCountAndSignalSequence.swift is now
AsyncChunksOfCountOrSignalSequence.swift, and the Collections guide
lives in the DocC catalog, so these links return 404.
The `#else` branch declared `AsyncAlgorithms_v1_3` with the macro name
`AsyncAlgorithms 1.2`, so that branch defined `AsyncAlgorithms 1.2` twice
and never defined `AsyncAlgorithms 1.3`.
Issue: https://github.com/apple/swift-async-algorithms/issues/452
The stored channel property of ChannelAndStream is intentionally
internal, so the documented consume channelAndSource.channel snippet
fails to compile for clients. The supported API is takeChannel(); use
it in both doc examples.
Signed-off-by: Aniket <aniketkomailkro@gmail.com>
* [AsyncStreaming] Propagate cancellation to late readers
Distinguish cancellation from normal source completion when a reader continuation is installed after the channel has already finished.
* [AsyncStreaming] Distinguish terminal read states
Track cancellation separately from an already-consumed terminal result so normal source completion does not throw CancellationError. Precondition on repeated reads after cancellation, a final element, or a source failure, with coverage for each terminal path.
* [AsyncStreaming] Handle pending reader cancellation
Deliver cancellation exactly once when it races with a resumed reader, preserve fused final-element completion, and align terminal-read tests and documentation with the reader contract.
* [AsyncStreaming] Test cancellation through public API
Remove the internal-only state-machine test so the regression suite exercises the supported AsyncStreaming interface without @testable import.
Rename 0016-mutli-producer-single-consumer-channel.md to the
correctly spelled 0016-multi-...; the document's own proposal link
already uses the correct spelling and currently 404s. Fill in the
NNNN template placeholder in 0017-map-error.md's proposal link.
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Replace `collect(upTo:body:)` with three target-based overloads: `collect(into:)`, `collect(exactlyInto:)`, and `collect(into:maximumSize:)`. Add `AsyncReaderInsufficientElementsError` for the exact-match shortfall case.
- NNNN-async-streaming.md is intentionally kept alongside the AsyncStreaming
implementation
- Exclude it from the target in Package.swift so SwiftPM no
longer reports it as an unhandled file (#433)
* [AsyncStreaming] Introduce a `MultiProducerSingleConsumerChannel` and a duplex
* Add tests
* Fix UnstableAsyncStreaming test build
---------
Co-authored-by: Franz Busch <f.busch@apple.com>
* [AsyncStreaming] Add support for final elements
## Motivation
Real transports deliver structured data alongside end-of-stream and need to fuse the last write with the close. HTTP trailers and gRPC status are the obvious cases. Neither can be expressed by an empty-buffer terminator, and both lose H2/H3/QUIC's DATA+END_STREAM coalescing without a fused call.
## Modification
Adds `FinalElement: ~Copyable = Void` as a primary associated type on all four protocols. `AsyncReader` delivers it via a `consuming FinalElement?` closure parameter; `CallerAsyncReader` returns it. Both writers gain a consuming `finish` carrying the last chunk and the payload in one call.
`forEachBuffer`, `collect`, and the `pipe` variants thread the payload through. `collect` becomes consuming and gains a `Void`-final overload returning just the result.
Adds an "Alternatives considered" entry covering what would break without
this: HTTP body, gRPC, and fused close on H2/H3/QUIC.
## Result
The four protocols can back HTTP body, gRPC streaming, and similar shapes without giving up the fused close. Default `Void` keeps simple conformers unchanged; `Never` marks infinite streams. Custom and `~Copyable` payloads work end to end.
* [AsyncStreaming] Add bidirectional adapters for the async writers
## Motivation
It is quite common to have one type of writer and wanting to adapt to the other type of writer.
## Modifications
This PR adds two adapters into either direction between the writers to make conversion seamless.
## Result
It is easy to go from one type of writer to the other type.
## Motivation
Real transports deliver structured data alongside end-of-stream and need to fuse the last write with the close. HTTP trailers and gRPC status are the obvious cases. Neither can be expressed by an empty-buffer terminator, and both lose H2/H3/QUIC's DATA+END_STREAM coalescing without a fused call.
## Modification
Adds `FinalElement: ~Copyable = Void` as a primary associated type on all four protocols. `AsyncReader` delivers it via a `consuming FinalElement?` closure parameter; `CallerAsyncReader` returns it. Both writers gain a consuming `finish` carrying the last chunk and the payload in one call.
`forEachBuffer`, `collect`, and the `pipe` variants thread the payload through. `collect` becomes consuming and gains a `Void`-final overload returning just the result.
Adds an "Alternatives considered" entry covering what would break without
this: HTTP body, gRPC, and fused close on H2/H3/QUIC.
## Result
The four protocols can back HTTP body, gRPC streaming, and similar shapes without giving up the fused close. Default `Void` keeps simple conformers unchanged; `Never` marks infinite streams. Custom and `~Copyable` payloads work end to end.
The doc comments for the exclusive reductions overloads stated that the
returned sequence is "the initial value followed by the reduced elements".
That does not match the behavior: the iterator pulls a base element before
emitting anything, so the first emitted element is the result of combining
the initial value with the first base element, and the initial value is
never emitted. For an empty base sequence the result is empty rather than a
single-element sequence containing the initial value.
This is the behavior the existing tests already pin down, and changing it
would be source-breaking, so this corrects the documentation to match.
Updates all four exclusive overloads (throwing and non-throwing, value and
into:) and adds empty-base tests covering the now-documented edge case.
The inclusive reductions and the DocC Reductions guide already describe the
actual behavior and are left unchanged.
## Motivation
Often developers want to pipe the contents of an async reader into a writer. This is a common scenario in asynchronous streaming APIs. Readers are modeling sources of data where writers are modeling sinks. As an example imagine reading from a network socket and writing the contents into a file. Importantly, the entire operation needs to uphold backpressure.
## Modifications
This PR adds 2 new `pipe` methods to each reader one for each writer type. Depending on the buffer ownership those methods will the pipe operation is copy free, requires a copy or even requires an intermediate allocation for copying. The method parameter labels are trying to express what the operation does.
## Result
It is very easy for developers now to move elements from readers into writers.
Replace `InputSpan`/`OutputSpan` with generic `Buffer` associated types constrained to `RangeReplaceableContainer` across all four streaming protocols. This sidesteps the limitation that `OutputSpan` (and other `~Escapable` types) cannot be used in async contexts today, while still allowing conforming types to choose a buffer representation optimized for their use case (e.g. `UniqueArray` for heap-backed storage, or a future stack-allocated container for embedded).
Switch callee-owned protocols from `consuming` to `inout` closure semantics, rename `forEachChunk` to `forEachBuffer`, and move `forEach`/`collect` to future directions in the proposal.
Adds test coverage for `AsyncWriter`, `CallerAsyncReader`, and `EitherError`. Updates all documentation to reflect the new API surface.
* Rework `AsyncStreaming` protocols to use generic `RangeReplaceableContainer` buffers
Replace `InputSpan`/`OutputSpan` with generic `Buffer` associated types constrained to `RangeReplaceableContainer` across all four streaming protocols. This sidesteps the limitation that `OutputSpan` (and other `~Escapable` types) cannot be used in async contexts today, while still allowing conforming types to choose a buffer representation optimized for their use case (e.g. `UniqueArray` for heap-backed storage, or a future stack-allocated container for embedded).
Switch callee-owned protocols from `consuming` to `inout` closure semantics, rename `forEachChunk` to `forEachBuffer`, and move `forEach`/`collect` to future directions in the proposal.
Adds test coverage for `AsyncWriter`, `CallerAsyncReader`, and `EitherError`. Updates all documentation to reflect the new API surface.
* Fix a few new region isolation errors in `nightly-main`
Region isolation got smarter and started diagnosing more potential data races. This PR addresses the new errors. Importantly I had to introduce one `@unchecked Sendable` conformance since we can't yet use `UniqueDeque`
This PR adds an experimental module for asynchronous streaming interfaces as a potential evolution to async sequence. These APIs are considered highly unstable and are mostly for experimentation purposes; hence, they are part of an underscored module and all APIs are hidden behind a trait. I expect significant changes to those APIs to offer more functionality such as gather scatter and more.
* rename the map error proposal to have a specific number
* rename the flatMapLatest proposal to have a specific numbering
* Update availability markers for flatmapLatest
* Cleanup formatting pass for FlatMapLatest
* Remove stray extra single quote
* Add a fallback for older versions of swift for the next method on flatMapLatest
* feat: Add initial naive implementation of flatMapLatest
This introduces a simplified version of the flatMapLatest operator for AsyncSequence, along with a basic unit test. Note that this implementation uses unstructured concurrency and may have race conditions regarding task cancellation.
* refactor: Implement robust flatMapLatest with generation tracking
- Replaces naive implementation with a thread-safe approach using ManagedCriticalState.
- Introduces generation tracking to prevent race conditions where cancelled inner sequences could yield stale values.
- Adds test_interleaving_race_condition to verify correctness under concurrent load.
- Ensures Swift 6 Sendable compliance.
* refactor: Implement high-performance State Machine for flatMapLatest
- Replaces AsyncThrowingStream implementation with a custom AsyncSequence, Storage, and StateMachine.
- Implements explicit state management using Lock for thread safety.
- Handles concurrency between outer and inner sequences robustly.
- Ensures correct cancellation propagation and error handling.
- Verified with test_interleaving_race_condition.
* refactor: Split flatMapLatest into separate files
- Moves AsyncFlatMapLatestSequence.swift to Sources/AsyncAlgorithms/FlatMapLatest/
- Extracts FlatMapLatestStateMachine and FlatMapLatestStorage into their own files.
- Aligns project structure with other complex operators like CombineLatest and Debounce.
* test: Add comprehensive tests for flatMapLatest
- Added test_outer_throwing to verify outer sequence error propagation
- Added test_inner_throwing to verify inner sequence error propagation
- Added test_cancellation to verify proper cancellation handling
- Added test_empty_outer to verify empty outer sequence handling
- Added test_empty_inner to verify empty inner sequence handling
- Fixed test_simple_sequence to be more robust against timing issues
* fix: Make test_outer_throwing stable
- Changed from synchronous map with throwIf to AsyncThrowingStream
- Added delay to ensure proper error propagation timing
- Fixes intermittent test failures
* refactor: Clean up verbose comments in FlatMapLatestStateMachine
- Removed LLM-style thinking comments
- Kept only essential, professional explanations
- No functional changes, all tests pass
* docs: Add comprehensive guide for flatMapLatest
- Added FlatMapLatest.md documentation guide
- Authored by Peter Friese
- Includes introduction, code samples, and use cases
- Covers search-as-you-type, location-based data, and dynamic config examples
- Compares with similar operators in ReactiveX and Combine
* docs: Add evolution proposal for flatMapLatest
- Created SAA-00nn proposal for flatMapLatest operator
- Includes motivation, detailed design, and examples
- Covers implementation strategy with state machine
- Compares with ReactiveX switchMap and Combine switchToLatest
* mapError - transforming failures (#366)
* Implement AsyncMapErrorSequence
* Update the proposal, implementation and tests with formatting, additional details and modifications for being able to push it to a review
* Foratting and preambles
* Update Copyright date for error sequence
Co-authored-by: Clive <clive819@users.noreply.github.com>
* Update copyright year for mapError tests
Co-authored-by: Clive <clive819@users.noreply.github.com>
* Update availablity and modify the mapError algorithm to be opaque
* Correct pre 6.0 build avail parameters
* Make the formatting of the algorithm accesor a bit more consistent
* Update the 5.8 package builder to define 1.2
* re-run formatter
---------
Co-authored-by: Clive <clive819@gmail.com>
Co-authored-by: Clive <clive819@users.noreply.github.com>
* Update the flatMapLatest proposal and implementation to a pitchable state
* Fix race condition in flatMapLatest and add regression test
* chore: Remove test_output.log and add it to .gitignore
---------
Co-authored-by: Philippe Hausler <phausler@apple.com>
Co-authored-by: Clive <clive819@gmail.com>
Co-authored-by: Clive <clive819@users.noreply.github.com>
We added a little helper to move a non-Sendable type into an optional and take it out as sending so that it is disconnected. This was actually a data race hole which got fixed by the compiler so we need to use a proper disconnected abstractions.
* Add partial support for FreeBSD
This change will allow `swift build` to succeed. However, SwiftPM support is still incomplete and does not yet allow adding `.freebsd` to `.when(platforms:)`, so `swift test` cannot be executed.
* Make `swift test` work on FreeBSD
* Introduce test_share_with_no_buffering
* Update test_share_with_no_buffering implementation
* Check limit bigger than 0 in AsyncShareSequence iterate
* Revert "Check limit bigger than 0 in AsyncShareSequence iterate"
This reverts commit 3c9e1ab0ff.
* Check limit bigger than 0 in iterate in AsyncShareSequence
* Hand correct the lint failures in TestShare.swift
* An additional hand-edit lint fix for TestShare.swift
---------
Co-authored-by: Philippe Hausler <phausler@apple.com>
### Motivation:
The previous comments could mislead readers into thinking WASILibc doesn't have pthreads or locks, which isn't correct. The updated comment adds needed clarification to help readers know there multiple variations and configurations of Swift for WebAssembly, some of which provide pthreads and locks.
### Modifications:
- Updated code comments in Locking.swift
### Result:
- Better explanation of WASILibc capabilities.
* Implement AsyncMapErrorSequence
* Update the proposal, implementation and tests with formatting, additional details and modifications for being able to push it to a review
* Foratting and preambles
* Update Copyright date for error sequence
Co-authored-by: Clive <clive819@users.noreply.github.com>
* Update copyright year for mapError tests
Co-authored-by: Clive <clive819@users.noreply.github.com>
* Update availablity and modify the mapError algorithm to be opaque
* Correct pre 6.0 build avail parameters
* Make the formatting of the algorithm accesor a bit more consistent
* Update the 5.8 package builder to define 1.2
* re-run formatter
---------
Co-authored-by: Clive <clive819@gmail.com>
Co-authored-by: Clive <clive819@users.noreply.github.com>
`nonisolated(nonsending)` is a 6.2 language feature that allows the inheritance of the callers isolation. The new `MPSCAsyncChannel.Source` send methods should adopt this to avoid unnecessary isolation hops.
* Add `AsyncBackpressuredStream` proposal and implementation
# Motivation
The pitch to add external backpressure support to the standard libraries `AsyncStream` got returned for revision since there are larger open questions around `AsyncSequence`. However, having external backpressure in a source asynchronous sequence is becoming more and more important.
# Modification
This PR adds a modified proposal and implementation that brings the Swift Evolution proposal over to Swift Async Algorithms.
* Update proposal and implementation
* Update proposal
* Add example project
* Formatting
* Fix Swift 6.0 build
* Future direction for ~Copyable elements
* Apply formatting
* Fix CI
* Move to 6.1 and update proposal
* Guard tests
* Minor edits to the proposal
* Fix revision order
* FIxup setOnTerminationCallback
* Address review feedback
* Rename to `MultiProducerSingleConsumerAsyncChannel`
* Allow one termination callback per source.
* Fix all sendable warnings
* Remove unbounded strategy, rename copy -> makeAdditionalSource, rename asyncSequence() -> elements(), and move enqueue and cancel to the `CallbackHandle` previously called `CallbackToken`
* Remove Example and fix docs and fix format
* fixes grammar and some word order issues
* Remove unnecessary consume
---------
Co-authored-by: Joe Heck <j_heck@apple.com>
This is an implementation of share; an algorithm for allowing AsyncSequence types to be iterated by multiple consumers. In addition this cleans up the project to be a starting point for the 1.1 release - it fixes the continuous integration support and configurations but has the cost of only supporting 3 active releases (6.0, 6.1, and 6.2). Previous releases are only going to have legacy support via older released versions.
* Fix the remaining todo on hard cancellation vs soft cancellation
* Update Sources/AsyncAlgorithms/AsyncShareSequence.swift
Co-authored-by: Jamie <2119834+jamieQ@users.noreply.github.com>
* Update the implementation of share to handle sendability requirements, address some edge cases and update to the latest version of the discussion around buffering behaviors as well as adding some documentation and commentary
* Add some first drafts at unit tests for verifying share behaviors
* Cleanup some of the documentation and address feedback for implementation details
* Fix merge damage by restoring the changes from @jamieQ
* Fix up some availability issues w.r.t. sendable metatypes
* Add a preamble to SendableMetatypes.swift
* Add a fallback for next (for pre 6.2 swift's)
* Slight adjustments for older compiler builds and expose the sendable metatypes since those are part of documentation
* Update formatting and gate availability for older compilers
* Speculative fix for pre 6.1 compiler crashes by removing the sending keyword
* Revert "Speculative fix for pre 6.1 compiler crashes by removing the sending keyword"
This reverts commit 875fa81bc6.
* Speculative fix for pre 6.1 compiler crashes by removing the sending keyword, and correct sendable metatype shim availability
* Remove block comment for formatting check
* Take a more conservative approach and roll back to only supporting share on 6.2
* Only test share on 6.2 or newer
* roll back to older sendable metatype constraints
* yet another stab at 5.10 visionOS guards
* Formatting pass
* Disable 5.10 builds and leave those to older releases
* Ignore swift-version files to avoid local swiftly toolchain selection from being required by others
* Renumber the proposals and copy share into a guide
* Add a link from the primary doc interface to share
---------
Co-authored-by: Jamie <2119834+jamieQ@users.noreply.github.com>
* Conditionalize the testing targets for non-windows targets to avoid the TaskDriver requirements
* Some driveby formatting fixes and remove the freebsd target since that is not supported in swiftpm yet
* Rework validation import conditions to dependency conditionals
* Exclude 5.9 workflows and enable macOS testing scheme for CI
* Additional ifdefing out non windows targets for sequence diagram testing and more formatting fixes
* Correct build failures while updating to 6.2 swift build modes
* Add a package definition for 5.8 builds
* Add some brief explanation for the alteration of the Element in the buffer to an UnsafeTransfer
* Fix broken merge for wasi threads
When DocC was introduced the DocC plugin was useful for building documentation locally. Nowadays both Xcode and VSCode have built-in support to generate this without the need for the plugin.
This PR removes the direct dependency on the plugin.
* Move platform requirements to availability annotations
Adding or raising the deployment platforms in the package manifest is a
SemVer major breaking change as consumers must also add or raise their
deployment platforms. This is a known limitation of SwiftPM.
Unforunately this means that it's very difficult for non-leaf packages
to adopt packages which declare their platforms in the manifest. Doing
so puts the brakes on adoption and ecosystem growth. For 'core' packages
like this one availability constraints should be expressed on
declarations rather than in the manifest.
This patch adds equivalent availability annotations to declarations
across the package and removes platforms from the package manifest.
* Address naming feedback