10
10
11
11
//! A type representing either success or failure
12
12
13
- use any:: Any ;
14
13
use clone:: Clone ;
15
14
use cmp:: Eq ;
16
15
use fmt;
17
16
use iter:: Iterator ;
18
- use kinds:: Send ;
19
- use option:: { None , Option , Some , OptionIterator } ;
17
+ use option:: { None , Option , Some } ;
20
18
use option:: { ToOption , IntoOption , AsOption } ;
21
19
use str:: OwnedStr ;
22
20
use to_str:: ToStr ;
23
21
use vec:: OwnedVector ;
24
22
use vec;
25
23
26
24
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
27
- ///
28
- /// In order to provide informative error messages, `E` is required to implement `ToStr`.
29
- /// It is further recommended for `E` to be a descriptive error type, eg a `enum` for
30
- /// all possible errors cases.
31
25
#[ deriving( Clone , DeepClone , Eq , Ord , TotalEq , TotalOrd , ToStr ) ]
32
26
pub enum Result < T , E > {
33
- /// Contains the successful result value
27
+ /// Contains the success value
34
28
Ok ( T ) ,
29
+
35
30
/// Contains the error value
36
31
Err ( E )
37
32
}
@@ -40,7 +35,7 @@ pub enum Result<T, E> {
40
35
// Type implementation
41
36
/////////////////////////////////////////////////////////////////////////////
42
37
43
- impl < T , E : ToStr > Result < T , E > {
38
+ impl < T , E > Result < T , E > {
44
39
/////////////////////////////////////////////////////////////////////////
45
40
// Querying the contained values
46
41
/////////////////////////////////////////////////////////////////////////
@@ -60,71 +55,48 @@ impl<T, E: ToStr> Result<T, E> {
60
55
!self . is_ok ( )
61
56
}
62
57
63
- /////////////////////////////////////////////////////////////////////////
64
- // Adapter for working with references
65
- /////////////////////////////////////////////////////////////////////////
66
-
67
- /// Convert from `Result<T, E>` to `Result<&T, &E>`
68
- #[ inline]
69
- pub fn as_ref < ' r > ( & ' r self ) -> Result < & ' r T , & ' r E > {
70
- match * self {
71
- Ok ( ref x) => Ok ( x) ,
72
- Err ( ref x) => Err ( x) ,
73
- }
74
- }
75
-
76
- /// Convert from `Result<T, E>` to `Result<&mut T, &mut E>`
77
- #[ inline]
78
- pub fn as_mut < ' r > ( & ' r mut self ) -> Result < & ' r mut T , & ' r mut E > {
79
- match * self {
80
- Ok ( ref mut x) => Ok ( x) ,
81
- Err ( ref mut x) => Err ( x) ,
82
- }
83
- }
84
58
85
59
/////////////////////////////////////////////////////////////////////////
86
- // Getting to contained values
60
+ // Adapter for each variant
87
61
/////////////////////////////////////////////////////////////////////////
88
62
89
- /// Unwraps a result, yielding the content of an `Ok`.
90
- /// Fails if the value is a `Err` with a custom failure message provided by `msg`.
63
+ /// Convert from `Result<T, E>` to `Option<T>`
91
64
#[ inline]
92
- pub fn expect < M : Any + Send > ( self , msg : M ) -> T {
65
+ pub fn ok ( self ) -> Option < T > {
93
66
match self {
94
- Ok ( t ) => t ,
95
- Err ( _) => fail ! ( msg ) ,
67
+ Ok ( x ) => Some ( x ) ,
68
+ Err ( _) => None ,
96
69
}
97
70
}
98
71
99
- /// Unwraps a result, yielding the content of an `Err`.
100
- /// Fails if the value is a `Ok` with a custom failure message provided by `msg`.
72
+ /// Convert from `Result<T, E>` to `Option<E>`
101
73
#[ inline]
102
- pub fn expect_err < M : Any + Send > ( self , msg : M ) -> E {
74
+ pub fn err ( self ) -> Option < E > {
103
75
match self {
104
- Err ( e ) => e ,
105
- Ok ( _ ) => fail ! ( msg ) ,
76
+ Ok ( _ ) => None ,
77
+ Err ( x ) => Some ( x ) ,
106
78
}
107
79
}
108
80
109
- /// Unwraps a result, yielding the content of an `Ok`.
110
- /// Fails if the value is a `Err` with an error message derived
111
- /// from `E`'s `ToStr` implementation.
81
+ /////////////////////////////////////////////////////////////////////////
82
+ // Adapter for working with references
83
+ /////////////////////////////////////////////////////////////////////////
84
+
85
+ /// Convert from `Result<T, E>` to `Result<&T, &E>`
112
86
#[ inline]
113
- pub fn unwrap ( self ) -> T {
114
- match self {
115
- Ok ( t) => t,
116
- Err ( e) => fail ! ( "called `Result::unwrap()` on `Err` value '{}'" ,
117
- e. to_str( ) ) ,
87
+ pub fn as_ref < ' r > ( & ' r self ) -> Result < & ' r T , & ' r E > {
88
+ match * self {
89
+ Ok ( ref x) => Ok ( x) ,
90
+ Err ( ref x) => Err ( x) ,
118
91
}
119
92
}
120
93
121
- /// Unwraps a result, yielding the content of an `Err`.
122
- /// Fails if the value is a `Ok`.
94
+ /// Convert from `Result<T, E>` to `Result<&mut T, &mut E>`
123
95
#[ inline]
124
- pub fn unwrap_err ( self ) -> E {
125
- match self {
126
- Ok ( _ ) => fail ! ( "called `Result::unwrap_err()` on an `Ok` value" ) ,
127
- Err ( e ) => e
96
+ pub fn as_mut < ' r > ( & ' r mut self ) -> Result < & ' r mut T , & ' r mut E > {
97
+ match * self {
98
+ Ok ( ref mut x ) => Ok ( x ) ,
99
+ Err ( ref mut x ) => Err ( x ) ,
128
100
}
129
101
}
130
102
@@ -163,34 +135,6 @@ impl<T, E: ToStr> Result<T, E> {
163
135
}
164
136
}
165
137
166
- /////////////////////////////////////////////////////////////////////////
167
- // Iterator constructors
168
- /////////////////////////////////////////////////////////////////////////
169
-
170
- /// Returns an `Iterator` over one or zero references to the value of an `Ok`
171
- ///
172
- /// Example:
173
- ///
174
- /// for buf in read_file(file) {
175
- /// print_buf(buf)
176
- /// }
177
- #[ inline]
178
- pub fn iter < ' r > ( & ' r self ) -> OptionIterator < & ' r T > {
179
- match * self {
180
- Ok ( ref t) => Some ( t) ,
181
- Err ( ..) => None ,
182
- } . move_iter ( )
183
- }
184
-
185
- /// Returns an `Iterator` over one or zero references to the value of an `Err`
186
- #[ inline]
187
- pub fn iter_err < ' r > ( & ' r self ) -> OptionIterator < & ' r E > {
188
- match * self {
189
- Ok ( ..) => None ,
190
- Err ( ref t) => Some ( t) ,
191
- } . move_iter ( )
192
- }
193
-
194
138
////////////////////////////////////////////////////////////////////////
195
139
// Boolean operations on the values, eager and lazy
196
140
/////////////////////////////////////////////////////////////////////////
@@ -239,17 +183,23 @@ impl<T, E: ToStr> Result<T, E> {
239
183
// Common special cases
240
184
/////////////////////////////////////////////////////////////////////////
241
185
242
- /// Get a reference to the value out of a successful result
243
- ///
244
- /// # Failure
245
- ///
246
- /// If the result is an error
186
+ /// Unwraps a result, yielding the content of an `Ok`.
187
+ /// Fails if the value is an `Err`.
247
188
#[ inline]
248
- pub fn get_ref < ' a > ( & ' a self ) -> & ' a T {
249
- match * self {
250
- Ok ( ref t) => t,
251
- Err ( ref e) => fail ! ( "called `Result::get_ref()` on `Err` value '{}'" ,
252
- e. to_str( ) ) ,
189
+ pub fn unwrap ( self ) -> T {
190
+ match self {
191
+ Ok ( t) => t,
192
+ Err ( _) => fail ! ( "called `Result::unwrap()` on an `Err` value" )
193
+ }
194
+ }
195
+
196
+ /// Unwraps a result, yielding the content of an `Err`.
197
+ /// Fails if the value is an `Ok`.
198
+ #[ inline]
199
+ pub fn unwrap_err ( self ) -> E {
200
+ match self {
201
+ Ok ( _) => fail ! ( "called `Result::unwrap_err()` on an `Ok` value" ) ,
202
+ Err ( e) => e
253
203
}
254
204
}
255
205
}
@@ -458,31 +408,6 @@ mod tests {
458
408
assert_eq!(op2().or_else(|e| Err::<int, ~str>(e + " !")).unwrap_err(), ~" sadface!");
459
409
}
460
410
461
- #[test]
462
- pub fn test_impl_iter() {
463
- let mut valid = false;
464
- let okval = Ok::<~str, ~str>(~" a");
465
- okval.iter().next().map(|_| { valid = true; });
466
- assert!(valid);
467
-
468
- let errval = Err::<~str, ~str>(~" b");
469
- errval.iter().next().map(|_| { valid = false; });
470
- assert!(valid);
471
- }
472
-
473
- #[test]
474
- pub fn test_impl_iter_err() {
475
- let mut valid = true;
476
- let okval = Ok::<~str, ~str>(~" a");
477
- okval.iter_err().next().map(|_| { valid = false });
478
- assert!(valid);
479
-
480
- valid = false;
481
- let errval = Err::<~str, ~str>(~" b");
482
- errval.iter_err().next().map(|_| { valid = true });
483
- assert!(valid);
484
- }
485
-
486
411
#[test]
487
412
pub fn test_impl_map() {
488
413
assert_eq!(Ok::<~str, ~str>(~" a").map(|x| x + " b"), Ok(~" ab"));
@@ -495,12 +420,6 @@ mod tests {
495
420
assert_eq!(Err::<~str, ~str>(~" a").map_err(|x| x + " b"), Err(~" ab"));
496
421
}
497
422
498
- #[test]
499
- pub fn test_get_ref_method() {
500
- let foo: Result<int, ()> = Ok(100);
501
- assert_eq!(*foo.get_ref(), 100);
502
- }
503
-
504
423
#[test]
505
424
fn test_collect() {
506
425
assert_eq!(collect(range(0, 0)
0 commit comments