GRPCAsyncResponseStream.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright 2021, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if compiler(>=5.6)
  17. /// This is currently a wrapper around AsyncThrowingStream because we want to be
  18. /// able to swap out the implementation for something else in the future.
  19. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  20. public struct GRPCAsyncResponseStream<Element>: AsyncSequence {
  21. @usableFromInline
  22. internal typealias WrappedStream = PassthroughMessageSequence<Element, Error>
  23. @usableFromInline
  24. internal let stream: WrappedStream
  25. @inlinable
  26. internal init(_ stream: WrappedStream) {
  27. self.stream = stream
  28. }
  29. public func makeAsyncIterator() -> Iterator {
  30. Self.AsyncIterator(self.stream)
  31. }
  32. public struct Iterator: AsyncIteratorProtocol {
  33. @usableFromInline
  34. internal var iterator: WrappedStream.AsyncIterator
  35. fileprivate init(_ stream: WrappedStream) {
  36. self.iterator = stream.makeAsyncIterator()
  37. }
  38. @inlinable
  39. public mutating func next() async throws -> Element? {
  40. try await self.iterator.next()
  41. }
  42. }
  43. }
  44. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  45. extension GRPCAsyncResponseStream: Sendable where Element: Sendable {}
  46. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  47. extension GRPCAsyncResponseStream.Iterator: Sendable where Element: Sendable {}
  48. #endif