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

std lib iterators unnecessarily require value copies #24720

Open
arnetheduck opened this issue Feb 25, 2025 · 0 comments · May be fixed by #24723
Open

std lib iterators unnecessarily require value copies #24720

arnetheduck opened this issue Feb 25, 2025 · 0 comments · May be fixed by #24723
Assignees

Comments

@arnetheduck
Copy link
Contributor

arnetheduck commented Feb 25, 2025

Description

When iterating over the pairs of a Table or (or indeed any other std lib type with a custom iterator), there's no fundamental reason why a copy of the keys/values should be needed and indeed, it is significantly expensive to require one for anything but non-trivial table entries.

Apart from being broadly inefficient, it also makes it hard to use move-only types in general because they can no longer be used with std containers.

import std/tables
type NoCopies = object

proc `=copy`(a: var NoCopies, b: NoCopies) {.error.}

var t: Table[int, NoCopies]
t[3] = NoCopies() # only moves
for k,v in t.pairs(): # lent values, no need to copy!
  echo "found entry"

Nim Version

2.2

Current Output

Error: '=copy' is not available for type <NoCopies>; requires a copy because it's not the last read of 't.data[h].val'; routine: main

Expected Output


Known Workarounds

No response

Additional Information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants