From c500d3dda1f3cb9df0aed8ded83ef58af293cfb1 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Wed, 6 May 2020 15:46:32 +0300 Subject: [PATCH] Some renames to cater to the latest FastStreams API --- stew/varints.nim | 20 ++++++++++---------- tests/test_varints.nim | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/stew/varints.nim b/stew/varints.nim index 0260e7e..d418367 100644 --- a/stew/varints.nim +++ b/stew/varints.nim @@ -62,7 +62,7 @@ type bytes*: array[10, byte] totalBytesWritten*: int -func append*(buf: var VarintBuffer, b: byte) = +func write*(buf: var VarintBuffer, b: byte) = buf.bytes[buf.totalBytesWritten] = b inc buf.totalBytesWritten @@ -171,11 +171,11 @@ func readVarint*[Stream](input: var Stream, raise newException(EOFError, "Failed to read a varint") -proc appendVarintImpl[Stream](s: var Stream, x: SomeUnsignedInt) {.inline.} = - mixin append +proc writeVarintImpl[Stream](s: var Stream, x: SomeUnsignedInt) {.inline.} = + mixin write if x <= 0x7F: - s.append byte(x and 0xFF) + s.write byte(x and 0xFF) else: var x = x while true: @@ -183,13 +183,13 @@ proc appendVarintImpl[Stream](s: var Stream, x: SomeUnsignedInt) {.inline.} = x = x shr 7 if x == 0: nextByte = nextByte and 0x7F - s.append nextByte + s.write nextByte return else: - s.append nextByte + s.write nextByte -proc appendVarint*[Stream](s: var Stream, x: SomeInteger, - flavour: static VarintFlavour = ProtoBuf) {.inline.} = +proc writeVarint*[Stream](s: var Stream, x: SomeInteger, + flavour: static VarintFlavour = ProtoBuf) {.inline.} = ## Writes a varint to a stream (e.g. faststreams.OutputStream) when x is SomeSignedInt: 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: doAssert(x.getBitBE(0) == false) - s.appendVarintImpl x + s.writeVarintImpl x func vsizeof*(x: SomeInteger): int {.inline.} = ## 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, flavour: static VarintFlavour = ProtoBuf): untyped = var buf: VarintBuffer - buf.appendVarint(x, flavour) + buf.writeVarint(x, flavour) # TODO: toOpenArray doesn't work here for some reason, so we must # use the less optimal approach of allocating a sequence copy. # buf.bytes.toOpenArray(0, buf.totalBytesWritten - 1) diff --git a/tests/test_varints.nim b/tests/test_varints.nim index 459cef6..00d5df5 100644 --- a/tests/test_varints.nim +++ b/tests/test_varints.nim @@ -28,7 +28,7 @@ const edgeValues = { suite "varints": template roundtipTest(val) = var s {.inject.}: VarintBuffer - s.appendVarint val + s.writeVarint val var roundtripVal: uint64 let bytesRead = readVarint(s.bytes, roundtripVal)