diff --git a/.travis.yml b/.travis.yml index b8fd3a5..ba786ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,5 +25,6 @@ before_install: export PATH=$PWD/bin:$PATH cd .. script: + - nimble install -y - nimble test diff --git a/rlp.nim b/rlp.nim index 33425ee..3968897 100644 --- a/rlp.nim +++ b/rlp.nim @@ -168,7 +168,7 @@ proc isInt*(self: Rlp): bool = return bytes[offset] != 0 return false -template maxBytes*(o: typedesc[Ordinal]): int = sizeof(o) +template maxBytes*(o: typedesc[Ordinal | uint64 | uint]): int = sizeof(o) proc toInt*(self: Rlp, IntType: typedesc): IntType = # XXX: self insertions are not working in generic procs @@ -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 = @@ -306,6 +306,7 @@ proc decode*(bytes: openarray[byte]): RlpNode = bytesCopy = @bytes rlp = rlpFromBytes initBytesRange(bytesCopy) return rlp.toNodes + template decode*(bytes: BytesRange, T: typedesc): untyped = var rlp = rlpFromBytes bytes diff --git a/rlp.nimble b/rlp.nimble index 873eb76..a988cb7 100644 --- a/rlp.nimble +++ b/rlp.nimble @@ -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 diff --git a/rlp/object_serialization.nim b/rlp/object_serialization.nim index 37c43bf..2effbc8 100644 --- a/rlp/object_serialization.nim +++ b/rlp/object_serialization.nim @@ -15,4 +15,3 @@ macro rlpFields*(T: typedesc, fields: varargs[untyped]): untyped = result = quote do: template enumerateRlpFields*(`ins`: `T`, `op`: untyped) {.inject.} = `body` - diff --git a/rlp/writer.nim b/rlp/writer.nim index 471b642..e508928 100644 --- a/rlp/writer.nim +++ b/rlp/writer.nim @@ -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 or uint or uint64 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 diff --git a/tests/test_object_serialization.nim b/tests/test_object_serialization.nim index 356158b..f42ff9e 100644 --- a/tests/test_object_serialization.nim +++ b/tests/test_object_serialization.nim @@ -9,7 +9,7 @@ type receiver: string Foo = object - x: int + x: uint64 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'u64, y: "hocus pocus", z: @[100, 200, 300])) var bytes = encode(originalBar)