mirror of https://github.com/status-im/nim-rlp.git
Add support for SomeOrdinal
This commit is contained in:
parent
ce79722d43
commit
b082a14a8b
6
rlp.nim
6
rlp.nim
|
@ -184,7 +184,7 @@ proc toInt*(self: Rlp, IntType: typedesc): IntType =
|
|||
raise newException(BadCastError, "")
|
||||
|
||||
for i in payloadStart ..< (payloadStart + payloadSize):
|
||||
result = (result shl 8) or int(self.bytes[self.position + i])
|
||||
result = cast[IntType](result shl 8) or cast[IntType](self.bytes[self.position + i])
|
||||
|
||||
proc toString*(self: Rlp): string =
|
||||
if not isBlob():
|
||||
|
@ -266,8 +266,8 @@ proc read*(rlp: var Rlp, T: type string): string =
|
|||
result = rlp.toString
|
||||
rlp.skipElem
|
||||
|
||||
proc read*(rlp: var Rlp, T: type int): int =
|
||||
result = rlp.toInt(int)
|
||||
proc read*(rlp: var Rlp, T: type Integer): Integer =
|
||||
result = rlp.toInt(T)
|
||||
rlp.skipElem
|
||||
|
||||
proc read*[E](rlp: var Rlp, T: typedesc[seq[E]]): T =
|
||||
|
|
|
@ -7,7 +7,7 @@ description = "RLP serialization library for Nim"
|
|||
license = "Apache2"
|
||||
skipDirs = @["tests"]
|
||||
|
||||
requires "nim >= 0.17.0", "ranges >= 0.0.1"
|
||||
requires "nim >= 0.17.0", "https://github.com/status-im/nim-ranges >= 0.0.1"
|
||||
|
||||
proc configForTests() =
|
||||
--hints: off
|
||||
|
|
|
@ -15,4 +15,3 @@ macro rlpFields*(T: typedesc, fields: varargs[untyped]): untyped =
|
|||
result = quote do:
|
||||
template enumerateRlpFields*(`ins`: `T`, `op`: untyped) {.inject.} =
|
||||
`body`
|
||||
|
||||
|
|
|
@ -13,18 +13,17 @@ type
|
|||
|
||||
PrematureFinalizationError* = object of Exception
|
||||
|
||||
IntLike* = concept x, y
|
||||
x + y
|
||||
x * y
|
||||
x - y
|
||||
x div y
|
||||
x mod y
|
||||
x shr y
|
||||
x shl y
|
||||
IntLike* = concept x, y, type T
|
||||
x + y is T
|
||||
x * y is T
|
||||
x - y is T
|
||||
x div y is T
|
||||
x mod y is T
|
||||
x shr y is T
|
||||
x shl y is T
|
||||
x and int # for masking
|
||||
|
||||
# Integer* = SomeOrdinal or IntLike
|
||||
Integer = int
|
||||
Integer* = SomeOrdinal or IntLike
|
||||
|
||||
proc bytesNeeded(num: Integer): int =
|
||||
var n = num
|
||||
|
@ -154,7 +153,7 @@ proc append*(self; data: MemRange) =
|
|||
proc append*(self; i: Integer) =
|
||||
if i == 0:
|
||||
self.output.add BLOB_START_MARKER
|
||||
elif i < Integer(BLOB_START_MARKER):
|
||||
elif i < BLOB_START_MARKER.Integer:
|
||||
self.output.add byte(i)
|
||||
else:
|
||||
let bytesNeeded = i.bytesNeeded
|
||||
|
|
|
@ -9,7 +9,7 @@ type
|
|||
receiver: string
|
||||
|
||||
Foo = object
|
||||
x: int
|
||||
x: uint32
|
||||
y: string
|
||||
z: seq[int]
|
||||
|
||||
|
@ -27,7 +27,7 @@ proc default(T: typedesc): T = discard
|
|||
|
||||
test "encoding and decoding an object":
|
||||
var originalBar = Bar(b: "abracadabra",
|
||||
f: Foo(x: 5, y: "hocus pocus", z: @[100, 200, 300]))
|
||||
f: Foo(x: 5'u32, y: "hocus pocus", z: @[100, 200, 300]))
|
||||
|
||||
var bytes = encode(originalBar)
|
||||
|
||||
|
|
Loading…
Reference in New Issue