/src/swift-protobuf/Sources/SwiftProtobuf/MessageExtension.swift
Line | Count | Source |
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 | | |
18 | | /// Type-erased MessageExtension field implementation. |
19 | | @preconcurrency |
20 | | public protocol AnyMessageExtension: Sendable { |
21 | | var fieldNumber: Int { get } |
22 | | var fieldName: String { get } |
23 | | var messageType: any Message.Type { get } |
24 | | func _protobuf_newField<D: Decoder>(decoder: inout D) throws -> (any AnyExtensionField)? |
25 | | } |
26 | | |
27 | | /// A "Message Extension" relates a particular extension field to |
28 | | /// a particular message. The generic constraints allow |
29 | | /// compile-time compatibility checks. |
30 | | public final class MessageExtension<FieldType: ExtensionField, MessageType: Message>: AnyMessageExtension { |
31 | | public let fieldNumber: Int |
32 | | public let fieldName: String |
33 | | public let messageType: any Message.Type |
34 | 1.30k | public init(_protobuf_fieldNumber: Int, fieldName: String) { |
35 | 1.30k | self.fieldNumber = _protobuf_fieldNumber |
36 | 1.30k | self.fieldName = fieldName |
37 | 1.30k | self.messageType = MessageType.self |
38 | 1.30k | } |
39 | 624k | public func _protobuf_newField<D: Decoder>(decoder: inout D) throws -> (any AnyExtensionField)? { |
40 | 624k | try FieldType(protobufExtension: self, decoder: &decoder) |
41 | 619k | } |
42 | | } |