2
0

GRPCAsyncRequestStream.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 GRPCAsyncRequestStream<Element: Sendable>: 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. @inlinable
  30. public func makeAsyncIterator() -> Iterator {
  31. Self.AsyncIterator(self._stream)
  32. }
  33. public struct Iterator: AsyncIteratorProtocol {
  34. @usableFromInline
  35. internal var iterator: _WrappedStream.AsyncIterator
  36. @usableFromInline
  37. internal init(_ stream: _WrappedStream) {
  38. self.iterator = stream.makeAsyncIterator()
  39. }
  40. @inlinable
  41. public mutating func next() async throws -> Element? {
  42. try await self.iterator.next()
  43. }
  44. }
  45. }
  46. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  47. extension GRPCAsyncRequestStream: Sendable where Element: Sendable {}
  48. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  49. extension GRPCAsyncRequestStream.Iterator: Sendable where Element: Sendable {}
  50. #endif