add support for reading and writing enums

This commit is contained in:
Zahary Karadjov 2018-04-01 04:37:06 +03:00
parent e0fa989449
commit f903ad8bff
2 changed files with 13 additions and 3 deletions

View File

@ -271,6 +271,10 @@ proc read*(rlp: var Rlp, T: type Integer): Integer =
result = rlp.toInt(T)
rlp.skipElem
proc read*(rlp: var Rlp, T: typedesc[enum]): T =
result = T(rlp.toInt(int))
rlp.skipElem
proc read*[E](rlp: var Rlp, T: typedesc[seq[E]]): T =
if not rlp.isList:
raise newException(BadCastError, "The source RLP is not a list.")

View File

@ -23,14 +23,15 @@ type
x shl y is T
x and int # for masking
Integer* = SomeOrdinal or IntLike or uint or uint64
Integer* = SomeInteger or IntLike
const
wrapObjectsInList* = true
proc bytesNeeded(num: Integer): int =
type IntType = type(num)
var n = num
while n != 0:
while n != IntType(0):
inc result
n = n shr 8
@ -154,7 +155,9 @@ proc append*(self; data: MemRange) =
appendImpl(self, data, BLOB_START_MARKER)
proc append*(self; i: Integer) =
if i == 0:
type IntType = type(i)
if i == IntType(0):
self.output.add BLOB_START_MARKER
elif i < BLOB_START_MARKER.Integer:
self.output.add byte(i)
@ -165,6 +168,9 @@ proc append*(self; i: Integer) =
self.maybeClosePendingLists()
template append*(self; e: enum) =
append(self, int(e))
proc append*[T](self; listOrBlob: openarray[T]) =
# TODO: This append proc should be overloaded by `openarray[byte]` after
# nim bug #7416 is fixed.