Skip to content

Commit

Permalink
remove deprecated support for Option
Browse files Browse the repository at this point in the history
  • Loading branch information
chirag-parmar committed Oct 28, 2024
1 parent 72a8872 commit 3b53f41
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 39 deletions.
15 changes: 0 additions & 15 deletions eth/common/base_rlp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,11 @@ import std/typetraits, ./base, ../rlp

export base, rlp

# TODO why is rlp serialization of `Opt` here and not in rlp?
proc append*[T](w: var RlpWriter, val: Opt[T]) =
mixin append

if val.isSome:
w.append(val.get())
else:
w.append("")

template read*[T](rlp: var Rlp, val: var T) =
mixin read
val = rlp.read(type val)

proc read*[T](rlp: var Rlp, val: var Opt[T]) {.raises: [RlpError].} =
mixin read
if rlp.blobLen != 0:
val = Opt.some(rlp.read(T))
else:
rlp.skipElem

proc read*(rlp: var Rlp, T: type StUint): T {.raises: [RlpError].} =
if rlp.isBlob:
let bytes = rlp.toBytes
Expand Down
13 changes: 0 additions & 13 deletions eth/rlp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -448,26 +448,13 @@ func readImpl(
else:
rlp.bytes.len()

template getUnderlyingType[T](_: Option[T]): untyped =
T

template getUnderlyingType[T](_: Opt[T]): untyped =
T

template op(RecordType, fieldName, field) {.used.} =
type FieldType {.used.} = type field
when hasCustomPragmaFixed(RecordType, fieldName, rlpCustomSerialization):
field = rlp.read(result, FieldType)
elif field is Option:
# this works for optional fields at the end of an object/tuple
# if the optional field is followed by a mandatory field,
# custom serialization for a field or for the parent object
# will be better
type UT = getUnderlyingType(field)
if rlp.position < payloadEnd:
field = some(rlp.read(UT))
else:
field = none(UT)
elif field is Opt:
# this works for optional fields at the end of an object/tuple
# if the optional field is followed by a mandatory field,
Expand Down
24 changes: 13 additions & 11 deletions eth/rlp/options.nim
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import
std/options,
../rlp
import ../rlp

proc read*[T](rlp: var Rlp, O: type Option[T]): O {.inline.} =
mixin read
if not rlp.isEmpty:
result = some read(rlp, T)
proc append*[T](w: var RlpWriter, val: Opt[T]) =
mixin append

if val.isSome:
w.append(val.get())
else:
w.append("")

proc append*(writer: var RlpWriter, value: Option) =
if value.isSome:
writer.append value.get
proc read*[T](rlp: var Rlp, val: var Opt[T]) {.raises: [RlpError].} =
mixin read
if rlp.blobLen != 0:
val = Opt.some(rlp.read(T))
else:
writer.append ""
rlp.skipElem

export
options, rlp

0 comments on commit 3b53f41

Please sign in to comment.