1- // Sources/SwiftProtobuf/ProtobufTypeAdditions .swift - Per-type binary coding
1+ // Sources/SwiftProtobuf/BinaryTypeAdditions .swift - Per-type binary coding
22//
33// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
44// Licensed under Apache License v2.0 with Runtime Library Exception
1313///
1414// -----------------------------------------------------------------------------
1515
16- import Swift
1716import Foundation
1817
1918// TODO: Examine how other proto2 implementations treat wire type mismatches
@@ -27,7 +26,7 @@ import Foundation
2726///
2827extension ProtobufFloat {
2928 public static var protobufWireFormat : WireFormat { return . fixed32 }
30- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: Float ) {
29+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: Float ) {
3130 encoder. putFloatValue ( value: value)
3231 }
3332
@@ -42,7 +41,7 @@ extension ProtobufFloat {
4241///
4342extension ProtobufDouble {
4443 public static var protobufWireFormat : WireFormat { return . fixed64 }
45- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: Double ) {
44+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: Double ) {
4645 encoder. putDoubleValue ( value: value)
4746 }
4847
@@ -57,7 +56,7 @@ extension ProtobufDouble {
5756extension ProtobufInt32 {
5857 public static var protobufWireFormat : WireFormat { return . varint }
5958
60- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: Int32 ) {
59+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: Int32 ) {
6160 encoder. putVarInt ( value: Int64 ( value) )
6261 }
6362
@@ -72,7 +71,7 @@ extension ProtobufInt32 {
7271extension ProtobufInt64 {
7372 public static var protobufWireFormat : WireFormat { return . varint }
7473
75- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: Int64 ) {
74+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: Int64 ) {
7675 encoder. putVarInt ( value: value)
7776 }
7877
@@ -87,7 +86,7 @@ extension ProtobufInt64 {
8786extension ProtobufUInt32 {
8887 public static var protobufWireFormat : WireFormat { return . varint }
8988
90- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: UInt32 ) {
89+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: UInt32 ) {
9190 encoder. putVarInt ( value: UInt64 ( value) )
9291 }
9392
@@ -102,7 +101,7 @@ extension ProtobufUInt32 {
102101extension ProtobufUInt64 {
103102 public static var protobufWireFormat : WireFormat { return . varint }
104103
105- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: UInt64 ) {
104+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: UInt64 ) {
106105 encoder. putVarInt ( value: value)
107106 }
108107
@@ -117,7 +116,7 @@ extension ProtobufUInt64 {
117116extension ProtobufSInt32 {
118117 public static var protobufWireFormat : WireFormat { return . varint }
119118
120- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: Int32 ) {
119+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: Int32 ) {
121120 encoder. putZigZagVarInt ( value: Int64 ( value) )
122121 }
123122
@@ -132,7 +131,7 @@ extension ProtobufSInt32 {
132131extension ProtobufSInt64 {
133132 public static var protobufWireFormat : WireFormat { return . varint }
134133
135- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: Int64 ) {
134+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: Int64 ) {
136135 encoder. putZigZagVarInt ( value: value)
137136 }
138137
@@ -146,7 +145,7 @@ extension ProtobufSInt64 {
146145///
147146extension ProtobufFixed32 {
148147 public static var protobufWireFormat : WireFormat { return . fixed32 }
149- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: UInt32 ) {
148+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: UInt32 ) {
150149 encoder. putFixedUInt32 ( value: value)
151150 }
152151
@@ -160,7 +159,7 @@ extension ProtobufFixed32 {
160159///
161160extension ProtobufFixed64 {
162161 public static var protobufWireFormat : WireFormat { return . fixed64 }
163- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: UInt64 ) {
162+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: UInt64 ) {
164163 encoder. putFixedUInt64 ( value: value. littleEndian)
165164 }
166165
@@ -174,7 +173,7 @@ extension ProtobufFixed64 {
174173///
175174extension ProtobufSFixed32 {
176175 public static var protobufWireFormat : WireFormat { return . fixed32 }
177- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: Int32 ) {
176+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: Int32 ) {
178177 encoder. putFixedUInt32 ( value: UInt32 ( bitPattern: value) )
179178 }
180179
@@ -188,7 +187,7 @@ extension ProtobufSFixed32 {
188187///
189188extension ProtobufSFixed64 {
190189 public static var protobufWireFormat : WireFormat { return . fixed64 }
191- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: Int64 ) {
190+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: Int64 ) {
192191 encoder. putFixedUInt64 ( value: UInt64 ( bitPattern: value. littleEndian) )
193192 }
194193
@@ -203,7 +202,7 @@ extension ProtobufSFixed64 {
203202extension ProtobufBool {
204203 public static var protobufWireFormat : WireFormat { return . varint }
205204
206- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: Bool ) {
205+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: Bool ) {
207206 encoder. putBoolValue ( value: value)
208207 }
209208
@@ -218,7 +217,7 @@ extension ProtobufBool {
218217
219218extension ProtobufString {
220219 public static var protobufWireFormat : WireFormat { return . lengthDelimited }
221- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: String ) {
220+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: String ) {
222221 encoder. putStringValue ( value: value)
223222 }
224223
@@ -235,7 +234,7 @@ extension ProtobufString {
235234extension ProtobufBytes {
236235 public static var protobufWireFormat : WireFormat { return . lengthDelimited }
237236
238- public static func serializeProtobufValue( encoder: inout ProtobufEncoder , value: Data ) {
237+ public static func serializeProtobufValue( encoder: inout BinaryEncoder , value: Data ) {
239238 encoder. putBytesValue ( value: value)
240239 }
241240
@@ -259,12 +258,12 @@ public extension Message {
259258 }
260259
261260 private func serializeBinary( into pointer: UnsafeMutablePointer < UInt8 > ) throws {
262- let visitor = ProtobufEncodingVisitor ( forWritingInto: pointer)
261+ let visitor = BinaryEncodingVisitor ( forWritingInto: pointer)
263262 try traverse ( visitor: visitor)
264263 }
265264
266265 internal func serializedDataSize( ) throws -> Int {
267- let visitor = ProtobufEncodingSizeVisitor ( )
266+ let visitor = BinaryEncodingSizeVisitor ( )
268267 try traverse ( visitor: visitor)
269268 return visitor. serializedSize
270269 }
@@ -283,25 +282,25 @@ public extension Message {
283282
284283/// Proto2 messages preserve unknown fields
285284public extension Proto2Message {
286- public mutating func decodeBinary( from protobufBytes : UnsafePointer < UInt8 > , count: Int , extensions: ExtensionSet ? ) throws {
287- var protobufDecoder = ProtobufDecoder ( protobufPointer : protobufBytes , count: count, extensions: extensions)
288- try decodeMessage ( decoder: & protobufDecoder )
289- if !protobufDecoder . complete {
290- throw ProtobufDecodingError . trailingGarbage
285+ public mutating func decodeBinary( from bytes : UnsafePointer < UInt8 > , count: Int , extensions: ExtensionSet ? ) throws {
286+ var decoder = BinaryDecoder ( forReadingFrom : bytes , count: count, extensions: extensions)
287+ try decodeMessage ( decoder: & decoder )
288+ guard decoder . complete else {
289+ throw BinaryDecodingError . trailingGarbage
291290 }
292- if let unknownData = protobufDecoder . unknownData {
291+ if let unknownData = decoder . unknownData {
293292 unknownFields. append ( protobufData: unknownData)
294293 }
295294 }
296295}
297296
298297// Proto3 messages ignore unknown fields
299298public extension Proto3Message {
300- public mutating func decodeBinary( from protobufBytes : UnsafePointer < UInt8 > , count: Int , extensions: ExtensionSet ? ) throws {
301- var protobufDecoder = ProtobufDecoder ( protobufPointer : protobufBytes , count: count, extensions: extensions)
302- try decodeMessage ( decoder: & protobufDecoder )
303- if !protobufDecoder . complete {
304- throw ProtobufDecodingError . trailingGarbage
299+ public mutating func decodeBinary( from bytes : UnsafePointer < UInt8 > , count: Int , extensions: ExtensionSet ? ) throws {
300+ var decoder = BinaryDecoder ( forReadingFrom : bytes , count: count, extensions: extensions)
301+ try decodeMessage ( decoder: & decoder )
302+ guard decoder . complete else {
303+ throw BinaryDecodingError . trailingGarbage
305304 }
306305 }
307306}
0 commit comments