Skip to content
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

[stdlib] Adds macros for option #18421

Merged
merged 3 commits into from
Jun 28, 2024
Merged

[stdlib] Adds macros for option #18421

merged 3 commits into from
Jun 28, 2024

Conversation

damirka
Copy link
Contributor

@damirka damirka commented Jun 26, 2024

Description

Adds macros for option, currently, contains do and destroy (identical).

Test plan

Features 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:
  • Nodes (Validators and Full nodes):
  • Indexer:
  • JSON-RPC:
  • GraphQL:
  • CLI:
  • Rust SDK:

@damirka damirka self-assigned this Jun 26, 2024
Copy link

vercel bot commented Jun 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sui-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 26, 2024 7:36pm
3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
multisig-toolkit ⬜️ Ignored (Inspect) Visit Preview Jun 26, 2024 7:36pm
sui-kiosk ⬜️ Ignored (Inspect) Visit Preview Jun 26, 2024 7:36pm
sui-typescript-docs ⬜️ Ignored (Inspect) Visit Preview Jun 26, 2024 7:36pm

Copy link
Contributor

@tnowacki tnowacki left a comment

Choose a reason for hiding this comment

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

Looks amazing! Thank you so much!

Just some style nits.
Also I would ask that we make sure @tzakian gives this a once over before we land, just since that he helped come up with a lot of the names for the macros here

Comment on lines +156 to +158
if (o.is_some()) {
$f(o.destroy_some());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

style nit

Suggested change
if (o.is_some()) {
$f(o.destroy_some());
}
if (o.is_some()) $f(o.destroy_some())

Comment on lines +164 to +166
if (o.is_some()) {
$f(o.borrow());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (o.is_some()) {
$f(o.borrow());
}
if (o.is_some()) $f(o.borrow())

/// Execute a closure on the mutable reference to the value inside `t` if it holds one.
public macro fun do_mut<$T>($o: &mut Option<$T>, $f: |&mut $T|) {
let o = $o;
if (o.is_some()) $f(o.borrow_mut());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (o.is_some()) $f(o.borrow_mut());
if (o.is_some()) $f(o.borrow_mut())

Comment on lines +179 to +180
if (o.is_some()) o
else $default
Copy link
Contributor

Choose a reason for hiding this comment

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

🔥

/// Return `None` if the value is `None`, otherwise return `Option<T>` if the predicate `f` returns true.
public macro fun filter<$T: drop>($o: Option<$T>, $f: |&$T| -> bool): Option<$T> {
let o = $o;
if (o.is_some() && $f(o.borrow())) o
Copy link
Contributor

Choose a reason for hiding this comment

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

Beautiful, love how this one turned out

option::some(5).destroy!(|x| counter = x);
option::some(10).do!(|x| counter = counter + x);

assert!(counter == 15);
Copy link
Contributor

Choose a reason for hiding this comment

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

I snuck in std::unit_test::assert_eq a while back if you want to give it a shot. This is fine though

Copy link
Contributor

@tzakian tzakian left a comment

Choose a reason for hiding this comment

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

Looks amazing! 🚀

@damirka damirka merged commit b350ded into main Jun 28, 2024
47 checks passed
@damirka damirka deleted the ds/macros-option branch June 28, 2024 18:53
tx-tomcat pushed a commit to tx-tomcat/sui-network that referenced this pull request Jul 29, 2024
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