-
-
Notifications
You must be signed in to change notification settings - Fork 738
std.container.rbtree: Return range elements by ref #10755
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
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request, @CyberShadow! Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#10755" |
Looks like the compiler was incorrectly allowing import std.container.rbtree;
void main()
{
auto t = new RedBlackTree!int();
t.insert(2);
foreach (ref r; t[]) // "ref" compiles, but doesn't do anything
r++;
assert(t[].front == 3); // fails
} |
Unfortunately, it seems the fix dlang/dmd#16381 is stalled or dead. |
From what I see that seems to target the array index. Or does it also affect values of ranges with non-ref elements? |
It does. It targetted two issues. |
This is the only qualm I have about it. But you are right that returning by value may still allow mutation that would mess up the comparison. I'd say that maybe a comment about this should be in there, but apparently the Range struct is private, so it doesn't even get ddoc'd! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add ref
to the container's front
and back
as well, for consistency.
I wrote this note:
but I'm not sure if it should be repeated 4 times. |
Hm... maybe add it to the RedBlackTree ddoc comment? |
Done, how's this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I don't see any downsides, and it will allow updating value types in-place as we're iterating over the tree.
Some counter-arguments I can think of:
What if this is used to change the key such that the tree is no longer ordered?
Elem
is a reference type, it's not like we're returningconst
.What if the tree is changed while we're holding a reference to a node?
new
, so even if they're gone from the tree, it shouldn't result in a dangling pointer.Did I miss anything?
CC @schveiguy