aboutsummaryrefslogtreecommitdiff
path: root/src/protobuf
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2018-03-27 20:17:06 +0300
committerOskari Timperi <oskari.timperi@iki.fi>2018-03-27 20:17:06 +0300
commitd76ec81388454c24ee99a601830ed39cfa50063c (patch)
tree0248dbbce5ff28ae7c7cd04c0254bbd9a7bfca06 /src/protobuf
parent1a7bfab6a74ef3001c619891970818f2dcb434ea (diff)
downloadnimpb-d76ec81388454c24ee99a601830ed39cfa50063c.tar.gz
nimpb-d76ec81388454c24ee99a601830ed39cfa50063c.zip
Get rid of the distinct numeric types
We have distinct procs for reading and writing differently encoded data, so we don't need distinct types in this case.
Diffstat (limited to 'src/protobuf')
-rw-r--r--src/protobuf/gen.nim20
-rw-r--r--src/protobuf/stream.nim30
-rw-r--r--src/protobuf/types.nim6
3 files changed, 25 insertions, 31 deletions
diff --git a/src/protobuf/gen.nim b/src/protobuf/gen.nim
index 512c0c5..fd3b3eb 100644
--- a/src/protobuf/gen.nim
+++ b/src/protobuf/gen.nim
@@ -79,16 +79,16 @@ proc getFieldTypeAsString(field: NimNode): string =
of FieldType.Int64: result = "int64"
of FieldType.UInt64: result = "uint64"
of FieldType.Int32: result = "int32"
- of FieldType.Fixed64: result = "fixed64"
- of FieldType.Fixed32: result = "fixed32"
+ of FieldType.Fixed64: result = "uint64"
+ of FieldType.Fixed32: result = "uint32"
of FieldType.Bool: result = "bool"
of FieldType.String: result = "string"
of FieldType.Bytes: result = "bytes"
of FieldType.UInt32: result = "uint32"
- of FieldType.SFixed32: result = "sfixed32"
- of FieldType.SFixed64: result = "sfixed64"
- of FieldType.SInt32: result = "sint32"
- of FieldType.SInt64: result = "sint64"
+ of FieldType.SFixed32: result = "int32"
+ of FieldType.SFixed64: result = "int64"
+ of FieldType.SInt32: result = "int32"
+ of FieldType.SInt64: result = "int64"
else: result = "AYBABTU"
proc getFullFieldType(field: NimNode): NimNode =
@@ -129,10 +129,10 @@ proc defaultValue(field: NimNode): NimNode =
nameId = ident(getFieldTypeAsString(field))
result = quote do:
`nameId`(`descId`.values[0].number)
- of FieldType.SFixed32: result = newCall(ident("sfixed32"), newLit(0))
- of FieldType.SFixed64: result = newCall(ident("sfixed64"), newLit(0))
- of FieldType.SInt32: result = newCall(ident("sint32"), newLit(0))
- of FieldType.SInt64: result = newCall(ident("sint64"), newLit(0))
+ of FieldType.SFixed32: result = newLit(0'u32)
+ of FieldType.SFixed64: result = newLit(0'u32)
+ of FieldType.SInt32: result = newLit(0)
+ of FieldType.SInt64: result = newLit(0)
proc wiretype(field: NimNode): WireType =
result = wiretype(getFieldType(field))
diff --git a/src/protobuf/stream.nim b/src/protobuf/stream.nim
index 83673a4..74f57dd 100644
--- a/src/protobuf/stream.nim
+++ b/src/protobuf/stream.nim
@@ -163,25 +163,25 @@ proc writeBool*(stream: ProtobufStream, value: bool) =
proc readBool*(stream: ProtobufStream): bool =
result = readVarint(stream).bool
-proc writeFixed64*(stream: ProtobufStream, value: fixed64) =
+proc writeFixed64*(stream: ProtobufStream, value: uint64) =
var
input = value
- output: fixed64
+ output: uint64
littleEndian64(addr(output), addr(input))
write(stream, output)
-proc readFixed64*(stream: ProtobufStream): fixed64 =
- var tmp: fixed64
+proc readFixed64*(stream: ProtobufStream): uint64 =
+ var tmp: uint64
discard readData(stream, addr(tmp), sizeof(tmp))
littleEndian64(addr(result), addr(tmp))
-proc writeSFixed64*(stream: ProtobufStream, value: sfixed64) =
- writeFixed64(stream, cast[fixed64](value))
+proc writeSFixed64*(stream: ProtobufStream, value: int64) =
+ writeFixed64(stream, cast[uint64](value))
-proc readSFixed64*(stream: ProtobufStream): sfixed64 =
- result = cast[sfixed64](readFixed64(stream))
+proc readSFixed64*(stream: ProtobufStream): int64 =
+ result = cast[int64](readFixed64(stream))
proc writeDouble*(stream: ProtobufStream, value: float64) =
var
@@ -197,25 +197,25 @@ proc readDouble*(stream: ProtobufStream): float64 =
discard readData(stream, addr(tmp), sizeof(tmp))
littleEndian64(addr(tmp), addr(result))
-proc writeFixed32*(stream: ProtobufStream, value: fixed32) =
+proc writeFixed32*(stream: ProtobufStream, value: uint32) =
var
input = value
- output: fixed32
+ output: uint32
littleEndian32(addr(output), addr(input))
write(stream, output)
-proc readFixed32*(stream: ProtobufStream): fixed32 =
+proc readFixed32*(stream: ProtobufStream): uint32 =
var tmp: uint32
discard readData(stream, addr(tmp), sizeof(tmp))
littleEndian32(addr(tmp), addr(result))
-proc writeSFixed32*(stream: ProtobufStream, value: sfixed32) =
- writeFixed32(stream, cast[fixed32](value))
+proc writeSFixed32*(stream: ProtobufStream, value: int32) =
+ writeFixed32(stream, cast[uint32](value))
-proc readSFixed32*(stream: ProtobufStream): sfixed32 =
- result = cast[sfixed32](readFixed32(stream))
+proc readSFixed32*(stream: ProtobufStream): int32 =
+ result = cast[int32](readFixed32(stream))
proc writeFloat*(stream: ProtobufStream, value: float32) =
var
diff --git a/src/protobuf/types.nim b/src/protobuf/types.nim
index fdd26db..c87524d 100644
--- a/src/protobuf/types.nim
+++ b/src/protobuf/types.nim
@@ -1,10 +1,4 @@
type
- fixed32* = distinct uint32
- fixed64* = distinct uint64
- sfixed32* = distinct int32
- sfixed64* = distinct int64
- sint32* = distinct int32
- sint64* = distinct int64
bytes* = distinct string
# int32 # varint, no zigzag