MessageExtension.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Sources/SwiftProtobuf/MessageExtension.swift - Extension support
  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. /// A 'Message Extension' is an immutable class object that describes
  12. /// a particular extension field, including string and number
  13. /// identifiers, serialization details, and the identity of the
  14. /// message that is being extended.
  15. ///
  16. // -----------------------------------------------------------------------------
  17. /// Type-erased MessageExtension field implementation.
  18. @preconcurrency
  19. public protocol AnyMessageExtension: Sendable {
  20. var fieldNumber: Int { get }
  21. var fieldName: String { get }
  22. var messageType: any Message.Type { get }
  23. func _protobuf_newField<D: Decoder>(decoder: inout D) throws -> (any AnyExtensionField)?
  24. }
  25. /// A "Message Extension" relates a particular extension field to
  26. /// a particular message. The generic constraints allow
  27. /// compile-time compatibility checks.
  28. public final class MessageExtension<FieldType: ExtensionField, MessageType: Message>: AnyMessageExtension {
  29. public let fieldNumber: Int
  30. public let fieldName: String
  31. public let messageType: any Message.Type
  32. public init(_protobuf_fieldNumber: Int, fieldName: String) {
  33. self.fieldNumber = _protobuf_fieldNumber
  34. self.fieldName = fieldName
  35. self.messageType = MessageType.self
  36. }
  37. public func _protobuf_newField<D: Decoder>(decoder: inout D) throws -> (any AnyExtensionField)? {
  38. try FieldType(protobufExtension: self, decoder: &decoder)
  39. }
  40. }