Internal.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Sources/SwiftProtobuf/Internal.swift - Message support
  2. //
  3. // Copyright (c) 2014 - 2017 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. /// Internal helpers on Messages for the library. These are public
  12. /// just so the generated code can call them, but shouldn't be called
  13. /// by developers directly.
  14. ///
  15. // -----------------------------------------------------------------------------
  16. import Foundation
  17. /// Functions that are public only because they are used by generated message implementations.
  18. /// - Important: NOT INTENDED TO BE CALLED BY CLIENTS.
  19. public enum Internal {
  20. /// A singleton instance of an empty data that is used by the generated code
  21. /// for default values. This is a performance enhancement to work around the
  22. /// fact that the `Data` type in Swift involves a new heap allocation every
  23. /// time an empty instance is initialized, instead of sharing a common empty
  24. /// backing storage.
  25. /// - Note: This isn't really used any longer - it's only here to support code generated by 1.10.2 and earlier.
  26. @available(
  27. *,
  28. deprecated,
  29. message:
  30. "Internal.emptyData isn't used any longer in newer versions of the generator. Generate code with a version later than 1.10.2 to get performance improvements. See https://github.com/apple/swift-protobuf/pull/1028 for more information."
  31. )
  32. public static let emptyData = Data()
  33. /// Helper to loop over a list of Messages to see if they are all
  34. /// initialized (see Message.isInitialized for what that means).
  35. public static func areAllInitialized(_ listOfMessages: [any Message]) -> Bool {
  36. for msg in listOfMessages {
  37. if !msg.isInitialized {
  38. return false
  39. }
  40. }
  41. return true
  42. }
  43. /// Helper to loop over dictionary with values that are Messages to see if
  44. /// they are all initialized (see Message.isInitialized for what that means).
  45. public static func areAllInitialized<K>(_ mapToMessages: [K: any Message]) -> Bool {
  46. for (_, msg) in mapToMessages {
  47. if !msg.isInitialized {
  48. return false
  49. }
  50. }
  51. return true
  52. }
  53. }