UnknownStorage.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Sources/SwiftProtobuf/UnknownStorage.swift - Handling unknown fields
  2. //
  3. // Copyright (c) 2014 - 2016 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// Proto2 binary coding requires storing and recoding of unknown fields.
  12. /// This simple support class handles that requirement. A property of this type
  13. /// is compiled into every proto2 message.
  14. ///
  15. // -----------------------------------------------------------------------------
  16. import Foundation
  17. /// Contains any unknown fields in a decoded message; that is, fields that were
  18. /// sent on the wire but were not recognized by the generated message
  19. /// implementation or were valid field numbers but with mismatching wire
  20. /// formats (for example, a field encoded as a varint when a fixed32 integer
  21. /// was expected).
  22. public struct UnknownStorage: Equatable, @unchecked Sendable {
  23. // Once swift(>=5.9) the '@unchecked' can be removed, it is needed for Data in
  24. // linux builds.
  25. /// The raw protocol buffer binary-encoded bytes that represent the unknown
  26. /// fields of a decoded message.
  27. public private(set) var data = Data()
  28. public init() {}
  29. internal mutating func append(protobufData: Data) {
  30. data.append(protobufData)
  31. }
  32. public func traverse<V: Visitor>(visitor: inout V) throws {
  33. if !data.isEmpty {
  34. try visitor.visitUnknown(bytes: data)
  35. }
  36. }
  37. }