@@ -73,6 +73,7 @@ extension Message {
7373 /// `BinaryEncodingError.missingRequiredFields`.
7474 /// - options: The BinaryDecodingOptions to use.
7575 /// - Throws: `BinaryDecodingError` if decoding fails.
76+ @inlinable
7677 public init (
7778 serializedData data: Data ,
7879 extensions: ExtensionMap ? = nil ,
@@ -102,8 +103,9 @@ extension Message {
102103 /// `BinaryEncodingError.missingRequiredFields`.
103104 /// - options: The BinaryDecodingOptions to use.
104105 /// - Throws: `BinaryDecodingError` if decoding fails.
105- public init (
106- contiguousBytes bytes: ContiguousBytes ,
106+ @inlinable
107+ public init < Bytes: ContiguousBytes > (
108+ contiguousBytes bytes: Bytes ,
107109 extensions: ExtensionMap ? = nil ,
108110 partial: Bool = false ,
109111 options: BinaryDecodingOptions = BinaryDecodingOptions ( )
@@ -131,6 +133,7 @@ extension Message {
131133 /// `BinaryEncodingError.missingRequiredFields`.
132134 /// - options: The BinaryDecodingOptions to use.
133135 /// - Throws: `BinaryDecodingError` if decoding fails.
136+ @inlinable
134137 public mutating func merge(
135138 serializedData data: Data ,
136139 extensions: ExtensionMap ? = nil ,
@@ -140,20 +143,8 @@ extension Message {
140143#if swift(>=5.0)
141144 try merge ( contiguousBytes: data, extensions: extensions, partial: partial, options: options)
142145#else
143- if !data. isEmpty {
144- try data. withUnsafeBytes { ( body: UnsafeRawBufferPointer ) in
145- if let baseAddress = body. baseAddress, body. count > 0 {
146- let pointer = baseAddress. assumingMemoryBound ( to: UInt8 . self)
147- var decoder = BinaryDecoder ( forReadingFrom: pointer,
148- count: body. count,
149- options: options,
150- extensions: extensions)
151- try decoder. decodeFullMessage ( message: & self )
152- }
153- }
154- }
155- if !partial && !isInitialized {
156- throw BinaryDecodingError . missingRequiredFields
146+ try data. withUnsafeBytes { ( body: UnsafeRawBufferPointer ) in
147+ try _merge ( rawBuffer: body, extensions: extensions, partial: partial, options: options)
157148 }
158149#endif // swift(>=5.0)
159150 }
@@ -178,25 +169,39 @@ extension Message {
178169 /// `BinaryEncodingError.missingRequiredFields`.
179170 /// - options: The BinaryDecodingOptions to use.
180171 /// - Throws: `BinaryDecodingError` if decoding fails.
181- public mutating func merge(
182- contiguousBytes bytes: ContiguousBytes ,
172+ @inlinable
173+ public mutating func merge< Bytes: ContiguousBytes > (
174+ contiguousBytes bytes: Bytes ,
183175 extensions: ExtensionMap ? = nil ,
184176 partial: Bool = false ,
185177 options: BinaryDecodingOptions = BinaryDecodingOptions ( )
186178 ) throws {
187179 try bytes. withUnsafeBytes { ( body: UnsafeRawBufferPointer ) in
188- if let baseAddress = body. baseAddress, body. count > 0 {
189- let pointer = baseAddress. assumingMemoryBound ( to: UInt8 . self)
190- var decoder = BinaryDecoder ( forReadingFrom: pointer,
191- count: body. count,
192- options: options,
193- extensions: extensions)
194- try decoder. decodeFullMessage ( message: & self )
195- }
180+ try _merge ( rawBuffer: body, extensions: extensions, partial: partial, options: options)
181+ }
182+ }
183+ #endif // swift(>=5.0)
184+
185+ // Helper for `merge()`s to keep the Decoder internal to SwiftProtobuf while
186+ // allowing the generic over ContiguousBytes to get better codegen from the
187+ // compiler by being `@inlinable`.
188+ @usableFromInline
189+ internal mutating func _merge(
190+ rawBuffer body: UnsafeRawBufferPointer ,
191+ extensions: ExtensionMap ? ,
192+ partial: Bool ,
193+ options: BinaryDecodingOptions
194+ ) throws {
195+ if let baseAddress = body. baseAddress, body. count > 0 {
196+ let pointer = baseAddress. assumingMemoryBound ( to: UInt8 . self)
197+ var decoder = BinaryDecoder ( forReadingFrom: pointer,
198+ count: body. count,
199+ options: options,
200+ extensions: extensions)
201+ try decoder. decodeFullMessage ( message: & self )
196202 }
197203 if !partial && !isInitialized {
198204 throw BinaryDecodingError . missingRequiredFields
199205 }
200206 }
201- #endif // swift(>=5.0)
202207}
0 commit comments