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

results: collections integration #179

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 46 additions & 0 deletions stew/results.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1068,3 +1068,49 @@ template `?`*[T, E](self: Result[T, E]): auto =

when not(T is void):
v.vResultPrivate

# Collection integration

iterator values*[T, E](self: Result[T, E]): T =
## Iterate over a Result as a 0/1-item collection, returning its value if set
if self.oResultPrivate:
yield self.vResultPrivate

iterator errors*[T, E](self: Result[T, E]): E =
## Iterate over a Result as a 0/1-item collection, returning its error if set
if not self.oResultPrivate:
yield self.eResultPrivate

iterator items*[T](self: Opt[T]): T =
## Iterate over an Opt as a 0/1-item collection, returning its value if set
if self.oResultPrivate:
yield self.vResultPrivate

iterator mvalues*[T, E](self: var Result[T, E]): var T =
if self.oResultPrivate:
yield self.vResultPrivate

iterator merrors*[T, E](self: var Result[T, E]): var E =
if not self.oResultPrivate:
yield self.eResultPrivate

iterator mitems*[T](self: var Opt[T]): var T =
if self.oResultPrivate:
yield self.vResultPrivate

func containsValue*(self: Result, v: auto): bool =
## Return true iff the given result is set to a value that equals `v`
self.oResultPrivate and self.vResultPrivate == v

func containsError*(self: Result, e: auto): bool =
## Return true iff the given result is set to an error that equals `e`
not self.oResultPrivate and self.eResultPrivate == e

func contains*(self: Opt, v: auto): bool =
## Return true iff the given `Opt` is set to a value that equals `v` - can
## also be used in the "infix" `in` form:
##
## ```nim
## assert "value" in Opt.some("value")
## ```
self.oResultPrivate and self.vResultPrivate == v
42 changes: 42 additions & 0 deletions tests/test_results.nim
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,37 @@ block:
doAssert rOk.filter(proc(x: int): auto = Result[void, string].err("filter")).error == "filter"
doAssert rErr.filter(proc(x: int): auto = Result[void, string].err("filter")) == rErr

# Collections
block:
var i = 0
for v in rOk.values:
doAssert v == rOk.value()
i += 1
doAssert i == 1

for v in rOk.errors:
raiseAssert "not an error"

doAssert rOk.containsValue(rOk.value())
doAssert not rOk.containsValue(rOk.value() + 1)

doAssert not rOk.containsError("test")

block:
var i = 0
for v in rErr.values:
raiseAssert "not a value"

for v in rErr.errors:
doAssert v == rErr.error()
i += 1
doAssert i == 1

doAssert rErr.containsError(rErr.error())
doAssert not rErr.containsError(rErr.error() & "X")

doAssert not rErr.containsValue(42)

# Exception conversions - toException must not be inside a block
type
AnEnum = enum
Expand Down Expand Up @@ -381,6 +412,17 @@ block: # Result[T, void] aka `Opt`
doAssert oOk.orErr("error").value() == oOk.get()
doAssert oErr.orErr("error").error() == "error"

# Collections
block:
var i = 0
for v in oOk:
doAssert v == oOk.value()
i += 1
doAssert i == 1

doAssert oOk.value() in oOk
doAssert oOk.value() + 1 notin oOk

block: # `cstring` dangling reference protection
type CSRes = Result[void, cstring]

Expand Down