Skip to content

[sui-framework] Macros for dynamic fields and dynamic object fields#26166

Open
tnowacki wants to merge 7 commits intoMystenLabs:mainfrom
tnowacki:df-macros
Open

[sui-framework] Macros for dynamic fields and dynamic object fields#26166
tnowacki wants to merge 7 commits intoMystenLabs:mainfrom
tnowacki:df-macros

Conversation

@tnowacki
Copy link
Copy Markdown
Contributor

@tnowacki tnowacki commented Apr 8, 2026

Description

  • Adds new dynamic field and dynamic object field macros

Test plan

  • Tests

Release notes

Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required.

For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates.

  • Protocol:

  • Adds sui::dynamic_field::borrow_or_add

  • Adds sui::dynamic_field::borrow_mut_or_add

  • Adds sui::dynamic_field::get_do

  • Adds sui::dynamic_field::get_mut_do

  • Adds sui::dynamic_field::get_fold

  • Adds sui::dynamic_field::get_mut_fold

  • Adds sui::dynamic_object_field::borrow_or_add

  • Adds sui::dynamic_object_field::borrow_mut_or_add

  • Adds sui::dynamic_object_field::get_do

  • Adds sui::dynamic_object_field::get_mut_do

  • Adds sui::dynamic_object_field::get_fold

  • Adds sui::dynamic_object_field::get_mut_fold

  • Nodes (Validators and Full nodes):

  • gRPC:

  • JSON-RPC:

  • GraphQL:

  • CLI:

  • Rust SDK:

  • Indexing Framework:

@tnowacki tnowacki requested a review from a team April 8, 2026 23:51
@tnowacki tnowacki temporarily deployed to sui-typescript-aws-kms-test-env April 8, 2026 23:51 — with GitHub Actions Inactive
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 8, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
sui-docs Ready Ready Preview, Comment Apr 9, 2026 10:38pm
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
multisig-toolkit Ignored Ignored Preview Apr 9, 2026 10:38pm
sui-kiosk Ignored Ignored Preview Apr 9, 2026 10:38pm

Request Review

) {
let o = $object;
let name = $name;
if (exists_with_type<$Name, $Value>(o, name)) { $f(borrow(o, name)); }
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it not an error to call this when the field does not exist? at least in debug mode etc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is? Both exists_* calls are safe to call, that's their goal.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i meant if element does not exist, should we do nothing or emit an error


/// If the field exists, call `$f` on an immutable reference to the value.
/// This is like getting an `Option<&Value>` then calling `std::option::do`.
public macro fun get_do<$Name: copy + drop + store, $Value: key + store, $R: drop>(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure we've discussed this, but I can't recall why is it not get_ref_do?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it that get_ always returns a Ref? And then the value would be remove_*?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct yeah, it is basically get_do_ref but since it is always a ref we are eliding it.
Similarly it will be remove_opt_do, but since it is always opt we are skipping it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errr actually no. It is option::do not do_ref. Because it is Option<T> and |T| -> R, the issue is that today you can't have T = &_

fun borrow_or_add_existing() {
let sender = @0x0;
let mut scenario = test_scenario::begin(sender);
let mut id = scenario.new_object();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL. Didn't know we have it on scenario.

@tnowacki tnowacki changed the title [sui-framework] Macros for dynamimc fields and dynamic object fields [sui-framework] Macros for dynamic fields and dynamic object fields Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants