Implement AsyncIteratorProtocol.next() in terms of next(isolation:).

New async iterators should be able to implement only `next(isolation:)` and
get the older `next()` implementation via a default. Implement the
appropriate default witness.

Fixes rdar://125447861.
This commit is contained in:
Doug Gregor
2024-03-28 11:20:14 -07:00
parent 9cbf165520
commit a8f3bb5e5e
2 changed files with 25 additions and 2 deletions

View File

@@ -111,8 +111,9 @@ public protocol AsyncIteratorProtocol<Element, Failure> {
@available(SwiftStdlib 5.1, *)
extension AsyncIteratorProtocol {
/// Default implementation of `next()` in terms of `next()`, which is
/// required to maintain backward compatibility with existing async iterators.
/// Default implementation of `next(isolation:)` in terms of `next()`, which
/// is required to maintain backward compatibility with existing async
/// iterators.
@available(SwiftStdlib 6.0, *)
@inlinable
public mutating func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element? {
@@ -123,3 +124,15 @@ extension AsyncIteratorProtocol {
}
}
}
@available(SwiftStdlib 5.1, *)
extension AsyncIteratorProtocol {
/// Default implementation of `next()` in terms of `next(isolation:)`, which
/// is required to maintain backward compatibility with existing async
/// iterators.
@available(SwiftStdlib 6.0, *)
@inlinable
public mutating func next() async throws(Failure) -> Element? {
return try await next(isolation: nil)
}
}

View File

@@ -75,3 +75,13 @@ enum MyError: Error {
func getASequence() -> any AsyncSequence<Data, MyError> {
return ErrorSequence<Data, _>(throwError: MyError.foo) // ERROR: Cannot convert return expression of type 'any Error' to return type 'MyError'
}
// Test the default implementation of next() in terms of next(isolation:).
struct AsyncIteratorWithOnlyNextIsolation: AsyncIteratorProtocol {
public mutating func next(isolation: (any Actor)?) throws(MyError) -> Int? { 0 }
}
// Test the default implementation of next(isolation:) in terms of next().
struct AsyncIteratorWithOnlyNext: AsyncIteratorProtocol {
public mutating func next() throws(MyError) -> Int? { 0 }
}