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.
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
RFC: guide: document saved state conventions and howto #846
base: main
Are you sure you want to change the base?
RFC: guide: document saved state conventions and howto #846
Changes from 1 commit
7eb1e6f
8d9c413
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Feel free to choose a stopping point for this PR. But I would also want to provide some more concrete advice, such as:
When a field has been added and your code reads an old saved state that did not have that field, the field will get the default value, as defined below. You must define the field's type so that the default value makes sense when reading an old saved state.
The easiest way to do this is to add fields with a type
Option<T>
, so that you can distinguish between old saved states (None
) and new ones (Some(_)
). But for field types with well-defined default values, this just adds an extra layer of complexity, so consider defining things so that the default value means the same thing as the value being missing.For example, if you are saving whether the guest enabled some new feature, just use a
bool
for that, sincefalse
is a reasonable value for an old saved state where the feature didn't exist;Option<bool>
just adds a third state that the reader has to go look in your save/restore code to understand.However, for types where there are no default values (currently, enums and arrays), you must wrap the type in an
Option
to add it to an existing saved state structure.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.
For the bool case, that makes a lot of sense. On the flip-side, I'm not sure how I'd feed about seeing something like
queue_count: usize
(or whatever), where0
is the in-band representation of missing state.In general, the Go/Protobuf style of using defaults as in-band signals for missing data just rubs me the wrong way, as now the compiler isn't gonna force you to consider those cases explicitly.
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.
If
queue_count
would otherwise never be 0 in a newly generated saved state, then I agree. E.g., if the natural default for queue count were 1, then I wouldn't want us to treat 0 the same as 1--we should use anOption
to clarify that the value might be missing (ideally withNonZeroU32
or whatever, which I don't think we support today but we could).Maybe another way to phrase it is, if you have to have a conditional (explicitly or implicitly via
unwrap_or_else
or similar) in your restore path to handle the missing case, it should probably be on anOption
. If you wouldn't have needed the condition except that you chose to use anOption
... well, maybe that was the wrong choice.Most egregious to me is where the newly added field has a natural default of
true
, so thatNone
is equivalent toSome(true)
rather thanSome(false)
. The guidance should be to flip the parity of the bool in that case, rather than have this unintuitive mapping.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.
I'd like to hit pause on new content for this PR. I'll fix up the comment above (#846 (comment)). When I hit pause, I'll file an issue to track any feedback not yet captured.
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.
They are supported, you just need to wrap them in
Option
.