]> git.lizzy.rs Git - rust.git/blob - src/libcore/ops/try.rs
Clear out std, not std tools
[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 #[rustc_on_unimplemented(
9     on(
10         all(
11             any(from_method = "from_error", from_method = "from_ok"),
12             from_desugaring = "QuestionMark"
13         ),
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}`"
25     )
26 )]
27 #[doc(alias = "?")]
28 pub trait Try {
29     /// The type of this value when viewed as successful.
30     #[unstable(feature = "try_trait", issue = "42327")]
31     type Ok;
32     /// The type of this value when viewed as failed.
33     #[unstable(feature = "try_trait", issue = "42327")]
34     type Error;
35
36     /// Applies the "?" operator. A return of `Ok(t)` means that the
37     /// execution should continue normally, and the result of `?` is the
38     /// value `t`. A return of `Err(e)` means that execution should branch
39     /// to the innermost enclosing `catch`, or return from the function.
40     ///
41     /// If an `Err(e)` result is returned, the value `e` will be "wrapped"
42     /// in the return type of the enclosing scope (which must itself implement
43     /// `Try`). Specifically, the value `X::from_error(From::from(e))`
44     /// is returned, where `X` is the return type of the enclosing function.
45     #[unstable(feature = "try_trait", issue = "42327")]
46     fn into_result(self) -> Result<Self::Ok, Self::Error>;
47
48     /// Wrap an error value to construct the composite result. For example,
49     /// `Result::Err(x)` and `Result::from_error(x)` are equivalent.
50     #[unstable(feature = "try_trait", issue = "42327")]
51     fn from_error(v: Self::Error) -> Self;
52
53     /// Wrap an OK value to construct the composite result. For example,
54     /// `Result::Ok(x)` and `Result::from_ok(x)` are equivalent.
55     #[unstable(feature = "try_trait", issue = "42327")]
56     fn from_ok(v: Self::Ok) -> Self;
57 }