-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add Option::inspect_none
& minor example improvements to other new inspect
methods
#94317
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
1 similar comment
r? rust-lang/libs |
☔ The latest upstream changes (presumably #94824) made this pull request unmergeable. Please resolve the merge conflicts. |
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.
I have never been thrilled about any of the inspect
methods, but this one seems the least appealing to me so far. I would prefer not to build this into the standard library.
I'll ping @rust-lang/libs-api in case someone else sees this being valuable.
I pretty much completely agree with you. I'm not against inspect methods as a concept but I don't think warning on my_long_method_chain
// ... many methods later
.tap(|my_option| if my_option.is_none() { print_my_warning() })
// continuing my method chain... Even then, I've rarely if ever had a need for this crate. |
The particular reason I believe something like this is useful is cases like this: let arr2d = vec![vec![0, 1, 2], vec![3, 4, 5], vec![6, 7, 8]];
let (x, y) = (1, 5);
let el = arr2d
.get(y)
.inspect_none(|| warn!("y out of bound"))
.and_then(|row| row.get(x).inspect_none(|| warn!("x out of bound"))); That being said, I am open to the idea of a better name than |
@cyqsimon think this is consistent with my expectation in my previous comment. What isn't clear to me is what advantage you get from // inspect_none(|| ... )
let arr2d = vec![vec![0, 1, 2], vec![3, 4, 5], vec![6, 7, 8]];
let (x, y) = (1, 5);
let el = arr2d
.get(y)
.inspect_none(|| warn!("y out of bound"))
.and_then(|row| row.get(x).inspect_none(|| warn!("x out of bound")));
// inspect(|opt| if opt.is_none() { ... })
let arr2d = vec![vec![0, 1, 2], vec![3, 4, 5], vec![6, 7, 8]];
let (x, y) = (1, 5);
let el = arr2d
.get(y)
.inspect(|opt| if opt.is_none() { warn!("y out of bound") })
.and_then(|row| row.get(x).inspect(|opt| if opt.is_none() { warn!("x out of bound") })); I feel like the more explicit API if anything directly resolves the confusion you pointed out with |
I only use I would prefer a general solution like If fn warn_y_out_of_bounds<T>(opt: Option<T>) {
if opt.is_none() {
warn!("y out of bound")
}
}
// inspect(|opt| if opt.is_none() { ... })
let arr2d = vec![vec![0, 1, 2], vec![3, 4, 5], vec![6, 7, 8]];
let (x, y) = (1, 5);
let el = arr2d
.get(y)
.inspect(warn_y_out_of_bounds)
.and_then(|row| row.get(x).inspect(|opt| if opt.is_none() { warn!("x out of bound") })); Edit: I didn't notice the |
related to this I did some more digging into the tap crate and talked a bit with the author and realized that they do have some tap traits for Applying that API to your example gives: // inspect_break(|None| ...)
let arr2d = vec![vec![0, 1, 2], vec![3, 4, 5], vec![6, 7, 8]];
let (x, y) = (1, 5);
let el = arr2d
.get(y)
.inspect_break(|None| warn!("y out of bound") })
.and_then(|row| row.get(x).inspect_break(|None| warn!("x out of bound"))); I hope you find this as aesthetically pleasing as I do. Footnotes |
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.
I'll close the PR because I would prefer not to build this API into the standard library as I wrote above, and there hasn't been a compelling argument in favor since then.
Thanks anyway for the PR!
See #91345
And some minor changes to the examples of other new
inspect
methods to (hopefully) improve clarity.