Forked from inamiy/AsyncSequence-subprotocol-try-await-element-type.swift
Created
May 2, 2023 06:29
-
-
Save brianmwadime/c8a2622835067ad09ce2bfd6ab13a38f to your computer and use it in GitHub Desktop.
Revisions
-
inamiy created this gist
Nov 21, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ import Foundation import _Concurrency extension AsyncStream { public init<S: AsyncSequence & Sendable>( _ sequence: S, bufferingPolicy limit: Continuation.BufferingPolicy = .unbounded ) where S.Element == Element { self.init(bufferingPolicy: limit) { continuation in let task = Task { do { for try await element in sequence { continuation.yield(element) } } catch {} continuation.finish() } let terminationClosure: @Sendable (Continuation.Termination) -> Void = { _ in task.cancel() } continuation.onTermination = terminationClosure } } } // MARK: ---------------------------------------- // Using Swift 5.7 primary associated type protocol MyAsyncSequence<Element>: AsyncSequence {} extension AsyncStream: MyAsyncSequence {} let stream = AsyncStream<Int> { nil } let anyMySequence: any MyAsyncSequence<Int> = stream // ERROR: x is Any, not Int for try await x: Int in anyMySequence { } let mySeqStream = AsyncStream.init(anyMySequence) // NOTE: This `init` is type-erasure // OK: x is Int for try await x: Int in mySeqStream { } "✅"