mirror of
https://github.com/status-im/nim-rlp.git
synced 2025-01-24 13:31:23 +00:00
add support for reading and writing enums
This commit is contained in:
parent
e0fa989449
commit
f903ad8bff
4
rlp.nim
4
rlp.nim
@ -271,6 +271,10 @@ proc read*(rlp: var Rlp, T: type Integer): Integer =
|
|||||||
result = rlp.toInt(T)
|
result = rlp.toInt(T)
|
||||||
rlp.skipElem
|
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 =
|
proc read*[E](rlp: var Rlp, T: typedesc[seq[E]]): T =
|
||||||
if not rlp.isList:
|
if not rlp.isList:
|
||||||
raise newException(BadCastError, "The source RLP is not a list.")
|
raise newException(BadCastError, "The source RLP is not a list.")
|
||||||
|
@ -23,14 +23,15 @@ type
|
|||||||
x shl y is T
|
x shl y is T
|
||||||
x and int # for masking
|
x and int # for masking
|
||||||
|
|
||||||
Integer* = SomeOrdinal or IntLike or uint or uint64
|
Integer* = SomeInteger or IntLike
|
||||||
|
|
||||||
const
|
const
|
||||||
wrapObjectsInList* = true
|
wrapObjectsInList* = true
|
||||||
|
|
||||||
proc bytesNeeded(num: Integer): int =
|
proc bytesNeeded(num: Integer): int =
|
||||||
|
type IntType = type(num)
|
||||||
var n = num
|
var n = num
|
||||||
while n != 0:
|
while n != IntType(0):
|
||||||
inc result
|
inc result
|
||||||
n = n shr 8
|
n = n shr 8
|
||||||
|
|
||||||
@ -154,7 +155,9 @@ proc append*(self; data: MemRange) =
|
|||||||
appendImpl(self, data, BLOB_START_MARKER)
|
appendImpl(self, data, BLOB_START_MARKER)
|
||||||
|
|
||||||
proc append*(self; i: Integer) =
|
proc append*(self; i: Integer) =
|
||||||
if i == 0:
|
type IntType = type(i)
|
||||||
|
|
||||||
|
if i == IntType(0):
|
||||||
self.output.add BLOB_START_MARKER
|
self.output.add BLOB_START_MARKER
|
||||||
elif i < BLOB_START_MARKER.Integer:
|
elif i < BLOB_START_MARKER.Integer:
|
||||||
self.output.add byte(i)
|
self.output.add byte(i)
|
||||||
@ -165,6 +168,9 @@ proc append*(self; i: Integer) =
|
|||||||
|
|
||||||
self.maybeClosePendingLists()
|
self.maybeClosePendingLists()
|
||||||
|
|
||||||
|
template append*(self; e: enum) =
|
||||||
|
append(self, int(e))
|
||||||
|
|
||||||
proc append*[T](self; listOrBlob: openarray[T]) =
|
proc append*[T](self; listOrBlob: openarray[T]) =
|
||||||
# TODO: This append proc should be overloaded by `openarray[byte]` after
|
# TODO: This append proc should be overloaded by `openarray[byte]` after
|
||||||
# nim bug #7416 is fixed.
|
# nim bug #7416 is fixed.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user