]> git.proxmox.com Git - rustc.git/blob - src/libcore/ops/try.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / libcore / ops / try.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /// A trait for customizing the behavior of the `?` operator.
12 ///
13 /// A type implementing `Try` is one that has a canonical way to view it
14 /// in terms of a success/failure dichotomy. This trait allows both
15 /// extracting those success or failure values from an existing instance and
16 /// creating a new instance from a success or failure value.
17 #[unstable(feature = "try_trait", issue = "42327")]
18 #[rustc_on_unimplemented(
19 on(all(
20 any(from_method="from_error", from_method="from_ok"),
21 from_desugaring="?"),
22 message="the `?` operator can only be used in a \
23 function that returns `Result` \
24 (or another type that implements `{Try}`)",
25 label="cannot use the `?` operator in a function that returns `{Self}`"),
26 on(all(from_method="into_result", from_desugaring="?"),
27 message="the `?` operator can only be applied to values \
28 that implement `{Try}`",
29 label="the `?` operator cannot be applied to type `{Self}`")
30 )]
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 }