Add Option::inspect_none & minor example improvements to other new inspect methods#94317
Add Option::inspect_none & minor example improvements to other new inspect methods#94317cyqsimon wants to merge 3 commits intorust-lang:masterfrom cyqsimon:inspect-none
Option::inspect_none & minor example improvements to other new inspect methods#94317Conversation
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. |
dtolnay
left a comment
There was a problem hiding this comment.
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 |
dtolnay
left a comment
There was a problem hiding this comment.
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
inspectmethods to (hopefully) improve clarity.