Internal.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #if !REMOVE_DEPRECATED_APIS
  21. /// A singleton instance of an empty data that is used by the generated code
  22. /// for default values. This is a performance enhancement to work around the
  23. /// fact that the `Data` type in Swift involves a new heap allocation every
  24. /// time an empty instance is initialized, instead of sharing a common empty
  25. /// backing storage.
  26. /// - Note: This isn't really used any longer - it's only here to support code generated by 1.10.2 and earlier.
  27. @available(
  28. *,
  29. deprecated,
  30. message:
  31. "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."
  32. )
  33. public static let emptyData = Data()
  34. #endif // !REMOVE_DEPRECATED_APIS
  35. /// Helper to loop over a list of Messages to see if they are all
  36. /// initialized (see Message.isInitialized for what that means).
  37. public static func areAllInitialized(_ listOfMessages: [any Message]) -> Bool {
  38. for msg in listOfMessages {
  39. if !msg.isInitialized {
  40. return false
  41. }
  42. }
  43. return true
  44. }
  45. /// Helper to loop over dictionary with values that are Messages to see if
  46. /// they are all initialized (see Message.isInitialized for what that means).
  47. public static func areAllInitialized<K>(_ mapToMessages: [K: any Message]) -> Bool {
  48. for (_, msg) in mapToMessages {
  49. if !msg.isInitialized {
  50. return false
  51. }
  52. }
  53. return true
  54. }
  55. }