From 6d3e6a21caf4110d0d432b82f14e41e0271cd76b Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Tue, 26 Jan 2021 15:06:17 +0100 Subject: [PATCH] results.`==` with void, Reboot #73 addressing comments (#74) --- stew/results.nim | 8 +++++++- tests/test_results.nim | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/stew/results.nim b/stew/results.nim index 470e213..71097fe 100644 --- a/stew/results.nim +++ b/stew/results.nim @@ -442,7 +442,7 @@ template capture*[E: Exception](T: type, someExceptionExpr: ref E): Result[T, re ret = R.err(caught) ret -func `==`*[T0, E0, T1, E1](lhs: Result[T0, E0], rhs: Result[T1, E1]): bool {.inline.} = +func `==`*[T0: not void, E0, T1: not void, E1](lhs: Result[T0, E0], rhs: Result[T1, E1]): bool {.inline.} = if lhs.o != rhs.o: false elif lhs.o: # and rhs.o implied @@ -450,6 +450,12 @@ func `==`*[T0, E0, T1, E1](lhs: Result[T0, E0], rhs: Result[T1, E1]): bool {.inl else: lhs.e == rhs.e +func `==`*[E0, E1](lhs: Result[void, E0], rhs: Result[void, E1]): bool {.inline.} = + if lhs.o != rhs.o: + false + else: + lhs.e == rhs.e + func get*[T: not void, E](self: Result[T, E]): T {.inline.} = ## Fetch value of result if set, or raise Defect ## Exception bridge mode: raise given Exception instead diff --git a/tests/test_results.nim b/tests/test_results.nim index b165c03..5da1d86 100644 --- a/tests/test_results.nim +++ b/tests/test_results.nim @@ -275,3 +275,8 @@ func cstringF(s: string): CSRes = doAssert false discard cstringF("test") + +# Compare void +block: + var a, b: Result[void, bool] + discard a == b