]> git.proxmox.com Git - rustc.git/blame - library/core/src/ops/try.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / core / src / ops / try.rs
CommitLineData
3b2f2976 1/// A trait for customizing the behavior of the `?` operator.
041b39d2
XL
2///
3/// A type implementing `Try` is one that has a canonical way to view it
9fa01778 4/// in terms of a success/failure dichotomy. This trait allows both
041b39d2
XL
5/// extracting those success or failure values from an existing instance and
6/// creating a new instance from a success or failure value.
7#[unstable(feature = "try_trait", issue = "42327")]
dfeec247
XL
8#[rustc_on_unimplemented(
9 on(
10 all(
11 any(from_method = "from_error", from_method = "from_ok"),
12 from_desugaring = "QuestionMark"
60c5eb7d 13 ),
dfeec247
XL
14 message = "the `?` operator can only be used in {ItemContext} \
15 that returns `Result` or `Option` \
16 (or another type that implements `{Try}`)",
17 label = "cannot use the `?` operator in {ItemContext} that returns `{Self}`",
18 enclosing_scope = "this function should return `Result` or `Option` to accept `?`"
19 ),
20 on(
21 all(from_method = "into_result", from_desugaring = "QuestionMark"),
22 message = "the `?` operator can only be applied to values \
23 that implement `{Try}`",
24 label = "the `?` operator cannot be applied to type `{Self}`"
60c5eb7d
XL
25 )
26)]
83c7162d 27#[doc(alias = "?")]
f035d41b 28#[lang = "try"]
041b39d2
XL
29pub trait Try {
30 /// The type of this value when viewed as successful.
31 #[unstable(feature = "try_trait", issue = "42327")]
32 type Ok;
33 /// The type of this value when viewed as failed.
34 #[unstable(feature = "try_trait", issue = "42327")]
35 type Error;
36
37 /// Applies the "?" operator. A return of `Ok(t)` means that the
38 /// execution should continue normally, and the result of `?` is the
39 /// value `t`. A return of `Err(e)` means that execution should branch
40 /// to the innermost enclosing `catch`, or return from the function.
41 ///
42 /// If an `Err(e)` result is returned, the value `e` will be "wrapped"
43 /// in the return type of the enclosing scope (which must itself implement
44 /// `Try`). Specifically, the value `X::from_error(From::from(e))`
45 /// is returned, where `X` is the return type of the enclosing function.
1b1a35ee 46 #[lang = "into_result"]
041b39d2
XL
47 #[unstable(feature = "try_trait", issue = "42327")]
48 fn into_result(self) -> Result<Self::Ok, Self::Error>;
49
50 /// Wrap an error value to construct the composite result. For example,
51 /// `Result::Err(x)` and `Result::from_error(x)` are equivalent.
1b1a35ee 52 #[lang = "from_error"]
041b39d2
XL
53 #[unstable(feature = "try_trait", issue = "42327")]
54 fn from_error(v: Self::Error) -> Self;
55
56 /// Wrap an OK value to construct the composite result. For example,
57 /// `Result::Ok(x)` and `Result::from_ok(x)` are equivalent.
1b1a35ee 58 #[lang = "from_ok"]
041b39d2
XL
59 #[unstable(feature = "try_trait", issue = "42327")]
60 fn from_ok(v: Self::Ok) -> Self;
61}