mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-24 04:17:52 +00:00
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>
65 lines
1.7 KiB
Swift
65 lines
1.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
public struct GatedSequence<Element> {
|
|
public typealias Failure = Never
|
|
let elements: [Element]
|
|
let gates: [Gate]
|
|
var index = 0
|
|
|
|
public mutating func advance() {
|
|
defer { index += 1 }
|
|
guard index < gates.count else {
|
|
return
|
|
}
|
|
gates[index].open()
|
|
}
|
|
|
|
public init(_ elements: [Element]) {
|
|
self.elements = elements
|
|
self.gates = elements.map { _ in Gate() }
|
|
}
|
|
}
|
|
|
|
extension GatedSequence: AsyncSequence {
|
|
public struct Iterator: AsyncIteratorProtocol {
|
|
var gatedElements: [(Element, Gate)]
|
|
|
|
init(elements: [Element], gates: [Gate]) {
|
|
gatedElements = Array(zip(elements, gates))
|
|
}
|
|
|
|
public mutating func next() async -> Element? {
|
|
guard gatedElements.count > 0 else {
|
|
return nil
|
|
}
|
|
let (element, gate) = gatedElements.removeFirst()
|
|
await gate.enter()
|
|
return element
|
|
}
|
|
|
|
public mutating func next(isolation actor: isolated (any Actor)?) async throws(Never) -> Element? {
|
|
guard gatedElements.count > 0 else {
|
|
return nil
|
|
}
|
|
let (element, gate) = gatedElements.removeFirst()
|
|
await gate.enter()
|
|
return element
|
|
}
|
|
}
|
|
|
|
public func makeAsyncIterator() -> Iterator {
|
|
Iterator(elements: elements, gates: gates)
|
|
}
|
|
}
|
|
|
|
extension GatedSequence: Sendable where Element: Sendable {}
|