forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#42275 - scottmcm:try-trait, r=nikomatsakis
Lower `?` to `Try` instead of `Carrier` The easy parts of rust-lang/rfcs#1859, whose FCP completed without further comments. Just the trait and the lowering -- neither the error message improvements nor the insta-stable impl for Option nor exhaustive docs. Based on a [github search](https://github.com/search?l=rust&p=1&q=question_mark_carrier&type=Code&utf8=%E2%9C%93), this will break the following: - https://github.com/pfpacket/rust-9p/blob/00206e34c680198a0ac7c2f066cc2954187d4fac/src/serialize.rs#L38 - https://github.com/peterdelevoryas/bufparse/blob/b1325898f4fc2c67658049196c12da82548af350/src/result.rs#L50 The other results appear to be files from libcore or its tests. I could also leave Carrier around after stage0 and `impl<T:Carrier> Try for T` if that would be better. r? @nikomatsakis Edit: Oh, and it might accidentally improve perf, based on rust-lang#37939 (comment), since `Try::into_result` for `Result` is an obvious no-op, unlike `Carrier::translate`.
- Loading branch information
Showing
7 changed files
with
138 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# `try_trait` | ||
|
||
The tracking issue for this feature is: [#42327] | ||
|
||
[#42327]: https://github.com/rust-lang/rust/issues/42327 | ||
|
||
------------------------ | ||
|
||
This introduces a new trait `Try` for extending the `?` operator to types | ||
other than `Result` (a part of [RFC 1859]). The trait provides the canonical | ||
way to _view_ a type in terms of a success/failure dichotomy. This will | ||
allow `?` to supplant the `try_opt!` macro on `Option` and the `try_ready!` | ||
macro on `Poll`, among other things. | ||
|
||
[RFC 1859]: https://github.com/rust-lang/rfcs/pull/1859 | ||
|
||
Here's an example implementation of the trait: | ||
|
||
```rust,ignore | ||
/// A distinct type to represent the `None` value of an `Option`. | ||
/// | ||
/// This enables using the `?` operator on `Option`; it's rarely useful alone. | ||
#[derive(Debug)] | ||
#[unstable(feature = "try_trait", issue = "42327")] | ||
pub struct None { _priv: () } | ||
#[unstable(feature = "try_trait", issue = "42327")] | ||
impl<T> ops::Try for Option<T> { | ||
type Ok = T; | ||
type Error = None; | ||
fn into_result(self) -> Result<T, None> { | ||
self.ok_or(None { _priv: () }) | ||
} | ||
fn from_ok(v: T) -> Self { | ||
Some(v) | ||
} | ||
fn from_error(_: None) -> Self { | ||
None | ||
} | ||
} | ||
``` | ||
|
||
Note the `Error` associated type here is a new marker. The `?` operator | ||
allows interconversion between different `Try` implementers only when | ||
the error type can be converted `Into` the error type of the enclosing | ||
function (or catch block). Having a distinct error type (as opposed to | ||
just `()`, or similar) restricts this to where it's semantically meaningful. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters