Skip to content

Commit ec945bf

Browse files
committed
Implement AsyncThrowingStream.nextElement() with typed throws
1 parent 31c0ab7 commit ec945bf

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

stdlib/public/Concurrency/AsyncThrowingStream.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ public struct AsyncThrowingStream<Element, Failure: Error> {
254254

255255
final class _Context {
256256
let storage: _Storage?
257-
let produce: () async throws -> Element?
257+
let produce: () async throws(Failure) -> Element?
258258

259-
init(storage: _Storage? = nil, produce: @escaping () async throws -> Element?) {
259+
init(storage: _Storage? = nil, produce: @escaping () async throws(Failure) -> Element?) {
260260
self.storage = storage
261261
self.produce = produce
262262
}
@@ -415,6 +415,24 @@ extension AsyncThrowingStream: AsyncSequence {
415415
public mutating func next() async throws -> Element? {
416416
return try await context.produce()
417417
}
418+
419+
/// The next value from the asynchronous stream.
420+
///
421+
/// When `nextElement()` returns `nil`, this signifies the end of the
422+
/// `AsyncThrowingStream`.
423+
///
424+
/// It is a programmer error to invoke `nextElement()` from a concurrent
425+
/// context that contends with another such call, which results in a call to
426+
/// `fatalError()`.
427+
///
428+
/// If you cancel the task this iterator is running in while `nextElement()`
429+
/// is awaiting a value, the `AsyncThrowingStream` terminates. In this case,
430+
/// `nextElement()` may return `nil` immediately, or else return `nil` on
431+
/// subsequent calls.
432+
@available(SwiftStdlib 5.11, *)
433+
public mutating func nextElement() async throws(Failure) -> Element? {
434+
return try await context.produce()
435+
}
418436
}
419437

420438
/// Creates the asynchronous iterator that produces elements of this

0 commit comments

Comments
 (0)