Add sink and lent annotations to the critbits module (#20021)

* Add sink and lent to critbits

* Remove lent for pairs
I guess lent doesn't work well inside tuples

* Remove lent from template in critbits
Apparently this also doesn't work, because some checks failed
This commit is contained in:
silent-observer 2022-07-14 13:03:16 +03:00 • committed by GitHub
commit 93211a2bdd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -197,7 +197,7 @@ proc missingOrExcl*[T](c: var CritBitTree[T], key: string): bool =
discard exclImpl(c, key) discard exclImpl(c, key)
result = c.count == oldCount result = c.count == oldCount
proc containsOrIncl*[T](c: var CritBitTree[T], key: string, val: T): bool = proc containsOrIncl*[T](c: var CritBitTree[T], key: string, val: sink T): bool =
## Returns true if `c` contains the given `key`. If the key does not exist, ## Returns true if `c` contains the given `key`. If the key does not exist,
## `c[key] = val` is performed. ## `c[key] = val` is performed.
## ##
@ -270,7 +270,7 @@ proc incl*(c: var CritBitTree[void], key: string) =
discard rawInsert(c, key) discard rawInsert(c, key)
proc incl*[T](c: var CritBitTree[T], key: string, val: T) = proc incl*[T](c: var CritBitTree[T], key: string, val: sink T) =
## Inserts `key` with value `val` into `c`. ## Inserts `key` with value `val` into `c`.
## ##
## **See also:** ## **See also:**
@ -284,7 +284,7 @@ proc incl*[T](c: var CritBitTree[T], key: string, val: T) =
var n = rawInsert(c, key) var n = rawInsert(c, key)
n.val = val n.val = val
proc `[]=`*[T](c: var CritBitTree[T], key: string, val: T) = proc `[]=`*[T](c: var CritBitTree[T], key: string, val: sink T) =
## Alias for `incl <#incl,CritBitTree[T],string,T>`_. ## Alias for `incl <#incl,CritBitTree[T],string,T>`_.
## ##
## **See also:** ## **See also:**
@ -300,7 +300,7 @@ template get[T](c: CritBitTree[T], key: string): T =
n.val n.val
func `[]`*[T](c: CritBitTree[T], key: string): T {.inline.} = func `[]`*[T](c: CritBitTree[T], key: string): lent T {.inline.} =
## Retrieves the value at `c[key]`. If `key` is not in `t`, the ## Retrieves the value at `c[key]`. If `key` is not in `t`, the
## `KeyError` exception is raised. One can check with `hasKey` whether ## `KeyError` exception is raised. One can check with `hasKey` whether
## the key exists. ## the key exists.
@ -342,7 +342,7 @@ iterator keys*[T](c: CritBitTree[T]): string =
for x in leaves(c.root): yield x.key for x in leaves(c.root): yield x.key
iterator values*[T](c: CritBitTree[T]): T = iterator values*[T](c: CritBitTree[T]): lent T =
## Yields all values of `c` in the lexicographical order of the ## Yields all values of `c` in the lexicographical order of the
## corresponding keys. ## corresponding keys.
## ##
@ -415,7 +415,7 @@ iterator keysWithPrefix*[T](c: CritBitTree[T], prefix: string): string =
let top = allprefixedAux(c, prefix) let top = allprefixedAux(c, prefix)
for x in leaves(top): yield x.key for x in leaves(top): yield x.key
iterator valuesWithPrefix*[T](c: CritBitTree[T], prefix: string): T = iterator valuesWithPrefix*[T](c: CritBitTree[T], prefix: string): lent T =
## Yields all values of `c` starting with `prefix` of the ## Yields all values of `c` starting with `prefix` of the
## corresponding keys. ## corresponding keys.
## ##
@ -518,7 +518,7 @@ func commonPrefixLen*[T](c: CritBitTree[T]): int {.inline, since((1, 3)).} =
else: c.root.byte else: c.root.byte
else: 0 else: 0
proc toCritBitTree*[T](pairs: openArray[(string, T)]): CritBitTree[T] {.since: (1, 3).} = proc toCritBitTree*[T](pairs: sink openArray[(string, T)]): CritBitTree[T] {.since: (1, 3).} =
## Creates a new `CritBitTree` that contains the given `pairs`. ## Creates a new `CritBitTree` that contains the given `pairs`.
runnableExamples: runnableExamples:
doAssert {"a": "0", "b": "1", "c": "2"}.toCritBitTree is CritBitTree[string] doAssert {"a": "0", "b": "1", "c": "2"}.toCritBitTree is CritBitTree[string]
@ -526,7 +526,7 @@ proc toCritBitTree*[T](pairs: openArray[(string, T)]): CritBitTree[T] {.since: (
for item in pairs: result.incl item[0], item[1] for item in pairs: result.incl item[0], item[1]
proc toCritBitTree*(items: openArray[string]): CritBitTree[void] {.since: (1, 3).} = proc toCritBitTree*(items: sink openArray[string]): CritBitTree[void] {.since: (1, 3).} =
## Creates a new `CritBitTree` that contains the given `items`. ## Creates a new `CritBitTree` that contains the given `items`.
runnableExamples: runnableExamples:
doAssert ["a", "b", "c"].toCritBitTree is CritBitTree[void] doAssert ["a", "b", "c"].toCritBitTree is CritBitTree[void]