]> git.lizzy.rs Git - rust.git/blob - src/libcore/ops/try.rs
Rollup merge of #67154 - kraai:alloc-typos, r=Dylan-DPC
[rust.git] / src / libcore / ops / try.rs
1 /// A trait for customizing the behavior of the `?` operator.
2 ///
3 /// A type implementing `Try` is one that has a canonical way to view it
4 /// in terms of a success/failure dichotomy. This trait allows both
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")]
8 #[cfg_attr(
9     not(bootstrap),
10     rustc_on_unimplemented(
11         on(
12             all(
13                 any(from_method = "from_error", from_method = "from_ok"),
14                 from_desugaring = "QuestionMark"
15             ),
16             message = "the `?` operator can only be used in {ItemContext} \
17                        that returns `Result` or `Option` \
18                        (or another type that implements `{Try}`)",
19             label = "cannot use the `?` operator in {ItemContext} that returns `{Self}`",
20             enclosing_scope = "this function should return `Result` or `Option` to accept `?`"
21         ),
22         on(
23             all(from_method = "into_result", from_desugaring = "QuestionMark"),
24             message = "the `?` operator can only be applied to values \
25                        that implement `{Try}`",
26             label = "the `?` operator cannot be applied to type `{Self}`"
27         )
28     )
29 )]
30 #[doc(alias = "?")]
31 pub trait Try {
32     /// The type of this value when viewed as successful.
33     #[unstable(feature = "try_trait", issue = "42327")]
34     type Ok;
35     /// The type of this value when viewed as failed.
36     #[unstable(feature = "try_trait", issue = "42327")]
37     type Error;
38
39     /// Applies the "?" operator. A return of `Ok(t)` means that the
40     /// execution should continue normally, and the result of `?` is the
41     /// value `t`. A return of `Err(e)` means that execution should branch
42     /// to the innermost enclosing `catch`, or return from the function.
43     ///
44     /// If an `Err(e)` result is returned, the value `e` will be "wrapped"
45     /// in the return type of the enclosing scope (which must itself implement
46     /// `Try`). Specifically, the value `X::from_error(From::from(e))`
47     /// is returned, where `X` is the return type of the enclosing function.
48     #[unstable(feature = "try_trait", issue = "42327")]
49     fn into_result(self) -> Result<Self::Ok, Self::Error>;
50
51     /// Wrap an error value to construct the composite result. For example,
52     /// `Result::Err(x)` and `Result::from_error(x)` are equivalent.
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.
58     #[unstable(feature = "try_trait", issue = "42327")]
59     fn from_ok(v: Self::Ok) -> Self;
60 }