result types

This commit is contained in:
Jaremy Creechley 2023-09-26 17:16:13 -07:00
parent 36e6679a79
commit cfc743ce94
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
2 changed files with 12 additions and 5 deletions

View File

@ -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()

View File

@ -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())