-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking Issue for result_option_inspect
#91345
Comments
I propose to discuss adding |
I kind of like the idea of On the other hand, I don't think it's necessary to rename |
what are the next steps to stabilize this proposal? |
Could this somehow enable the following? struct Cookie {
size: usize,
};
let maybe_cookie = Some(Cookie { size: 5 });
maybe_cookie.inspect(|mut c| c.size = 3); I suppose the current alternative is: // The formatter put it onto multiple lines
maybe_cookie.map(|mut c| {
c.size = 3;
c
}); But that is not really pretty, especially when assigning to fields or directly in a function call: eat(maybe_cookie.inspect(|mut c| c.size = 3));
fn eat(cookie: Option<Cookie>) {
//
} |
@Elias-Graf You can already do this with map, with something like |
@Elias-Graf Whether |
Any progress on an FCP? |
Another set of hands for stabilizing this feature. I think it is very useful for e.g. logging a given error too and it's a nice addition in general |
Ok, from what I understand of FCP protocol, we should first make sure there are no outstanding concerns. The correct thing to do should be to ping one of the libs team members like @joshtriplett to give their comments and then potentially file an FCP. |
I don't know how much help I can be to the process, if at all (please let me know if there are things to read about how I can help at all^), but I would like to mention that I have found myself wanting to use this functionality quite often. It makes for clean code in a pretty common situation. ^I'm relatively new to Rust, and haven't contributed to it before. I would love to learn more about what that involves, though. |
I think this would be a nice addition, not sure how to push it forward though. |
Also switched to "or_else" for one construct since we don't have `inspect_err` yet. rust-lang/rust#91345
This comment was marked as resolved.
This comment was marked as resolved.
That makes sense to me. I would definitely say that adding |
There’s nothing special about an inspect that has the whole value that should be limited to That’s the tap crate https://docs.rs/tap/latest/tap/ |
@shepmaster. That's true. Fair enough. Thanks for that. Above, the body of this ticket has an unresolved question:
This discussion suggests that, yes, there probably should be. |
Instead of a trait, let x: Option<i32> = option
.tap!(|x| dbg!(x))
.tap_matches!(|Some(x)| Some(x + 1))
.inspect!(|x| println!("{x}"))
.inspect_matches!(|None| println!("x is None")); Where |
I definitely second emulating the tap crate's behavior, favoring @ibraheemdev's approach. Having a |
I don't necessarily like the addition of a macro for this, tbh, and would love to see this going the |
i just ran into this because i found the feature e.g. for this use-case here: fn foo() -> Result<Foo, SomeError> { /* ... */ }
fn bar() -> Result<Foo, SomeError> {
foo().inspect_err(|e| warn!("encountered error {} while performing task xyz", e))
} or, even simpler, when you don't actually need the result afterwards: fn foo() -> Result<(), SomeError> { /* ... */ }
fn bar() {
foo().inspect_err(|e| warn!("encountered error {} while performing task xyz", e));
} and i think that this is distinct from the
|
I completely agree,
|
What's blocking this from being stabilised? |
I would also like to see this stable, so I can deprecate my option-inspect and result-inspect crates! 😆 |
|
|
How do we push this to get stabilized? |
tl;dr
Explanation of proposed suggestionIMHO, one of the most important things when designing something is:
Something is intuitive to learn, if you only need to learn a concept once and can transfer that knowledge to other parts of the system that should behave the same (according to what has been your "entry point" of learning the concept). When we take those three points, we might come to the following conclusion: => When methods on In
Conclusion
|
Considering the points raised, let's try this again, this time keeping the closure argument as The fact that this function is not as general as it could be isn't a big issue: after all this is just a convenience method, you can always use @rfcbot fcp merge |
Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
I would like to work on a stabilization PR for it. @rustbot claim |
Since Some discussions about it: rust-lang/rfcs#3190 (comment) |
I would like to chime in on naming - I originally wanted to come argumenting for
as opposed to
The second version is harder to read because on the first glance it does not "tie" computation with logging and you need to go through the whole line looking for "x" to correlate it. While this RFC does not address either general |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
Stabilize `result_option_inspect` This PR stabilizes `result_option_inspect`: ```rust // core::option impl Option<T> { pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self; } // core::result impl Result<T, E> { pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self; pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self; } ``` <br> Tracking issue: rust-lang/rust#91345. Implementation PR: rust-lang/rust#91346. Closes rust-lang/rust#91345.
error[E0658]: use of unstable library feature 'result_option_inspect' --> server/src/sys/terrain.rs:573:34 | 573 | ... .inspect_err(|data| { | ^^^^^^^^^^^ | = note: see issue #91345 <rust-lang/rust#91345> for more information = help: add `#![feature(result_option_inspect)]` to the crate attributes to enable Reported by: pkg-fallout (direct commit to 2024Q1 as 2252f9d is missing on the branch)
chore: introduce icx-proxy library This MR introduces an icx-proxy library so that the icx-proxy can be reused by PocketIc. The diff between the original `rs/boundary_node/icx_proxy/src/main.rs` and the current `rs/boundary_node/icx_proxy/src/core.rs`: ``` 1,4d0 < // TODO: Remove after inspect_err stabilizes (rust-lang/rust#91345) < < #![allow(unstable_name_collisions)] < 12d7 < use jemallocator::Jemalloc; 15,26d9 < mod canister_alias; < mod canister_id; < mod config; < mod domain_addr; < mod error; < mod http; < mod http_client; < mod logging; < mod metrics; < mod proxy; < mod validate; < 28a12 > canister_id, 30,31c14,16 < metrics::{MetricParams, WithMetrics}, < proxy::ListenProto, --- > http_client, logging, > metrics::{self, MetricParams, WithMetrics}, > proxy::{self, ListenProto}, 35,37d19 < #[global_allocator] < static GLOBAL: Jemalloc = Jemalloc; < 67c49 < struct Opts { --- > pub struct Opts { 163c145 < fn main() -> Result<(), Error> { --- > pub fn main(opts: Opts) -> Result<(), Error> { 186c168 < } = Opts::parse(); --- > } = opts; ``` See merge request dfinity-lab/public/ic!18399
Stabilize `result_option_inspect` This PR stabilizes `result_option_inspect`: ```rust // core::option impl Option<T> { pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self; } // core::result impl Result<T, E> { pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self; pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self; } ``` <br> Tracking issue: rust-lang/rust#91345. Implementation PR: rust-lang/rust#91346. Closes rust-lang/rust#91345.
Stabilize `result_option_inspect` This PR stabilizes `result_option_inspect`: ```rust // core::option impl Option<T> { pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self; } // core::result impl Result<T, E> { pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self; pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self; } ``` <br> Tracking issue: rust-lang/rust#91345. Implementation PR: rust-lang/rust#91346. Closes rust-lang/rust#91345.
Feature gate:
#![feature(result_option_inspect)]
This is a tracking issue for
Option::inspect
andResult::{inspect, inspect_err}
.Public API
Steps / History
Option::inspect
andResult::{inspect, inspect_err}
#91346Unresolved Questions
Tap
trait instd
?The text was updated successfully, but these errors were encountered: