GPBUnknownFields+Additions.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2024 Google Inc. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file or at
  6. // https://developers.google.com/open-source/licenses/bsd
  7. /// Swift specific additions to simplify usage.
  8. extension GPBUnknownFields {
  9. /// Fetches the first varint for the given field number.
  10. public func firstVarint(_ fieldNumber: Int32) -> UInt64? {
  11. var value: UInt64 = 0
  12. guard getFirst(fieldNumber, varint: &value) else { return nil }
  13. return value
  14. }
  15. /// Fetches the first fixed32 for the given field number.
  16. public func firstFixed32(_ fieldNumber: Int32) -> UInt32? {
  17. var value: UInt32 = 0
  18. guard getFirst(fieldNumber, fixed32: &value) else { return nil }
  19. return value
  20. }
  21. /// Fetches the first fixed64 for the given field number.
  22. public func firstFixed64(_ fieldNumber: Int32) -> UInt64? {
  23. var value: UInt64 = 0
  24. guard getFirst(fieldNumber, fixed64: &value) else { return nil }
  25. return value
  26. }
  27. }
  28. /// Map the `NSFastEnumeration` support to a Swift `Sequence`.
  29. extension GPBUnknownFields: Sequence {
  30. public typealias Element = GPBUnknownField
  31. public struct Iterator: IteratorProtocol {
  32. var iter: NSFastEnumerationIterator
  33. init(_ fields: NSFastEnumeration) {
  34. self.iter = NSFastEnumerationIterator(fields)
  35. }
  36. public mutating func next() -> GPBUnknownField? {
  37. return iter.next() as? GPBUnknownField
  38. }
  39. }
  40. public func makeIterator() -> Iterator {
  41. return Iterator(self)
  42. }
  43. }