Skip to content

Commit

Permalink
add reap_value() method to DictEntry, and use it to ensure the dest…
Browse files Browse the repository at this point in the history
…ruction of entry_value in `Dict.pop()`

Signed-off-by: Max Brylski <[email protected]>
  • Loading branch information
helehex committed Jun 18, 2024
1 parent 87266e7 commit e586986
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion stdlib/src/collections/dict.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ struct DictEntry[K: KeyElement, V: CollectionElement](CollectionElement):
self.key = key^
self.value = value^

fn reap_value(owned self) -> V:
"""Take the value from an owned entry.
Returns:
The value of the entry.
"""
__mlir_op.`lit.ownership.mark_destroyed`(__get_mvalue_as_litref(self))
return self.value^


alias _EMPTY = -1
alias _REMOVED = -2
Expand Down Expand Up @@ -772,7 +781,7 @@ struct Dict[K: KeyElement, V: CollectionElement](
var entry_value = entry[].unsafe_take()
entry[] = None
self.size -= 1
return entry_value.value^
return entry_value^.reap_value()
elif default:
return default.value()
raise "KeyError"
Expand Down

0 comments on commit e586986

Please sign in to comment.