-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
[Core] Array
and Dictionary
set
improvements
#89653
base: master
Are you sure you want to change the base?
[Core] Array
and Dictionary
set
improvements
#89653
Conversation
@@ -412,9 +412,20 @@ void Array::set(int p_idx, const Variant &p_value) { | |||
Variant value = p_value; | |||
ERR_FAIL_COND(!_p->typed.validate(value, "set")); | |||
|
|||
ERR_FAIL_INDEX(p_idx, size()); |
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.
This check is unnecessary because it is checked in cowdata
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.
It is not, it crashes in Vector
, because of the use of write
:
Lines 51 to 52 in fe01776
_FORCE_INLINE_ T &operator[](typename CowData<T>::Size p_index) { | |
CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size()); |
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.
So going outside the array should crash. Especially if you added a set_safe()
method.
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.
No, see the description for the reason and explanation :) It shouldn't like Vector
, why have a separate set
otherwise? It is the safe option over operator[]
The set_safe
method might be better renamed, but the "safe" part isn't about it being safe, but that it can be used to check the details, originally named it set_check
and might restore it to that to avoid confusion
Edit: Further, calling the setter on Variant
doesn't crash in either case, so it isn't good to have it crash here either
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.
Ah, then I think this can be added for get
as well.
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.
Edit: My bad it does take that path, but the argument stands still, it returns a value so it can be expected to be valid, and this change is to match Vector
, this also matches all other containers in the engine, like HashMap
and VMap
It returns a const
reference, which can't be safely done with a check
e410f4e
to
23ccab4
Compare
23ccab4
to
cafa23f
Compare
cafa23f
to
ca398b3
Compare
ca398b3
to
d53f8e4
Compare
c40d550
to
e426347
Compare
d3fb692
to
12b3f56
Compare
This returns an error, making it possible to check the behavior Also adds range checking for `Array.set` to match `Vector` and allow it to be use safely
12b3f56
to
a9a1694
Compare
Dictionary.get_or_add
check for read-onlyDictionary.set
for read-only enforcing assignmentsset_safe
method toArray
andDictionary
Array.set
for safe usage and to matchVector
A number of different changes, largely related, can split them up if desired (the range check for
Array.set
could be cherry-picked if desired), and theset_safe
isn't necessarily needed but added it just to see about the demand for itWill look at adding some unit tests for read-only arrays to complement the dictionary ones