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