@@ -451,124 +451,3 @@ internal protocol AnyContinuation {
451
451
func finish( )
452
452
func finish( throwing error: Error )
453
453
}
454
-
455
-
456
- /// `NIOHTTP2InboundStreamChannels` provides access to inbound stream channels as a generic `AsyncSequence`.
457
- /// They make use of generics to allow for wrapping the stream `Channel`s, for example as `NIOAsyncChannel`s or protocol negotiation objects.
458
- @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
459
- @_spi ( AsyncChannel)
460
- public struct NIOHTTP2InboundStreamChannels < Output> : AsyncSequence {
461
- public struct AsyncIterator : AsyncIteratorProtocol {
462
- public typealias Element = Output
463
-
464
- private var iterator : AsyncThrowingStream < Output , Error > . AsyncIterator
465
-
466
- init ( _ iterator: AsyncThrowingStream < Output , Error > . AsyncIterator ) {
467
- self . iterator = iterator
468
- }
469
-
470
- public mutating func next( ) async throws -> Output ? {
471
- try await self . iterator. next ( )
472
- }
473
- }
474
-
475
- public typealias Element = Output
476
-
477
- private let asyncThrowingStream : AsyncThrowingStream < Output , Error >
478
-
479
- private init ( _ asyncThrowingStream: AsyncThrowingStream < Output , Error > ) {
480
- self . asyncThrowingStream = asyncThrowingStream
481
- }
482
-
483
- public func makeAsyncIterator( ) -> AsyncIterator {
484
- AsyncIterator ( self . asyncThrowingStream. makeAsyncIterator ( ) )
485
- }
486
- }
487
-
488
- @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
489
- extension NIOHTTP2InboundStreamChannels {
490
- /// `Continuation` is a wrapper for a generic `AsyncThrowingStream` to which inbound HTTP2 stream channels are yielded..
491
- @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
492
- struct Continuation : AnyContinuation {
493
- private var continuation : AsyncThrowingStream < Output , Error > . Continuation
494
-
495
- internal init (
496
- continuation: AsyncThrowingStream < Output , Error > . Continuation
497
- ) {
498
- self . continuation = continuation
499
- }
500
-
501
- /// `yield` takes a channel as outputted by the stream initializer and yields the wrapped `AsyncThrowingStream`.
502
- ///
503
- /// It takes channels as as `Any` type to allow wrapping by the stream initializer.
504
- func yield( any: Any ) {
505
- let yieldResult = self . continuation. yield ( any as! Output )
506
- switch yieldResult {
507
- case . enqueued:
508
- break // success, nothing to do
509
- case . dropped:
510
- preconditionFailure ( " Attempted to yield when AsyncThrowingStream is over capacity. This shouldn't be possible for an unbounded stream. " )
511
- case . terminated:
512
- preconditionFailure ( " Attempted to yield to AsyncThrowingStream in terminated state. " )
513
- default :
514
- preconditionFailure ( " Attempt to yield to AsyncThrowingStream failed for unhandled reason. " )
515
- }
516
- }
517
-
518
- /// `finish` marks the continuation as finished.
519
- func finish( ) {
520
- self . continuation. finish ( )
521
- }
522
-
523
- /// `finish` marks the continuation as finished with the supplied error.
524
- func finish( throwing error: Error ) {
525
- self . continuation. finish ( throwing: error)
526
- }
527
- }
528
-
529
-
530
- /// `initialize` creates a new `Continuation` object and returns it along with its backing `AsyncThrowingStream`.
531
- /// The `StreamChannelContinuation` provides access to the inbound HTTP2 stream channels.
532
- ///
533
- /// - Parameters:
534
- /// - inboundStreamInititializer: A closure which initializes the newly-created inbound stream channel and returns a generic.
535
- /// The returned type corresponds to the output of the channel once the operations in the initializer have been performed.
536
- /// For example an `inboundStreamInititializer` which inserts handlers before wrapping the channel in a `NIOAsyncChannel` would
537
- /// have a `Output` corresponding to that `NIOAsyncChannel` type. Another example is in cases where there is
538
- /// per-stream protocol negotiation where `Output` would be some form of `NIOProtocolNegotiationResult`.
539
- static func initialize( inboundStreamInitializerOutput: Output . Type = Output . self) -> ( NIOHTTP2InboundStreamChannels < Output > , Continuation ) {
540
- let ( stream, continuation) = AsyncThrowingStream . makeStream ( of: Output . self)
541
- return ( . init( stream) , Continuation ( continuation: continuation) )
542
- }
543
- }
544
-
545
- #if swift(>=5.7)
546
- // This doesn't compile on 5.6 but the omission of Sendable is sufficient in any case
547
- @available ( * , unavailable)
548
- extension NIOHTTP2InboundStreamChannels . AsyncIterator : Sendable { }
549
-
550
- @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
551
- extension NIOHTTP2InboundStreamChannels : Sendable where Output: Sendable { }
552
- #else
553
- // This wasn't marked as sendable in 5.6 however it should be fine
554
- // https://forums.swift.org/t/so-is-asyncstream-sendable-or-not/53148/2
555
- @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
556
- extension NIOHTTP2InboundStreamChannels : @unchecked Sendable where Output: Sendable { }
557
- #endif
558
-
559
-
560
- #if swift(<5.9)
561
- // this should be available in the std lib from 5.9 onwards
562
- @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
563
- extension AsyncThrowingStream {
564
- public static func makeStream(
565
- of elementType: Element . Type = Element . self,
566
- throwing failureType: Failure . Type = Failure . self,
567
- bufferingPolicy limit: Continuation . BufferingPolicy = . unbounded
568
- ) -> ( stream: AsyncThrowingStream < Element , Failure > , continuation: AsyncThrowingStream < Element , Failure > . Continuation ) where Failure == Error {
569
- var continuation : AsyncThrowingStream < Element , Failure > . Continuation !
570
- let stream = AsyncThrowingStream < Element , Failure > ( bufferingPolicy: limit) { continuation = $0 }
571
- return ( stream: stream, continuation: continuation!)
572
- }
573
- }
574
- #endif
0 commit comments