ProtobufUnknown.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // ProtobufRuntime/Sources/Protobuf/ProtobufUnknown.swift - Handling unknown fields
  2. //
  3. // This source file is part of the Swift.org open source project
  4. //
  5. // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See http://swift.org/LICENSE.txt for license information
  9. // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
  10. //
  11. // -----------------------------------------------------------------------------
  12. ///
  13. /// Proto2 binary coding requires storing and recoding of unknown fields.
  14. /// This simple support class handles that requirement. A property of this type
  15. /// is compiled into every proto2 message.
  16. ///
  17. // -----------------------------------------------------------------------------
  18. import Swift
  19. public struct ProtobufUnknownStorage: Equatable {
  20. fileprivate var data: [UInt8] = []
  21. public init() {}
  22. public mutating func decodeField(setter: inout ProtobufFieldDecoder) throws -> Bool {
  23. if let u = try setter.asProtobufUnknown() {
  24. data.append(contentsOf: u)
  25. return true
  26. } else {
  27. return false
  28. }
  29. }
  30. public func traverse(visitor: inout ProtobufVisitor) {
  31. visitor.visitUnknown(bytes: data)
  32. }
  33. public var isEmpty: Bool {
  34. get {return data.isEmpty}
  35. }
  36. }
  37. public func ==(lhs: ProtobufUnknownStorage, rhs: ProtobufUnknownStorage) -> Bool {
  38. return lhs.data == rhs.data
  39. }