RPCWriter+Closable.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. extension RPCWriter {
  17. public struct Closable: ClosableRPCWriterProtocol {
  18. @usableFromInline
  19. let writer: any ClosableRPCWriterProtocol<Element>
  20. /// Creates an ``RPCWriter`` by wrapping the `other` writer.
  21. ///
  22. /// - Parameter other: The writer to wrap.
  23. @inlinable
  24. public init(wrapping other: some ClosableRPCWriterProtocol<Element>) {
  25. self.writer = other
  26. }
  27. /// Writes a single element.
  28. ///
  29. /// This function suspends until the element has been accepted. Implementers can use this
  30. /// to exert backpressure on callers.
  31. ///
  32. /// - Parameter element: The element to write.
  33. @inlinable
  34. public func write(_ element: Element) async throws {
  35. try await self.writer.write(element)
  36. }
  37. /// Writes a sequence of elements.
  38. ///
  39. /// This function suspends until the elements have been accepted. Implementers can use this
  40. /// to exert backpressure on callers.
  41. ///
  42. /// - Parameter elements: The elements to write.
  43. @inlinable
  44. public func write(contentsOf elements: some Sequence<Element>) async throws {
  45. try await self.writer.write(contentsOf: elements)
  46. }
  47. /// Indicate to the writer that no more writes are to be accepted.
  48. ///
  49. /// All writes after ``finish()`` has been called should result in an error
  50. /// being thrown.
  51. @inlinable
  52. public func finish() async {
  53. await self.writer.finish()
  54. }
  55. /// Indicate to the writer that no more writes are to be accepted because an error occurred.
  56. ///
  57. /// All writes after ``finish(throwing:)`` has been called should result in an error
  58. /// being thrown.
  59. @inlinable
  60. public func finish(throwing error: any Error) async {
  61. await self.writer.finish(throwing: error)
  62. }
  63. }
  64. }