GRPCAsyncThrowingStream.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. package struct GRPCAsyncThrowingStream<Element: Sendable>: AsyncSequence, Sendable {
  21. package typealias Element = Element
  22. package typealias Failure = any Error
  23. private let base: AsyncThrowingStream<Element, any Error>
  24. package static func makeStream(
  25. of: Element.Type = Element.self
  26. ) -> (stream: Self, continuation: Self.Continuation) {
  27. let base = AsyncThrowingStream.makeStream(of: Element.self)
  28. let stream = GRPCAsyncThrowingStream(base: base.stream)
  29. let continuation = GRPCAsyncThrowingStream.Continuation(base: base.continuation)
  30. return (stream, continuation)
  31. }
  32. fileprivate init(base: AsyncThrowingStream<Element, any Error>) {
  33. self.base = base
  34. }
  35. package struct Continuation: Sendable {
  36. private let base: AsyncThrowingStream<Element, any Error>.Continuation
  37. fileprivate init(base: AsyncThrowingStream<Element, any Error>.Continuation) {
  38. self.base = base
  39. }
  40. func yield(_ value: Element) {
  41. self.base.yield(value)
  42. }
  43. func finish(throwing error: (any Error)? = nil) {
  44. self.base.finish(throwing: error)
  45. }
  46. }
  47. package func makeAsyncIterator() -> AsyncIterator {
  48. AsyncIterator(base: self.base.makeAsyncIterator())
  49. }
  50. package struct AsyncIterator: AsyncIteratorProtocol {
  51. private var base: AsyncThrowingStream<Element, any Error>.AsyncIterator
  52. fileprivate init(base: AsyncThrowingStream<Element, any Error>.AsyncIterator) {
  53. self.base = base
  54. }
  55. package mutating func next() async throws(any Error) -> Element? {
  56. try await self.next(isolation: nil)
  57. }
  58. package mutating func next(
  59. isolation actor: isolated (any Actor)?
  60. ) async throws(any Error) -> Element? {
  61. try await self.base.next(isolation: `actor`)
  62. }
  63. }
  64. }
  65. extension GRPCAsyncThrowingStream.Continuation: RPCWriterProtocol {
  66. package func write(_ element: Element) async throws {
  67. self.yield(element)
  68. }
  69. package func write(contentsOf elements: some Sequence<Element>) async throws {
  70. for element in elements {
  71. self.yield(element)
  72. }
  73. }
  74. }
  75. extension GRPCAsyncThrowingStream.Continuation: ClosableRPCWriterProtocol {
  76. package func finish() async {
  77. self.finish(throwing: nil)
  78. }
  79. package func finish(throwing error: any Error) async {
  80. self.finish(throwing: .some(error))
  81. }
  82. }