Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: valueOr and withValue utilities #1079

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions libp2p/utility.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,24 @@ template withValue*[T](self: Opt[T] | Option[T], value, body: untyped): untyped
macro withValue*[T](self: Opt[T] | Option[T], value, body, body2: untyped): untyped =
let elseBody = body2[0]
quote do:
if `self`.isSome:
let `value` {.inject.} = `self`.get()
let temp = `self`
lchenut marked this conversation as resolved.
Show resolved Hide resolved
if temp.isSome:
let `value` {.inject.} = temp.get()
`body`
else:
`elseBody`

template valueOr*[T](self: Option[T], body: untyped): untyped =
if self.isSome:
self.get()
let temp = (self)
if temp.isSome:
temp.get()
else:
body

template toOpt*[T, E](self: Result[T, E]): Opt[T] =
if self.isOk:
let temp = (self)
if temp.isOk:
when T is void: Result[void, void].ok()
else: Opt.some(self.unsafeGet())
else: Opt.some(temp.unsafeGet())
else:
Opt.none(type(T))
73 changes: 73 additions & 0 deletions tests/testutility.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# This file may not be copied, modified, or distributed except according to
# those terms.

import options
import ./helpers
import ../libp2p/utility

Expand Down Expand Up @@ -71,3 +72,75 @@ suite "Utility":
test "unsuccessful safeConvert from uint to int":
check not (compiles do:
result: uint = safeConvert[int, uint](11.uint))

test "withValue":
lchenut marked this conversation as resolved.
Show resolved Hide resolved
type
TestObj = ref object
x: int

proc testObjGen(self: TestObj): Opt[TestObj] =
if self.isNil():
return Opt.none(TestObj)
self.x.inc()
return Opt.some(self)

var obj = TestObj(x: 42)

Opt.some(obj).withValue(v):
discard
else:
check false
Opt.none(TestObj).withValue(v):
check false
else:
discard

Opt.some(obj).withValue(v):
v.x = 10
check obj.x == 10

testObjGen(obj).withValue(v):
check v.x == 11
v.x = 33
check obj.x == 33
testObjGen(obj).withValue(v):
check v.x == 34
v.x = 0
else:
check false
check obj.x == 0

testObjGen(nil).withValue(v):
check false
testObjGen(nil).withValue(v):
check false
else:
obj.x = 25
check obj.x == 25

test "valueOr":
type
testObj = ref object
x: int

proc testGen(self: testObj): Option[testObj] =
if self.isNil():
return none(testObj)
self.x.inc()
return some(self)

var x = none(int).valueOr:
33
check x == 33
x = some(12).valueOr:
check false
return
check x == 12

var o = testGen(nil).valueOr:
testObj(x: 4)
check o.x == 4
o = testGen(o).valueOr:
check false
return
check o.x == 5
Loading