ContiguousBytesAdapter.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2025, 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. public import GRPCCore // internal but @usableFromInline
  17. public import SwiftProtobuf // internal but @usableFromInline
  18. /// Brides between `GRPCContiguousBytes` and `SwiftProtobufContiguousBytes` which have the same
  19. /// requirements.
  20. ///
  21. /// This is necessary as `SwiftProtobufContiguousBytes` can't be the protocol in the gRPC API (as
  22. /// it'd require a dependency on Protobuf in the core package), and `GRPCContiguousBytes` can't
  23. /// refine `SwiftProtobufContiguousBytes` for the same reason.
  24. @usableFromInline
  25. struct ContiguousBytesAdapter<
  26. Bytes: GRPCContiguousBytes
  27. >: GRPCContiguousBytes, SwiftProtobufContiguousBytes {
  28. @usableFromInline
  29. var bytes: Bytes
  30. @inlinable
  31. init(_ bytes: Bytes) {
  32. self.bytes = bytes
  33. }
  34. @inlinable
  35. init(repeating: UInt8, count: Int) {
  36. self.bytes = Bytes(repeating: repeating, count: count)
  37. }
  38. @inlinable
  39. init(_ sequence: some Sequence<UInt8>) {
  40. self.bytes = Bytes(sequence)
  41. }
  42. @inlinable
  43. var count: Int {
  44. self.bytes.count
  45. }
  46. @inlinable
  47. func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
  48. try self.bytes.withUnsafeBytes(body)
  49. }
  50. @inlinable
  51. mutating func withUnsafeMutableBytes<R>(
  52. _ body: (UnsafeMutableRawBufferPointer) throws -> R
  53. ) rethrows -> R {
  54. try self.bytes.withUnsafeMutableBytes(body)
  55. }
  56. }