From 260fb19d81284b2ed49998ebb0b45fa0c5886a89 Mon Sep 17 00:00:00 2001 From: Fabiana Cecin Date: Thu, 26 Mar 2026 23:41:05 -0300 Subject: [PATCH] std/options fixes - add Option[T] valueOr/withValue no longer provided by libp2p - add missing std/options imports no longer re-exported by libp2p --- config.nims | 3 +++ tests/test_wakunode.nim | 2 +- tests/waku_relay/test_wakunode_relay.nim | 2 +- tests/waku_store/test_wakunode_store.nim | 2 +- .../waku_store_legacy/test_wakunode_store.nim | 2 +- waku/common/option_shim.nim | 26 +++++++++++++++++++ 6 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 waku/common/option_shim.nim diff --git a/config.nims b/config.nims index f74fe183f..5e8b0188e 100644 --- a/config.nims +++ b/config.nims @@ -99,6 +99,9 @@ if not defined(macosx) and not defined(android): nimStackTraceOverride switch("import", "libbacktrace") +# Shim to provide valueOr and withValue for Option[T] +switch("import", "waku/common/option_shim") + --define: nimOldCaseObjects # https://github.com/status-im/nim-confutils/issues/9 diff --git a/tests/test_wakunode.nim b/tests/test_wakunode.nim index a7f1084fb..39452890d 100644 --- a/tests/test_wakunode.nim +++ b/tests/test_wakunode.nim @@ -1,7 +1,7 @@ {.used.} import - std/[sequtils, strutils, net], + std/[options, sequtils, strutils, net], stew/byteutils, testutils/unittests, chronicles, diff --git a/tests/waku_relay/test_wakunode_relay.nim b/tests/waku_relay/test_wakunode_relay.nim index a687119bd..ebfeef22c 100644 --- a/tests/waku_relay/test_wakunode_relay.nim +++ b/tests/waku_relay/test_wakunode_relay.nim @@ -1,7 +1,7 @@ {.used.} import - std/[os, strutils, sequtils, sysrand, math], + std/[options, os, strutils, sequtils, sysrand, math], stew/byteutils, testutils/unittests, chronos, diff --git a/tests/waku_store/test_wakunode_store.nim b/tests/waku_store/test_wakunode_store.nim index fa73cd16d..b7732aa65 100644 --- a/tests/waku_store/test_wakunode_store.nim +++ b/tests/waku_store/test_wakunode_store.nim @@ -1,7 +1,7 @@ {.used.} import - std/sequtils, + std/[options, sequtils], testutils/unittests, chronicles, chronos, diff --git a/tests/waku_store_legacy/test_wakunode_store.nim b/tests/waku_store_legacy/test_wakunode_store.nim index 58e3ca9e0..150f2f056 100644 --- a/tests/waku_store_legacy/test_wakunode_store.nim +++ b/tests/waku_store_legacy/test_wakunode_store.nim @@ -1,7 +1,7 @@ {.used.} import - std/net, + std/[options, net], testutils/unittests, chronos, libp2p/crypto/crypto, diff --git a/waku/common/option_shim.nim b/waku/common/option_shim.nim new file mode 100644 index 000000000..6827cd856 --- /dev/null +++ b/waku/common/option_shim.nim @@ -0,0 +1,26 @@ +# Shim to provide valueOr and withValue for Option[T] + +{.push raises: [].} + +import std/options + +template valueOr*[T](self: Option[T], def: untyped): T = + let s = self + if s.isSome(): + s.get() + else: + def + +template withValue*[T](self: Option[T], value, body: untyped) = + let s = self + if s.isSome(): + let value {.inject.} = s.get() + body + +template withValue*[T](self: Option[T], value, body, elseStmt: untyped) = + let s = self + if s.isSome(): + let value {.inject.} = s.get() + body + else: + elseStmt