@@ -2918,15 +2918,9 @@ pub trait BoxPlace<Data: ?Sized> : Place<Data> {
2918
2918
fn make_place ( ) -> Self ;
2919
2919
}
2920
2920
2921
- /// A trait for types which have success and error states and are meant to work
2922
- /// with the question mark operator.
2923
- /// When the `?` operator is used with a value, whether the value is in the
2924
- /// success or error state is determined by calling `translate`.
2925
- ///
2926
- /// This trait is **very** experimental, it will probably be iterated on heavily
2927
- /// before it is stabilised. Implementors should expect change. Users of `?`
2928
- /// should not rely on any implementations of `Carrier` other than `Result`,
2929
- /// i.e., you should not expect `?` to continue to work with `Option`, etc.
2921
+ /// This trait has been superseded by the `Try` trait, but must remain
2922
+ /// here as `?` is still lowered to it in stage0 .
2923
+ #[ cfg( stage0) ]
2930
2924
#[ unstable( feature = "question_mark_carrier" , issue = "31436" ) ]
2931
2925
pub trait Carrier {
2932
2926
/// The type of the value when computation succeeds.
@@ -2945,6 +2939,7 @@ pub trait Carrier {
2945
2939
fn translate < T > ( self ) -> T where T : Carrier < Success =Self :: Success , Error =Self :: Error > ;
2946
2940
}
2947
2941
2942
+ #[ cfg( stage0) ]
2948
2943
#[ unstable( feature = "question_mark_carrier" , issue = "31436" ) ]
2949
2944
impl < U , V > Carrier for Result < U , V > {
2950
2945
type Success = U ;
@@ -2970,21 +2965,57 @@ impl<U, V> Carrier for Result<U, V> {
2970
2965
2971
2966
struct _DummyErrorType ;
2972
2967
2973
- impl Carrier for _DummyErrorType {
2974
- type Success = ( ) ;
2968
+ impl Try for _DummyErrorType {
2969
+ type Ok = ( ) ;
2975
2970
type Error = ( ) ;
2976
2971
2977
- fn from_success ( _: ( ) ) -> _DummyErrorType {
2972
+ fn into_result ( self ) -> Result < Self :: Ok , Self :: Error > {
2973
+ Ok ( ( ) )
2974
+ }
2975
+
2976
+ fn from_ok ( _: ( ) ) -> _DummyErrorType {
2978
2977
_DummyErrorType
2979
2978
}
2980
2979
2981
2980
fn from_error ( _: ( ) ) -> _DummyErrorType {
2982
2981
_DummyErrorType
2983
2982
}
2983
+ }
2984
2984
2985
- fn translate < T > ( self ) -> T
2986
- where T : Carrier < Success =( ) , Error =( ) >
2987
- {
2988
- T :: from_success ( ( ) )
2989
- }
2985
+ /// A trait for customizing the behaviour of the `?` operator.
2986
+ ///
2987
+ /// A type implementing `Try` is one that has a canonical way to view it
2988
+ /// in terms of a success/failure dichotomy. This trait allows both
2989
+ /// extracting those success or failure values from an existing instance and
2990
+ /// creating a new instance from a success or failure value.
2991
+ #[ unstable( feature = "try_trait" , issue = "31436" ) ]
2992
+ pub trait Try {
2993
+ /// The type of this value when viewed as successful.
2994
+ #[ unstable( feature = "try_trait" , issue = "31436" ) ]
2995
+ type Ok ;
2996
+ /// The type of this value when viewed as failed.
2997
+ #[ unstable( feature = "try_trait" , issue = "31436" ) ]
2998
+ type Error ;
2999
+
3000
+ /// Applies the "?" operator. A return of `Ok(t)` means that the
3001
+ /// execution should continue normally, and the result of `?` is the
3002
+ /// value `t`. A return of `Err(e)` means that execution should branch
3003
+ /// to the innermost enclosing `catch`, or return from the function.
3004
+ ///
3005
+ /// If an `Err(e)` result is returned, the value `e` will be "wrapped"
3006
+ /// in the return type of the enclosing scope (which must itself implement
3007
+ /// `Try`). Specifically, the value `X::from_error(From::from(e))`
3008
+ /// is returned, where `X` is the return type of the enclosing function.
3009
+ #[ unstable( feature = "try_trait" , issue = "31436" ) ]
3010
+ fn into_result ( self ) -> Result < Self :: Ok , Self :: Error > ;
3011
+
3012
+ /// Wrap an error value to construct the composite result. For example,
3013
+ /// `Result::Err(x)` and `Result::from_error(x)` are equivalent.
3014
+ #[ unstable( feature = "try_trait" , issue = "31436" ) ]
3015
+ fn from_error ( v : Self :: Error ) -> Self ;
3016
+
3017
+ /// Wrap an OK value to construct the composite result. For example,
3018
+ /// `Result::Ok(x)` and `Result::from_ok(x)` are equivalent.
3019
+ #[ unstable( feature = "try_trait" , issue = "31436" ) ]
3020
+ fn from_ok ( v : Self :: Ok ) -> Self ;
2990
3021
}
0 commit comments