RPCWriterProtocol.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 sequence of elements.
  22. ///
  23. /// This function suspends until the elements have been accepted. Implements can use this
  24. /// to exert backpressure on callers.
  25. ///
  26. /// - Parameter elements: The elements to write.
  27. func write(contentsOf elements: some Sequence<Element>) async throws
  28. }
  29. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  30. extension RPCWriterProtocol {
  31. /// Writes a single element into the sink.
  32. ///
  33. /// - Parameter element: The element to write.
  34. public func write(_ element: Element) async throws {
  35. try await self.write(contentsOf: CollectionOfOne(element))
  36. }
  37. /// Writes an `AsyncSequence` of values into the sink.
  38. ///
  39. /// - Parameter elements: The elements to write.
  40. public func write<Elements: AsyncSequence>(
  41. _ elements: Elements
  42. ) async throws where Elements.Element == Element {
  43. for try await element in elements {
  44. try await self.write(element)
  45. }
  46. }
  47. }
  48. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  49. public protocol ClosableRPCWriterProtocol<Element>: RPCWriterProtocol {
  50. /// Indicate to the writer that no more writes are to be accepted.
  51. ///
  52. /// All writes after ``finish()`` has been called should result in an error
  53. /// being thrown.
  54. func finish()
  55. }