Skip to content

Commit 39a9fcc

Browse files
authored
added []= value assignment for smart pointers (#52)
1 parent 1bde449 commit 39a9fcc

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/fusion/smartptrs.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ proc `[]`*[T](p: UniquePtr[T]): var T {.inline.} =
5151
assert(p.val != nil, "deferencing nil unique pointer")
5252
p.val[]
5353
54+
proc `[]=`*[T](p:UniquePtr[T], v:T) {.inline.} = (p[]) = v
55+
5456
proc `$`*[T](p: UniquePtr[T]): string {.inline.} =
5557
if p.val == nil: "UniquePtr[" & $T & "](nil)"
5658
else: "UniquePtr[" & $T & "](" & $p.val[] & ")"
@@ -110,6 +112,8 @@ proc `[]`*[T](p: SharedPtr[T]): var T {.inline.} =
110112
doAssert(p.val != nil, "deferencing nil shared pointer")
111113
p.val.value
112114
115+
proc `[]=`*[T](p:SharedPtr[T], v:T) {.inline.} = (p[]) = v
116+
113117
proc `$`*[T](p: SharedPtr[T]): string {.inline.} =
114118
if p.val == nil: "SharedPtr[" & $T & "](nil)"
115119
else: "SharedPtr[" & $T & "](" & $p.val.value & ")"
@@ -135,6 +139,8 @@ proc `[]`*[T](p: ConstPtr[T]): lent T {.inline.} =
135139
doAssert(SharedPtr[T](p).val != nil, "deferencing nil const pointer")
136140
SharedPtr[T](p).val.value
137141
142+
template `[]=`*[T](p:ConstPtr[T], v:T) = {.error: "'" & p.astToStr & "[]' cannot be assigned to (ConstPtr)".}
143+
138144
proc `$`*[T](p: ConstPtr[T]): string {.inline.} =
139145
if SharedPtr[T](p).val == nil: "ConstPtr[" & $T & "](nil)"
140146
else: "ConstPtr[" & $T & "](" & $SharedPtr[T](p).val.value & ")"

0 commit comments

Comments
 (0)