RPCWriterProtocol.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2023, 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. /// A sink for values which are produced over time.
  17. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  18. public protocol RPCWriterProtocol<Element>: Sendable {
  19. /// The type of value written.
  20. associatedtype Element
  21. /// Writes a single element.
  22. ///
  23. /// This function suspends until the element has been accepted. Implementers can use this
  24. /// to exert backpressure on callers.
  25. ///
  26. /// - Parameter element: The element to write.
  27. func write(_ element: Element) async throws
  28. /// Writes a sequence of elements.
  29. ///
  30. /// This function suspends until the elements have been accepted. Implementers can use this
  31. /// to exert backpressure on callers.
  32. ///
  33. /// - Parameter elements: The elements to write.
  34. func write(contentsOf elements: some Sequence<Element>) async throws
  35. }
  36. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  37. extension RPCWriterProtocol {
  38. /// Writes an `AsyncSequence` of values into the sink.
  39. ///
  40. /// - Parameter elements: The elements to write.
  41. public func write<Elements: AsyncSequence>(
  42. contentsOf elements: Elements
  43. ) async throws where Elements.Element == Element {
  44. for try await element in elements {
  45. try await self.write(element)
  46. }
  47. }
  48. }
  49. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  50. public protocol ClosableRPCWriterProtocol<Element>: RPCWriterProtocol {
  51. /// Indicate to the writer that no more writes are to be accepted.
  52. ///
  53. /// All writes after ``finish()`` has been called should result in an error
  54. /// being thrown.
  55. func finish()
  56. /// Indicate to the writer that no more writes are to be accepted because an error occurred.
  57. ///
  58. /// All writes after ``finish(throwing:)`` has been called should result in an error
  59. /// being thrown.
  60. func finish(throwing error: any Error)
  61. }