RPCWriter+Closable.swift 2.0 KB

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