Global<T>
to store global variables ergonomically
#547
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Similar to
once_cell::Lazy
, this type allows lazy initialization instatic
variables, withoutOption
or other workarounds. The implementation was initially very simple (just 100 LoC), but got a bit more involved oncetry_lock()
was added. An older commit contains the simpler implementation. Depending on needs, we might also considerRwLock
instead ofMutex
internally, but I think it's not necessary right now, and it would add quite a bit of further complexity in both implementation and usage (e.g. for caches).I replaced two occurrences of
Mutex<Option<T>>
in gdext withGlobal<T>
and added a new one.Global<T>
is currently not part of the public API, but we may consider exposing it if there is demand.At some point, we should also consider what to do with the
static mut
in godot-ffi that gives access to the entireBINDING
. We need to see if concurrent access is needed. And if yes, how to provide it without compromising performance for every single Godot API call.