GuiAction with a well-defined initial state #1
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.
I was watching your video and I noticed you want to avoid uninitialised state in GuiAction. You had to use
MaybeUninit
in Rust for Rust reasons, but that introduces an UB in your code.I think the trick you wanted is an empty struct as the default enum state in GuiAction. It is as simple as
struct {} none;
, but I added as an explicit struct because it is easier to remember and explicitly informs your intent (it is also nicer if you use an automatic code formatting).The empty struct should be fine in both sides because empty structs are actually modelled as a single byte in memory. It is an old C standard trick to make that memory byte's address as an identity for an empty struct.
Since it is on top of the enum, it will be the default state - therefore making any value in memory as a valid and well-defined union state.
Disclaimer: I don't know Zig or Rust but I did my best to follow the how the GuiAction is used in both sides.
Zig seems fine because it doesn't use the value of the union when the action is "none". So I left it as-is.
Rust might not need the
MaybeUninit
trick because the empty struct should make it a valid enum. So I think it is enough to initialise the tag inmake_action
.Unfortunately, my Windows machine has VIM and Clang, but no Rust - so I could not test this change. But I checked if
gui.h
compiles viaclang -x c
. :)