Skip to content

libcore/Result - Add an expect method that prints a message and the Err value #25359

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

Merged
merged 1 commit into from
Jun 15, 2015

Conversation

thepowersgang
Copy link
Contributor

As it says in the title. I've added an expect method to Result that allows printing both an error message (e.g. what operation was attempted), and the error value. This is separate from the unwrap and ok().expect("message") behaviours.

@rust-highfive
Copy link
Contributor

r? @brson

(rust_highfive has picked a reviewer for you, use r? to override)

/// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
/// ```
#[inline]
#[unstable(feature = "core", reason = "newly introduced")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're trying to use more specific features now, so maybe something like result_expect instead of core here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update to use result_expect

@sfackler
Copy link
Member

👍 this is something I've been thinking about adding for a while.

@@ -729,6 +729,26 @@ impl<T, E: fmt::Debug> Result<T, E> {
panic!("called `Result::unwrap()` on an `Err` value: {:?}", e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could now turn into just self.expect("called Result::unwrap() on an Err value").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add that to this PR, or leave it as is?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(it's fine to add this to this PR)

@bluss
Copy link
Member

bluss commented May 13, 2015

This addition needs an RFC, see RFC guidelines

@retep998
Copy link
Member

I see absolutely no reason why we shouldn't have this method.

@thepowersgang
Copy link
Contributor Author

@bluss I will prepare an RFC

@thepowersgang
Copy link
Contributor Author

RFC PR rust-lang/rfcs#1119

@brson
Copy link
Contributor

brson commented May 13, 2015

I also constantly 'expect' this method to exist, and reluctantly agree that it's important enough to put through the RFC process.

@bluss
Copy link
Member

bluss commented May 15, 2015

@brson, I'm sorry to be so dull but you could update the RFC guidelines to say that rust team can use their judgement and merge methods as they wish instead. But that's not what they say today :). I like this particular change but that makes this an easy case -- send it to RFCs. I want to follow the process that's laid out or it will be much harder to navigate proposed changes that have more contention.

Maybe I'm misinterpreting the RFC process, maybe it's only certain people that can say "this needs an RFC"?

It's not intended to be a roadblock, just a process (?)

@alexcrichton
Copy link
Member

I'm going to close this in favor of the associated RFC, but we can certainly reopen if it's accepted!

@alexcrichton alexcrichton reopened this Jun 10, 2015
@alexcrichton alexcrichton added the B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. label Jun 10, 2015
@alexcrichton
Copy link
Member

Turning this into the tracking issue for rust-lang/rfcs#1119

@thepowersgang could you update this PR with a test as well exercising expect in both the Ok and Err cases? Other than that it looks great to me, thanks!

@thepowersgang
Copy link
Contributor Author

Test added and pushed, fixed merge errors (and running local test for that now)

@thepowersgang
Copy link
Contributor Author

All done, probably needs collating into one commit.

@alexcrichton
Copy link
Member

Looks good to me! A few small stylistic nits and a squash and this is good to go!

@thepowersgang
Copy link
Contributor Author

All good for someone to merge and squash? (Or would you prefer I squash it first, not sure how that would affect the comment threads)

@alexcrichton
Copy link
Member

Thanks! Could you do the squash yourself? That way we just mark this with @bors: r+ and it's auto-tested.

@thepowersgang
Copy link
Contributor Author

Manually squashed commits, should be all good (I hope)

@alexcrichton
Copy link
Member

@bors: r+ 090aa91d44582f03b9eab9f9f2684e0c78751721

Thanks!

@bors
Copy link
Collaborator

bors commented Jun 13, 2015

⌛ Testing commit 090aa91 with merge b6a8e79...

@bors
Copy link
Collaborator

bors commented Jun 13, 2015

💔 Test failed - auto-mac-64-nopt-t

@thepowersgang
Copy link
Contributor Author

Updated commit with fixed doc comment

@sfackler
Copy link
Member

@bors r=alexcrichton

@bors
Copy link
Collaborator

bors commented Jun 15, 2015

📌 Commit 0937c10 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Jun 15, 2015

⌛ Testing commit 0937c10 with merge a54a809...

bors added a commit that referenced this pull request Jun 15, 2015
As it says in the title. I've added an `expect` method to `Result` that allows printing both an error message (e.g. what operation was attempted), and the error value. This is separate from the `unwrap` and `ok().expect("message")` behaviours.
@bors bors merged commit 0937c10 into rust-lang:master Jun 15, 2015
@thepowersgang thepowersgang deleted the result-expect-2 branch June 15, 2015 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants