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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
adds macros for option
  • Loading branch information
damirka committed Jun 26, 2024
commit 8bfddf080444bf85eca54854e4c0c0634a723c1a
83 changes: 83 additions & 0 deletions crates/sui-framework/packages/move-stdlib/sources/option.move
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,87 @@ module std::option {
let Option { vec } = t;
vec
}

// === Macro Functions ===

/// Destroy `Option<T>` and call the closure `f` on the value inside if it holds one.
public macro fun destroy<$T>($o: Option<$T>, $f: |$T|) {
let o = $o;
o.do!($f);
}

/// Destroy `Option<T>` and call the closure `f` on the value inside if it holds one.
public macro fun do<$T>($o: Option<$T>, $f: |$T|) {
let o = $o;
if (o.is_some()) {
$f(o.destroy_some());
}
Comment on lines +156 to +158
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())

}

/// Execute a closure on the value inside `t` if it holds one.
public macro fun do_ref<$T>($o: &Option<$T>, $f: |&$T|) {
let o = $o;
if (o.is_some()) {
$f(o.borrow());
}
Comment on lines +164 to +166
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())

}

/// Select the first `Some` value from the two options, or `None` if both are `None`.
/// Equivalent to Rust's `a.or(b)`.
public macro fun or<$T>($o: Option<$T>, $default: Option<$T>): Option<$T> {
let o = $o;
if (o.is_some()) o
else $default
Comment on lines +179 to +180
Copy link
Contributor

Choose a reason for hiding this comment

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

🔥

}

/// If the value is `Some`, call the closure `f` on it. Otherwise, return `None`.
/// Equivalent to Rust's `t.and_then(f)`.
public macro fun and<$T, $U>($o: Option<$T>, $f: |$T| -> Option<$U>): Option<$U> {
let o = $o;
if (o.is_some()) $f(o.extract())
else none()
}

/// If the value is `Some`, call the closure `f` on it. Otherwise, return `None`.
/// Equivalent to Rust's `t.and_then(f)`.
public macro fun and_ref<$T, $U>($o: &Option<$T>, $f: |&$T| -> Option<$U>): Option<$U> {
let o = $o;
if (o.is_some()) $f(o.borrow())
else none()
}

/// Map an `Option<T>` to `Option<U>` by applying a function to a contained value.
/// Equivalent to Rust's `t.map(f)`.
public macro fun map<$T, $U>($o: Option<$T>, $f: |$T| -> $U): Option<$U> {
let mut o = $o;
if (o.is_some()) some($f(o.extract()))
else none()
}

/// Map an `Option<T>` value to `Option<U>` by applying a function to a contained value by reference.
/// Original `Option<T>` is preserved.
/// Equivalent to Rust's `t.map(f)`.
public macro fun map_ref<$T, $U>($o: &Option<$T>, $f: |&$T| -> $U): Option<$U> {
let o = $o;
if (o.is_some()) some($f(o.borrow()))
else none()
}

/// Destroy `Option<T>` and return the value inside if it holds one, or `default` otherwise.
/// Equivalent to Rust's `t.unwrap_or(default)`.
///
/// Note: this function is a more efficient version of `destroy_with_default`, as it does not
/// evaluate the default value unless necessary. The `destroy_with_default` function should be
/// deprecated in favor of this function.
public macro fun destroy_or<$T>($o: Option<$T>, $default: $T): $T {
let o = $o;
if (o.is_some()) o.destroy_some()
else $default
}
}
34 changes: 34 additions & 0 deletions crates/sui-framework/packages/move-stdlib/tests/option_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,38 @@ module std::option_tests {
let v: vector<u64> = option::none().to_vec();
assert!(v.is_empty());
}

// === Macros ===

#[test]
fun do_destroy() {
let mut counter = 0;
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

}

#[test]
fun do_ref_mut() {
let mut counter = 0;
option::some(10).do_mut!(|x| *x = 100);
option::some(5).do_ref!(|x| counter = *x);

assert!(counter == 100);
}

#[test]
fun map_map_ref() {
assert!(option::some(5).map!(|x| vector[x]) == option::some(vector[6]));
assert!(option::some(5).map_ref!(|x| vector[*x]) == option::some(vector[6]));
assert!(option::none<u8>().map!(|x| vector[x]) == option::none());
assert!(option::none<u8>().map_ref!(|x| vector[*x]) == option::none());
}

#[test]
fun destroy_or() {
assert!(option::none().destroy_or!(10) == 10);
assert!(option::some(5).destroy_or!(10) == 5);
}
}
Loading