From 63af57837d17b13446cd4db9f830abdd4ceb8b68 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Wed, 16 May 2018 10:37:26 +0300 Subject: [PATCH] Workaround nim bug, startList made public --- rlp/writer.nim | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/rlp/writer.nim b/rlp/writer.nim index 87c9391..deca882 100644 --- a/rlp/writer.nim +++ b/rlp/writer.nim @@ -120,7 +120,7 @@ proc appendRawList(self; bytes: BytesRange) = output.add(bytes) maybeClosePendingLists() -proc startList(self; listSize: int) = +proc startList*(self; listSize: int) = if listSize == 0: appendRawList(BytesRange()) else: @@ -186,24 +186,30 @@ proc append*[T](self; listOrBlob: openarray[T]) = for i in 0 ..< listOrBlob.len: self.append listOrBlob[i] -proc append*(self; data: object|tuple, wrapInList = wrapObjectsInList) = +proc appendTupleOrObject(self; data: object|tuple, wrapInList: bool) = + mixin enumerateRlpFields, append + + const wrapInList = wrapObjectsInList + + if wrapInList: + var fieldsCount = 0 + template countFields(x) = inc fieldsCount + enumerateRlpFields(data, countFields) + self.startList(fieldsCount) + + template op(x) = append(self, x) + enumerateRlpFields(data, op) + +proc append*(self; data: object, wrapInList = wrapObjectsInList) {.inline.} = # TODO: This append proc should be overloaded by `BytesRange` after # nim bug #7416 is fixed. when data is BytesRange: - appendBytesRange(data) + self.appendBytesRange(data) else: - mixin enumerateRlpFields, append + self.appendTupleOrObject(data, wrapInList) - const wrapInList = wrapObjectsInList - - if wrapInList: - var fieldsCount = 0 - template countFields(x) = inc fieldsCount - enumerateRlpFields(data, countFields) - self.startList(fieldsCount) - - template op(x) = append(self, x) - enumerateRlpFields(data, op) +proc append*(self; data: tuple, wrapInList = wrapObjectsInList) {.inline.} = + self.appendTupleOrObject(data, wrapInList) proc initRlpList*(listSize: int): RlpWriter = result = initRlpWriter()