-
Notifications
You must be signed in to change notification settings - Fork 122
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
feat: Add in place update support to versioned macro #1755
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Docker tags |
Benchmark for 67bdf40Click to view benchmark
|
dhedey
commented
Mar 30, 2024
iamyulong
requested changes
Apr 5, 2024
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.
As discussed, there is concern about the update-in-progress enum variant. We will need to address this before merging.
Looks good. One idea to explore is to make the attributes more structural, such as #[sbor(as_type("type", as_ref="", from_value="", transparent_name))] |
iamyulong
approved these changes
Apr 24, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Pulls in (and productionizes) versioned macro changes from #1744 ... and adds a lot of other stuff.
Key changes:
define_versioned!
calls need updating.kind: StaticMultiVersioned
as_type
and ignored enum variants, and tweaks to whencategorize_types
is needed to be more intuitive.PermitSborAttributes
derive which simply allows#[sbor]
attributes to be used even if the type doesn't deriveSbor
. This means a helper macro can add sbor-specific attributes, but leave it up to the user to decide if they actually need Sbor on the type or not.eager_replace!
procedural macro which is a pre-processor style macro intended for use in declarative macros. It is effectively a more powerful version thanpaste!
which is also inspired byquote!
/parse_quote!
and gets around this issue: Generate doc-comments based on provided tokens dtolnay/paste#40 (comment) - and generally solves a lot of frustrations I've had building out the more gnarly declarative macros in the code base - to see it in practice, seesbor-derive/lib.rs
andsbor/versioned.rs
.Details
Versioned
Versioned_
types have been renamed to be much clearer._Versions
enum. See yak shaving below.fully_update_to_latest_version_mut
method to Versioned enums, which takes a Versioned enum, updates it in place if necessary, and returns a&mut
to the resultant latest content. This makes for much more streamlined updates of nested state, which was why I added it when doing prototyping in [ON HOLD]declare_native_blueprint_state
extensions #1744.as_unique_version_mut
to ease use of single versioned itemYak-shaving background
&mut
reference, we need some temporary state to store into the enum whilst we take out the current value and try to upgrade it (which requires taking ownership of the value, because the upgrade mechanism uses the more flexible From mechanism which needs an owned object).Option
so that we could temporarily replace it withNone
during the upgrade process.#[derive(Sbor)]
annotations to get a compile error (about an unrecognizedsbor
attribute), so I also added aPermitSborAttributes
derive macro which doesn't actually derive anything, but does enable#[sbor(..)]
annotations to be present without giving a confusing compile error, to support versioned types which don't use SBOR derivesTesting
Added tests for the SBOR changes to
sbor-tests
.The versioned changes are tested in the rust doc tests, and by virtue of being used by other tests.
Update Recommendations
For Internal Integrators
Anyone consuming a versioned type may need to change from using a
match
to using one of the many new methods, e.g.:fully_update_into_latest_version
(self -> Self::Latest
)fully_update_to_latest_version_mut
(&mut self -> &mut Self::Latest
)as_unique_version_ref
(&self -> &Self::Latest
) - only on single versioned enumsIf you need to match the versions, you can call
.as_versions_ref()
/.as_versions_mut()
/.into_versions()
and then match on that.Anyone creating a versioned type will need to update the declaration to e.g. something like the following. Notable changes are 1. removal of the
enum
type, and addition of the Versions type.