Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added []= value assignment for smart pointers #52

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/fusion/smartptrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ proc `[]`*[T](p: UniquePtr[T]): var T {.inline.} =
assert(p.val != nil, "deferencing nil unique pointer")
p.val[]

proc `[]=`*[T](p:UniquePtr[T], v:T) {.inline.} = (p[]) = v
Copy link
Contributor

@planetis-m planetis-m Dec 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please assign to p.val directly, seems wasteful to go through a function call

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh its inline, so maybe no need to.

Copy link
Contributor Author

@JorySchossau JorySchossau Dec 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing! Yeah, I made it inline in lieu of p.val since that was the existing design for the other procs. I just followed suit.


proc `$`*[T](p: UniquePtr[T]): string {.inline.} =
if p.val == nil: "UniquePtr[" & $T & "](nil)"
else: "UniquePtr[" & $T & "](" & $p.val[] & ")"
Expand Down Expand Up @@ -110,6 +112,8 @@ proc `[]`*[T](p: SharedPtr[T]): var T {.inline.} =
doAssert(p.val != nil, "deferencing nil shared pointer")
p.val.value

proc `[]=`*[T](p:SharedPtr[T], v:T) {.inline.} = (p[]) = v

proc `$`*[T](p: SharedPtr[T]): string {.inline.} =
if p.val == nil: "SharedPtr[" & $T & "](nil)"
else: "SharedPtr[" & $T & "](" & $p.val.value & ")"
Expand All @@ -135,6 +139,8 @@ proc `[]`*[T](p: ConstPtr[T]): lent T {.inline.} =
doAssert(SharedPtr[T](p).val != nil, "deferencing nil const pointer")
SharedPtr[T](p).val.value

template `[]=`*[T](p:ConstPtr[T], v:T) = {.error: "'" & p.astToStr & "[]' cannot be assigned to (ConstPtr)".}

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