From cfc743ce94c296d225d229ceeccfe0a2804aa0ab Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Tue, 26 Sep 2023 17:16:13 -0700 Subject: [PATCH] result types --- datastore/threads/threadproxyds.nim | 5 +++-- datastore/threads/threadresult.nim | 12 +++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/datastore/threads/threadproxyds.nim b/datastore/threads/threadproxyds.nim index 6582c0e..d77ec49 100644 --- a/datastore/threads/threadproxyds.nim +++ b/datastore/threads/threadproxyds.nim @@ -202,7 +202,7 @@ method put*(self: ThreadDatastore, let data = DataBuffer.new data dispatchTask[void](self, signal): self.tp.spawn putTask(ctx, ds, key, data) - return ctx[].res + return ctx[].res.toRes() method put*( self: ThreadDatastore, @@ -231,7 +231,8 @@ method get*(self: ThreadDatastore, let key = KeyId.new key.id() dispatchTask[DataBuffer](self, signal): self.tp.spawn getTask(ctx, ds, key) - # return ctx[].res + return ctx[].res.toRes() do(v: DataBuffer) -> seq[byte]: + v.toSeq() method close*(self: ThreadDatastore): Future[?!void] {.async.} = await self.semaphore.closeAll() diff --git a/datastore/threads/threadresult.nim b/datastore/threads/threadresult.nim index f0a226b..7754f12 100644 --- a/datastore/threads/threadresult.nim +++ b/datastore/threads/threadresult.nim @@ -40,6 +40,12 @@ converter toExc*(e: ThreadResErr): ref CatchableError = of ErrorEnum.CatchableErr: (ref CatchableError)(msg: $e[1]) of ErrorEnum.DefectErr: (ref CatchableError)(msg: "defect: " & $e[1]) -converter toExcRes*[T](res: ThreadResult[T]): ?!T = - res.mapErr() do(exc: ThreadResErr) -> ref CatchableError: - exc.toExc() +proc toRes*(res: ThreadResult[void]): ?!void = + res.mapErr() do(e: ThreadResErr) -> ref CatchableError: + e.toExc() + +proc toRes*[T,S](res: ThreadResult[T], m: proc(v: T): S = proc(v: T): T = v): ?!S = + if res.isErr(): + result.err res.error().toExc() + else: + result.ok m(res.get())