-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implementation of Result::swap
and tests
#81323
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @kennytm (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
The job Click to see the possible cause of the failure (guessed by this bot)
|
What's the practical use case for this method? |
The job Click to see the possible cause of the failure (guessed by this bot)
|
@lzutao I described two use cases I could think of in the tracking issue. I intend this method to be primarily used when the success case of a function is actually an not-so-happy path for the caller. With this function, it would be easy to propagate that success result as an error. let ok = not_so_good_success().swap()?; This is basically a case I had in one of my projects and I thought it would be a nice addition because currently, the idiomatic way of doing the same thing would be something like let ok = match not_so_good_success() {
Ok(err) => return Err(err),
Err(ok) = ok,
}; |
The job Click to see the possible cause of the failure (guessed by this bot)
|
I don't think fn do_something() -> Result<(), Error> {
let e1 = attempt("1").swap()?;
let e2 = attempt("2").swap()?;
let e3 = attempt("3").swap()?;
Err(Error::all(vec![e1, e2, e3]))
} you need to do double-swap in a fn do_something() -> Result<(), Error> {
(|| {
let e1 = attempt("1").swap()?;
let e2 = attempt("2").swap()?;
let e3 = attempt("3").swap()?;
Ok(Error::all(vec![e1, e2, e3]))
}()).swap()
} |
When it comes to early return from the Ok variant, I'm much more interested in the approach via We define a struct YeetSuccess<R>(R);
impl<T, E> Result<T, E> {
/// Wraps this result in a type such that, when applied with `?`,
/// will either `return Ok(t)` early or extract the error value out.
pub fn yeet_ok(self) -> YeetSuccess<Self> {
YeetSuccess(self)
}
}
impl<T> Option<T> {
/// Wraps this result in a type such that, when applied with `?`,
/// will either `return Some(t)` early or results in an empty value.
pub fn yeet_some(self) -> YeetSuccess<Self> {
YeetSuccess(self)
}
} Then we implement the try_trait_v2 traits.impl<T, E> Bubble for YeetSuccess<Result<T, E>> {
type Continue = E;
type Holder = YeetSuccess<Result<T, !>>;
fn continue_with(e: E) -> Self {
Self(Err(e))
}
fn branch(self) -> ControlFlow<YeetSuccess<Result<T, !>>, E> {
match self.0 {
Ok(t) => ControlFlow::Break(YeetSuccess(Ok(t))),
Err(e) => ControlFlow::Continue(e),
}
}
}
impl<T> Bubble for YeetSuccess<Option<T>> {
type Continue = ();
type Holder = YeetSuccess<Option<T>>;
fn continue_with(_: ()) -> Self {
Self(None)
}
fn branch(self) -> ControlFlow<YeetSuccess<Option<T>>, ()> {
match self.0 {
Some(t) => ControlFlow::Break(YeetSuccess(Some(t))),
None => ControlFlow::Continue(()),
}
}
}
impl<T, E> BreakHolder<E> for YeetSuccess<Result<T, !>> {
type Output = YeetSuccess<Result<T, E>>;
}
impl<T> BreakHolder<()> for YeetSuccess<Option<T>> {
type Output = YeetSuccess<Option<T>>;
}
impl<T, E> Try<YeetSuccess<Result<T, !>>> for Result<T, E> {
fn from_holder(holder: YeetSuccess<Result<T, !>>) -> Self {
match holder.0 {
Ok(t) => Ok(t),
Err(e) => match e {},
}
}
}
impl<T> Try<YeetSuccess<Option<T>>> for Option<T> {
fn from_holder(holder: YeetSuccess<Option<T>>) -> Self {
holder.0
}
}
impl<H, R> Try<YeetSuccess<H>> for YeetSuccess<R>
where
YeetSuccess<R>: Bubble,
R: Try<YeetSuccess<H>>,
{
fn from_holder(holder: YeetSuccess<H>) -> Self {
YeetSuccess(R::from_holder(holder))
}
} With these, we can perform the early Ok return more naturally like: fn do_something() -> Result<(), Error> {
let e1 = attempt("1").yeet_ok()?;
let e2 = attempt("2").yeet_ok()?;
let e3 = attempt("3").yeet_ok()?;
Err(Error::All(vec![e1, e2, e3]))
}
fn do_something_option() -> Option<&'static str> {
attempt_option("1").yeet_some()?;
attempt_option("2").yeet_some()?;
attempt_option("3").yeet_some()?;
None
} |
@kennytm I see how |
Yes. Thanks for the contribution. In the future you could also first raise a thread on the internals forum or #t-libs zulip stream to see if there's general interest to a new library feature. |
@kennytm I will keep that in mind! |
This adds the
swap
method onResult
as well as a test function for it.Tracking issue: #81332