Skip to content

Commit

Permalink
add isUniqueRef
Browse files Browse the repository at this point in the history
  • Loading branch information
elcritch committed Dec 24, 2024
1 parent c69e13a commit b233c51
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/tsmartptrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ block:
doAssert not a3.isNil
doAssert a3[] == 0

doAssert a1.isUniqueRef() == true
a1 = newSharedPtr(int)
a1[] = 1
doAssert a1[] == 1
var a4 = newSharedPtr(string)
a4[] = "hello world"
doAssert a4[] == "hello world"
doAssert a4.isUniqueRef() == true
var a4p = a4
doAssert a4.isUniqueRef() == false

block:
var a1: ConstPtr[float]
Expand Down
6 changes: 6 additions & 0 deletions threading/smartptrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ proc `[]=`*[T](p: SharedPtr[T], val: sink Isolated[T]) {.inline.} =
template `[]=`*[T](p: SharedPtr[T]; val: T) =
`[]=`(p, isolate(val))

proc isUniqueRef*[T](p: SharedPtr[T]): bool =
if p.val == nil:
return true
p.val.counter.load(moAcquireRelease) == 0


proc `$`*[T](p: SharedPtr[T]): string {.inline.} =
if p.val == nil: "nil"
else: "(val: " & $p.val.value & ")"
Expand Down

0 comments on commit b233c51

Please sign in to comment.