-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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()); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
/// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
/// 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()); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
/// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I snuck in |
||
} | ||
|
||
#[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); | ||
} | ||
} |
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.
style nit