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 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>
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.
* 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`
* 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
* 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
# Motivation
During the discussion in https://github.com/apple/swift-async-algorithms/issues/248 it became clear that the semantics of `throttle` are not 100% figured out yet.
# Modification
This PR is making `throttle` an underscored API for the upcoming 1.0.0 release. This gives us more time to investigate what exact semantics we want to have.
# Motivation
We added `@_implementationOnly` a little while ago to hide the dependency on the `DequeModule`. However, with recent compilers this produces a warning since `@_implementationOnly` is only intended to be used in resilient libraries.
# Modification
This PR removes the usage of `@_implementationOnly`
# Motivation
`@unchecked Sendable` is a great way to make a type `Sendable` while it is not really `Sendable` this is rarely useful and we should rather use conditional `@unchecked Sendable` annotations such as the one on `ManagedCriticalState`
# Modification
This PR removes all `@unchecked Sendable` in the main algorithm target except the one on `Merge` since we are doing manual locking there.
# Result
No more `@unchecked Sendable` usage.
# Motivation
Currently, the `AsyncTimerSequence` is trying to correct for when an application becomes suspended and the timer might fire multiple times once the application gets foregrounded again.
However, this is already handled by the `Clock` types themselves. The `SuspendingClock` is correcting for suspension of the app whereas the `ContinuousClock` is not.
Additionally, this was not only hit by background an application but by just calling `Task.sleep` in the for-await loop that is consuming the sequence.
# Modification
This removes the part of the code in `AsyncTimerSequence` which corrected for suspension of the application.
# Motivation
We still had some `Sendable` warnings left under strict Concurrency checking.
# Modification
This PR fixes a bunch of `Sendable` warnings but we still have some left in the validation tests. Additionally, I fixed a flaky test.
* Integrate review feedback
This integrates all of the feedback from the review thread. Here is a quick summary:
- Change the trailing separator behaviour. We are no longer returning a separator before we are forwarding the error
- Add a synchronous and asynchronous closure based `interspersed` method.
- Support interspersing every n elements
* Add AsyncThrowingInterspersedSequence
* Update examples