mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-27 04:44:21 +00:00
Mark all iterators as non-Sendable (#280)
This commit is contained in:
1 parent
df46b4c235
commit
f5d5fb6483
28 files changed
+138
-100
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -95,3 +95,6 @@ extension AsyncChain3Sequence: AsyncSequence {
|
||||
}
|
||||
|
||||
extension AsyncChain3Sequence: Sendable where Base1: Sendable, Base2: Sendable, Base3: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncChain3Sequence.Iterator: Sendable { }
|
||||
@@ -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 { }
|
||||
@@ -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 { }
|
||||
@@ -83,3 +83,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 { }
|
||||
@@ -85,3 +85,6 @@ extension AsyncInclusiveReductionsSequence: AsyncSequence {
|
||||
}
|
||||
|
||||
extension AsyncInclusiveReductionsSequence: Sendable where Base: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncInclusiveReductionsSequence.Iterator: 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 { }
|
||||
@@ -67,3 +67,6 @@ public struct AsyncSyncSequence<Base: Sequence>: AsyncSequence {
|
||||
}
|
||||
|
||||
extension AsyncSyncSequence: Sendable where Base: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncSyncSequence.Iterator: Sendable { }
|
||||
@@ -102,3 +102,6 @@ extension AsyncThrottleSequence: AsyncSequence {
|
||||
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
extension AsyncThrottleSequence: Sendable where Base: Sendable, Element: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncThrottleSequence.Iterator: Sendable { }
|
||||
@@ -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.
|
||||
@@ -89,3 +100,6 @@ extension AsyncThrowingInclusiveReductionsSequence: AsyncSequence {
|
||||
}
|
||||
|
||||
extension AsyncThrowingInclusiveReductionsSequence: Sendable where Base: Sendable { }
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncThrowingInclusiveReductionsSequence.Iterator: Sendable { }
|
||||
@@ -89,3 +89,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 { }
|
||||
@@ -58,3 +58,6 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, @unchecked Se
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncChannel.Iterator: Sendable { }
|
||||
@@ -61,3 +61,6 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension AsyncThrowingChannel.Iterator: Sendable { }
|
||||
@@ -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 { }
|
||||
@@ -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 { }
|
||||
@@ -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 { }
|
||||
@@ -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 { }
|
||||
@@ -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 { }
|
||||
@@ -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 { }
|
||||
Reference in new issue
Block a user