GRPCAsyncThrowingStream.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2024, 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. // This exists to provide a version of 'AsyncThrowingStream' which is constrained to 'Sendable'
  17. // elements. This is required in order for the continuation to be compatible with
  18. // 'RPCWriterProtocol'. (Adding a constrained conformance to 'RPCWriterProtocol' on
  19. // 'AsyncThrowingStream.Continuation' isn't possible because 'Sendable' is a marker protocol.)
  20. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  21. package struct GRPCAsyncThrowingStream<Element: Sendable>: AsyncSequence, Sendable {
  22. package typealias Element = Element
  23. package typealias Failure = any Error
  24. private let base: AsyncThrowingStream<Element, any Error>
  25. package static func makeStream(
  26. of: Element.Type = Element.self
  27. ) -> (stream: Self, continuation: Self.Continuation) {
  28. let base = AsyncThrowingStream.makeStream(of: Element.self)
  29. let stream = GRPCAsyncThrowingStream(base: base.stream)
  30. let continuation = GRPCAsyncThrowingStream.Continuation(base: base.continuation)
  31. return (stream, continuation)
  32. }
  33. fileprivate init(base: AsyncThrowingStream<Element, any Error>) {
  34. self.base = base
  35. }
  36. package struct Continuation: Sendable {
  37. private let base: AsyncThrowingStream<Element, any Error>.Continuation
  38. fileprivate init(base: AsyncThrowingStream<Element, any Error>.Continuation) {
  39. self.base = base
  40. }
  41. func yield(_ value: Element) {
  42. self.base.yield(value)
  43. }
  44. func finish(throwing error: (any Error)? = nil) {
  45. self.base.finish(throwing: error)
  46. }
  47. }
  48. package func makeAsyncIterator() -> AsyncIterator {
  49. AsyncIterator(base: self.base.makeAsyncIterator())
  50. }
  51. package struct AsyncIterator: AsyncIteratorProtocol {
  52. private var base: AsyncThrowingStream<Element, any Error>.AsyncIterator
  53. fileprivate init(base: AsyncThrowingStream<Element, any Error>.AsyncIterator) {
  54. self.base = base
  55. }
  56. package mutating func next() async throws(any Error) -> Element? {
  57. try await self.next(isolation: nil)
  58. }
  59. package mutating func next(
  60. isolation actor: isolated (any Actor)?
  61. ) async throws(any Error) -> Element? {
  62. try await self.base.next(isolation: `actor`)
  63. }
  64. }
  65. }
  66. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  67. extension GRPCAsyncThrowingStream.Continuation: RPCWriterProtocol {
  68. package func write(_ element: Element) async throws {
  69. self.yield(element)
  70. }
  71. package func write(contentsOf elements: some Sequence<Element>) async throws {
  72. for element in elements {
  73. self.yield(element)
  74. }
  75. }
  76. }
  77. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  78. extension GRPCAsyncThrowingStream.Continuation: ClosableRPCWriterProtocol {
  79. package func finish() async {
  80. self.finish(throwing: nil)
  81. }
  82. package func finish(throwing error: any Error) async {
  83. self.finish(throwing: .some(error))
  84. }
  85. }