2
0

RPCWriter+Closable.swift 2.4 KB

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