Stop mixing Result and Exceptions in some of the KeyedQueue APIs

This commit is contained in:
Zahary Karadjov 2022-01-23 21:18:03 +02:00
parent b464505b4d
commit bb705bf17b
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609

View File

@ -370,19 +370,22 @@ proc popValue*[K,V](rq: var KeyedQueue[K,V]):
proc delete*[K,V](rq: var KeyedQueue[K,V]; key: K): proc delete*[K,V](rq: var KeyedQueue[K,V]; key: K):
Result[KeyedQueuePair[K,V],void] {.gcsafe,raises: [Defect,KeyError].} = Result[KeyedQueuePair[K,V], void] =
## Delete the item with key `key` from the queue and returns the key-value ## Delete the item with key `key` from the queue and returns the key-value
## item pair just deleted (if any). ## item pair just deleted (if any).
if rq.tab.hasKey(key): if rq.tab.hasKey(key):
let kvp = KeyedQueuePair[K,V]( try:
key: key, let kvp = KeyedQueuePair[K,V](
data: rq.tab[key].data) key: key,
rq.deleteImpl(key) data: rq.tab[key].data)
return ok(kvp) rq.deleteImpl(key)
return ok(kvp)
except KeyError:
raiseAssert "We've checked that the key is present above"
err() err()
proc del*[K,V](rq: var KeyedQueue[K,V]; key: K) proc del*[K,V](rq: var KeyedQueue[K,V]; key: K)
{.gcsafe,raises: [Defect,KeyError].} = {.gcsafe,raises: [Defect, KeyError].} =
## Similar to `delete()` but without return code. ## Similar to `delete()` but without return code.
if rq.tab.hasKey(key): if rq.tab.hasKey(key):
rq.deleteImpl(key) rq.deleteImpl(key)
@ -460,20 +463,23 @@ proc `[]`*[K,V](rq: var KeyedQueue[K,V]; key: K): V
# Public functions, LRU mode # Public functions, LRU mode
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
proc lruFetch*[K,V](rq: var KeyedQueue[K,V]; key: K): Result[V,void] proc lruFetch*[K,V](rq: var KeyedQueue[K,V]; key: K): Result[V,void] =
{.gcsafe, raises: [Defect,CatchableError].} =
## Fetch in *last-recently-used* mode: If the argument `key` exists in the ## Fetch in *last-recently-used* mode: If the argument `key` exists in the
## queue, move the key-value item pair to the *right end* (see `append()`) ## queue, move the key-value item pair to the *right end* (see `append()`)
## of the queue and return the value associated with the key. ## of the queue and return the value associated with the key.
let rc = rq.delete(key) let rc = rq.delete(key)
if rc.isErr: if rc.isErr:
return err() return err()
# Unlink and re-append item # Unlink and re-append item
rq.appendImpl(key, rc.value.data) try:
rq.appendImpl(key, rc.value.data)
except KeyError:
raiseAssert "Not possible"
ok(rc.value.data) ok(rc.value.data)
proc lruAppend*[K,V](rq: var KeyedQueue[K,V]; key: K; val: V; maxItems: int): V proc lruAppend*[K,V](rq: var KeyedQueue[K,V]; key: K; val: V; maxItems: int): V =
{.gcsafe, raises: [Defect,CatchableError].} =
## Append in *last-recently-used* mode: If the queue has at least `maxItems` ## Append in *last-recently-used* mode: If the queue has at least `maxItems`
## item entries, do `shift()` out the *left-most* one. Then `append()` the ## item entries, do `shift()` out the *left-most* one. Then `append()` the
## key-value argument pair `(key,val)` to the *right end*. Together with ## key-value argument pair `(key,val)` to the *right end*. Together with
@ -496,11 +502,14 @@ proc lruAppend*[K,V](rq: var KeyedQueue[K,V]; key: K; val: V; maxItems: int): V
## err() ## err()
## ##
# Limit number of cached items # Limit number of cached items
if maxItems <= rq.tab.len: try:
rq.shiftImpl if maxItems <= rq.tab.len:
# Append new value rq.shiftImpl
rq.appendImpl(key, val) # Append new value
val rq.appendImpl(key, val)
return val
except KeyError:
raiseAssert "Not possible"
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Public traversal functions, fetch keys # Public traversal functions, fetch keys