Some renames to cater to the latest FastStreams API

This commit is contained in:
Zahary Karadjov 2020-05-06 15:46:32 +03:00
parent 5fa6bb2742
commit c500d3dda1
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
2 changed files with 11 additions and 11 deletions

View File

@ -62,7 +62,7 @@ type
bytes*: array[10, byte] bytes*: array[10, byte]
totalBytesWritten*: int totalBytesWritten*: int
func append*(buf: var VarintBuffer, b: byte) = func write*(buf: var VarintBuffer, b: byte) =
buf.bytes[buf.totalBytesWritten] = b buf.bytes[buf.totalBytesWritten] = b
inc buf.totalBytesWritten inc buf.totalBytesWritten
@ -171,11 +171,11 @@ func readVarint*[Stream](input: var Stream,
raise newException(EOFError, "Failed to read a varint") raise newException(EOFError, "Failed to read a varint")
proc appendVarintImpl[Stream](s: var Stream, x: SomeUnsignedInt) {.inline.} = proc writeVarintImpl[Stream](s: var Stream, x: SomeUnsignedInt) {.inline.} =
mixin append mixin write
if x <= 0x7F: if x <= 0x7F:
s.append byte(x and 0xFF) s.write byte(x and 0xFF)
else: else:
var x = x var x = x
while true: while true:
@ -183,13 +183,13 @@ proc appendVarintImpl[Stream](s: var Stream, x: SomeUnsignedInt) {.inline.} =
x = x shr 7 x = x shr 7
if x == 0: if x == 0:
nextByte = nextByte and 0x7F nextByte = nextByte and 0x7F
s.append nextByte s.write nextByte
return return
else: else:
s.append nextByte s.write nextByte
proc appendVarint*[Stream](s: var Stream, x: SomeInteger, proc writeVarint*[Stream](s: var Stream, x: SomeInteger,
flavour: static VarintFlavour = ProtoBuf) {.inline.} = flavour: static VarintFlavour = ProtoBuf) {.inline.} =
## Writes a varint to a stream (e.g. faststreams.OutputStream) ## Writes a varint to a stream (e.g. faststreams.OutputStream)
when x is SomeSignedInt: when x is SomeSignedInt:
type UInt = (when sizeof(x) == 8: uint64 type UInt = (when sizeof(x) == 8: uint64
@ -202,7 +202,7 @@ proc appendVarint*[Stream](s: var Stream, x: SomeInteger,
when flavour == LibP2P and sizeof(x) == 8: when flavour == LibP2P and sizeof(x) == 8:
doAssert(x.getBitBE(0) == false) doAssert(x.getBitBE(0) == false)
s.appendVarintImpl x s.writeVarintImpl x
func vsizeof*(x: SomeInteger): int {.inline.} = func vsizeof*(x: SomeInteger): int {.inline.} =
## Returns number of bytes required to encode integer ``x`` as varint. ## Returns number of bytes required to encode integer ``x`` as varint.
@ -212,7 +212,7 @@ func vsizeof*(x: SomeInteger): int {.inline.} =
template varintBytes*(x: SomeInteger, template varintBytes*(x: SomeInteger,
flavour: static VarintFlavour = ProtoBuf): untyped = flavour: static VarintFlavour = ProtoBuf): untyped =
var buf: VarintBuffer var buf: VarintBuffer
buf.appendVarint(x, flavour) buf.writeVarint(x, flavour)
# TODO: toOpenArray doesn't work here for some reason, so we must # TODO: toOpenArray doesn't work here for some reason, so we must
# use the less optimal approach of allocating a sequence copy. # use the less optimal approach of allocating a sequence copy.
# buf.bytes.toOpenArray(0, buf.totalBytesWritten - 1) # buf.bytes.toOpenArray(0, buf.totalBytesWritten - 1)

View File

@ -28,7 +28,7 @@ const edgeValues = {
suite "varints": suite "varints":
template roundtipTest(val) = template roundtipTest(val) =
var s {.inject.}: VarintBuffer var s {.inject.}: VarintBuffer
s.appendVarint val s.writeVarint val
var roundtripVal: uint64 var roundtripVal: uint64
let bytesRead = readVarint(s.bytes, roundtripVal) let bytesRead = readVarint(s.bytes, roundtripVal)