Commit 50f90d5
Merge #748
748: Fix `Dictionary::get` unsoundness and add a few convenience functions r=Bromeon a=Waridley
For some strange reason `godot_dictionary_get` calls `Dictionary::operator[]` instead of `Dictionary::get`, which ends up inserting `Nil` if the key doesn't already exist in the dictionary.
https://github.com/godotengine/godot/blame/3.3/modules/gdnative/gdnative/dictionary.cpp#L126
This means it is unsound to call `godot_dictionary_get` on a `Dictionary<Shared>`. This PR re-implements godot-rust's `Dictionary::get` in terms of `godot_dictionary_get_with_default` (the behavior originally intended and likely expected by most Rust users), and adds `Dictionary::get_or_insert_nil` to provide the original behavior if it is indeed desired, but only on `Dictionary<T: LocalThreadAccess>` and an `unsafe` counterpart for `Dictionary<Shared>`, like the other mutating functions on `Dictionary`.
I also updated the documentation of `get_ref` and `get_mut_ref` since they call `operator[]`. Perhaps they should also be moved to non-shared access, but I figured that's less important since they're unsafe anyway?
I also added `try_get` and `get_non_nil` while I was at it, to simplify checking if a key exists and checking that its corresponding value is not `Nil` respectively.
Co-authored-by: Waridley <Waridley64@gmail.com>File tree
5 files changed
+62
-23
lines changed- gdnative-core/src/core_types
- gdnative-derive/src/variant
- test/src
5 files changed
+62
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
77 | 94 | | |
78 | 95 | | |
79 | | - | |
| 96 | + | |
80 | 97 | | |
81 | 98 | | |
| 99 | + | |
82 | 100 | | |
83 | 101 | | |
84 | 102 | | |
85 | 103 | | |
86 | | - | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
87 | 115 | | |
88 | 116 | | |
89 | 117 | | |
| |||
106 | 134 | | |
107 | 135 | | |
108 | 136 | | |
109 | | - | |
| 137 | + | |
| 138 | + | |
110 | 139 | | |
111 | 140 | | |
112 | 141 | | |
113 | 142 | | |
114 | | - | |
| 143 | + | |
| 144 | + | |
115 | 145 | | |
116 | 146 | | |
117 | 147 | | |
| |||
125 | 155 | | |
126 | 156 | | |
127 | 157 | | |
128 | | - | |
| 158 | + | |
| 159 | + | |
129 | 160 | | |
130 | 161 | | |
131 | 162 | | |
132 | 163 | | |
133 | | - | |
134 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
135 | 168 | | |
136 | 169 | | |
137 | 170 | | |
| |||
425 | 458 | | |
426 | 459 | | |
427 | 460 | | |
428 | | - | |
| 461 | + | |
429 | 462 | | |
430 | 463 | | |
431 | 464 | | |
| |||
591 | 624 | | |
592 | 625 | | |
593 | 626 | | |
594 | | - | |
| 627 | + | |
595 | 628 | | |
596 | 629 | | |
597 | 630 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1753 | 1753 | | |
1754 | 1754 | | |
1755 | 1755 | | |
1756 | | - | |
| 1756 | + | |
1757 | 1757 | | |
1758 | 1758 | | |
1759 | 1759 | | |
| |||
1762 | 1762 | | |
1763 | 1763 | | |
1764 | 1764 | | |
1765 | | - | |
| 1765 | + | |
1766 | 1766 | | |
1767 | 1767 | | |
1768 | 1768 | | |
| |||
1912 | 1912 | | |
1913 | 1913 | | |
1914 | 1914 | | |
1915 | | - | |
| 1915 | + | |
1916 | 1916 | | |
1917 | 1917 | | |
1918 | 1918 | | |
1919 | | - | |
| 1919 | + | |
1920 | 1920 | | |
1921 | 1921 | | |
1922 | 1922 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
| 90 | + | |
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
280 | 280 | | |
281 | 281 | | |
282 | 282 | | |
283 | | - | |
| 283 | + | |
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
87 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
88 | 91 | | |
89 | 92 | | |
90 | | - | |
| 93 | + | |
91 | 94 | | |
92 | 95 | | |
93 | 96 | | |
94 | 97 | | |
95 | 98 | | |
96 | | - | |
| 99 | + | |
97 | 100 | | |
98 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
99 | 105 | | |
100 | 106 | | |
101 | 107 | | |
| |||
146 | 152 | | |
147 | 153 | | |
148 | 154 | | |
149 | | - | |
| 155 | + | |
150 | 156 | | |
151 | 157 | | |
152 | 158 | | |
| |||
0 commit comments