Skip to content

Tracking Issue for result_swap #81322

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

Closed
2 of 4 tasks
gymore-io opened this issue Jan 24, 2021 · 5 comments
Closed
2 of 4 tasks

Tracking Issue for result_swap #81322

gymore-io opened this issue Jan 24, 2021 · 5 comments
Labels
A-result-option Area: Result and Option combinators C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@gymore-io
Copy link

gymore-io commented Jan 24, 2021

Feature gate: #![feature(result_swap)]

This is a tracking issue for result_swap

I propose the addition of a swap method for the Result type that would basically turn an Ok into an Err and the other way around.

Motivation

This would obviously allow more straightforward code when one need to propagate the success type of a function as an error.

fn my_function(vec: &mut Vec<u8>) -> Result<usize, usize> {
    // The value should not be found in the given vector.
    let b = vec.binary_search(&10).swap()?;
    vec.insert(b, 10);

    Ok(b)
}

Used with the Result constructors that are present on Option, this method also can come in handy.

fn expect_none(o: Option<u8>) -> Result<(), u8> {
    o.ok_or(()).swap()
}

Note that this second case could also be fixed with something like err_or (being discussed here #80190).

Steps

Unresolved Questions

  • The name swap may be confused with the already-used semantic of mem::swap or slice::swap which both have a slightly different meaning. The alternative I had for this matter was the name not (the function basically inverts the meaning of the Result) but I think it makes it even more confusing.
  • Do we want an Option::swap equivalent that would map Some(old_value) to None and None to Some(new_value) where new_value is a parameter of Option::swap? Such a function would probably require a Option::swap_with counterpart that takes a FnOnce() -> T.
@gymore-io gymore-io added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jan 24, 2021
@gymore-io
Copy link
Author

@rustbot label +C-tracking-issue +A-result-option

@rustbot rustbot added the A-result-option Area: Result and Option combinators label Jan 24, 2021
@egilburg
Copy link

egilburg commented Jan 24, 2021

  1. Bikeshed: Would something like and_err be more clear or similar to existing chaining like ok_or?
  2. Is there similar behavior on Option? E.g. option.swap(new_val) would replace Ok(old_val) with None and None with Ok(new_val)

@gymore-io
Copy link
Author

@egilburg I'm not sure to understand very well what you think meant about and_err. Regarding Option::swap, I think it maps pretty well to Result::swap (assuming you wanted to say that it would map None to Some and Some to None). In my opinion however, I find it a bit more confusing because it is giving a slightly different semantic to swap. That may or may not be something we want to pay attention to.

I'm editing the issue to consider Option::swap either way.

@Veykril
Copy link
Member

Veykril commented Jan 24, 2021

Is this addition pulling its weight though? This Result-swapping can already be done via result.map_or_else(Ok, Err). Result already has quite a lot of methods as is.

For the Option version this doesn't work as nicely as Option::None isn't considered a callable, result.map_or_else(|| None, Some).

@gymore-io
Copy link
Author

Righteously pointed by this comment, this whole Result::swap thing is basically a less clear version of what try_trait_v2 aims to achieve with some of its traits/structures. I'm closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-result-option Area: Result and Option combinators C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants