rlp.read for openarrays

This commit is contained in:
Zahary Karadjov 2018-04-01 05:20:46 +03:00
parent 09718bb5c3
commit 8acf725628
2 changed files with 12 additions and 1 deletions

View File

@ -276,6 +276,8 @@ proc read*(rlp: var Rlp, T: typedesc[enum]): T =
rlp.skipElem
proc read*[R, E](rlp: var Rlp, T: type array[R, E]): T =
mixin read
when E is byte:
if not rlp.isBlob:
raise newException(BadCastError, "The source RLP is not a blob.")
@ -297,7 +299,9 @@ proc read*[R, E](rlp: var Rlp, T: type array[R, E]): T =
result[i] = rlp.read(E)
inc i
proc read*[E](rlp: var Rlp, T: typedesc[seq[E]]): T =
proc read*[E](rlp: var Rlp, T: type seq[E]): T =
mixin read
when E is byte:
var bytes = rlp.toBytes
result = newSeq[byte](bytes.len)
@ -311,6 +315,9 @@ proc read*[E](rlp: var Rlp, T: typedesc[seq[E]]): T =
for elem in rlp:
result.add rlp.read(E)
proc read*[E](rlp: var Rlp, T: type openarray[E]): seq[E] =
result = read(rlp, seq[E])
proc read*(rlp: var Rlp, T: typedesc[object|tuple],
wrappedInList = wrapObjectsInList): T =
mixin enumerateRlpFields, read

View File

@ -172,6 +172,8 @@ template append*(self; e: enum) =
append(self, int(e))
proc append*[T](self; listOrBlob: openarray[T]) =
mixin append
# TODO: This append proc should be overloaded by `openarray[byte]` after
# nim bug #7416 is fixed.
when T is byte:
@ -182,6 +184,8 @@ proc append*[T](self; listOrBlob: openarray[T]) =
self.append listOrBlob[i]
proc append*[T](self; list: seq[T]) =
mixin append
self.startList list.len
for i in 0 ..< list.len:
self.append list[i]