GRPCContiguousBytes.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. /// A bag-of-bytes type.
  17. ///
  18. /// This protocol is used by the transport protocols (``ClientTransport`` and ``ServerTransport``)
  19. /// with the serialization protocols (``MessageSerializer`` and ``MessageDeserializer``) so that
  20. /// messages don't have to be copied to a fixed intermediate bag-of-bytes types.
  21. public protocol GRPCContiguousBytes {
  22. /// Initialize the bytes to a repeated value.
  23. ///
  24. /// - Parameters:
  25. /// - byte: The value to be repeated.
  26. /// - count: The number of times to repeat the byte value.
  27. init(repeating byte: UInt8, count: Int)
  28. /// Initialize the bag of bytes from a sequence of bytes.
  29. ///
  30. /// - Parameters:
  31. /// - sequence: a sequence of `UInt8` from which the bag of bytes should be constructed.
  32. init<Bytes: Sequence>(_ sequence: Bytes) where Bytes.Element == UInt8
  33. /// The number of bytes in the bag of bytes.
  34. var count: Int { get }
  35. /// Calls the given closure with the contents of underlying storage.
  36. ///
  37. /// - Note: Calling `withUnsafeBytes` multiple times does not guarantee that
  38. /// the same buffer pointer will be passed in every time.
  39. /// - Warning: The buffer argument to the body should not be stored or used
  40. /// outside of the lifetime of the call to the closure.
  41. func withUnsafeBytes<R>(_ body: (_ buffer: UnsafeRawBufferPointer) throws -> R) rethrows -> R
  42. /// Calls the given closure with the contents of underlying storage.
  43. ///
  44. /// - Note: Calling `withUnsafeBytes` multiple times does not guarantee that
  45. /// the same buffer pointer will be passed in every time.
  46. /// - Warning: The buffer argument to the body should not be stored or used
  47. /// outside of the lifetime of the call to the closure.
  48. mutating func withUnsafeMutableBytes<R>(
  49. _ body: (_ buffer: UnsafeMutableRawBufferPointer) throws -> R
  50. ) rethrows -> R
  51. }
  52. extension [UInt8]: GRPCContiguousBytes {}