@@ -149,6 +149,40 @@ impl<T, E: ToStr> Result<T, E> {
149
149
}
150
150
}
151
151
152
+ /// Call a method based on a previous result
153
+ ///
154
+ /// If `self` is `Ok` then the value is extracted and passed to `op`
155
+ /// whereupon `op`s result is wrapped in `Ok` and returned. if `self` is
156
+ /// `Err` then it is immediately returned. This function can be used to
157
+ /// compose the results of two functions.
158
+ ///
159
+ /// Example:
160
+ ///
161
+ /// let res = do read_file(file).map_move |buf| {
162
+ /// parse_bytes(buf)
163
+ /// }
164
+ #[ inline]
165
+ pub fn map_move < U > ( self , op : & fn ( T ) -> U ) -> Result < U , E > {
166
+ match self {
167
+ Ok ( t) => Ok ( op ( t) ) ,
168
+ Err ( e) => Err ( e)
169
+ }
170
+ }
171
+
172
+ /// Call a method based on a previous result
173
+ ///
174
+ /// If `self` is `Err` then the value is extracted and passed to `op`
175
+ /// whereupon `op`s result is wrapped in an `Err` and returned. if `self` is
176
+ /// `Ok` then it is immediately returned. This function can be used to pass
177
+ /// through a successful result while handling an error.
178
+ #[ inline]
179
+ pub fn map_err_move < F > ( self , op : & fn ( E ) -> F ) -> Result < T , F > {
180
+ match self {
181
+ Ok ( t) => Ok ( t) ,
182
+ Err ( e) => Err ( op ( e) )
183
+ }
184
+ }
185
+
152
186
/// Call a method based on a previous result
153
187
///
154
188
/// If `self` is `Ok` then the value is extracted and passed to `op`
@@ -312,7 +346,9 @@ pub fn iter_vec2<S, T, U: ToStr>(ss: &[S], ts: &[T],
312
346
#[ cfg( test) ]
313
347
mod tests {
314
348
use super :: * ;
349
+
315
350
use either;
351
+ use str:: OwnedStr ;
316
352
317
353
pub fn op1 ( ) -> Result < int , ~str > { Ok ( 666 ) }
318
354
@@ -359,14 +395,26 @@ mod tests {
359
395
360
396
#[test]
361
397
pub fn test_impl_map() {
362
- assert_eq!(Ok::<~str, ~str>(~" a").map(|_x| ~" b"), Ok(~" b "));
363
- assert_eq!(Err::<~str, ~str>(~" a").map(|_x| ~" b"), Err(~" a"));
398
+ assert_eq!(Ok::<~str, ~str>(~" a").map(|x| ( ~" b").append(*x)) , Ok(~" ba "));
399
+ assert_eq!(Err::<~str, ~str>(~" a").map(|x| ( ~" b").append(*x) ), Err(~" a"));
364
400
}
365
401
366
402
#[test]
367
403
pub fn test_impl_map_err() {
368
- assert_eq!(Ok::<~str, ~str>(~" a").map_err(|_x| ~" b"), Ok(~" a"));
369
- assert_eq!(Err::<~str, ~str>(~" a").map_err(|_x| ~" b"), Err(~" b") ) ;
404
+ assert_eq!(Ok::<~str, ~str>(~" a").map_err(|x| (~" b").append(*x)), Ok(~" a"));
405
+ assert_eq!(Err::<~str, ~str>(~" a").map_err(|x| (~" b").append(*x)), Err(~" ba"));
406
+ }
407
+
408
+ #[test]
409
+ pub fn test_impl_map_move() {
410
+ assert_eq!(Ok::<~str, ~str>(~" a").map_move(|x| x + ~" b"), Ok(~" ab"));
411
+ assert_eq!(Err::<~str, ~str>(~" a").map_move(|x| x + ~" b"), Err(~" a"));
412
+ }
413
+
414
+ #[test]
415
+ pub fn test_impl_map_err_move() {
416
+ assert_eq!(Ok::<~str, ~str>(~" a").map_err_move(|x| x + ~" b"), Ok(~" a"));
417
+ assert_eq!(Err::<~str, ~str>(~" a").map_err_move(|x| x + ~" b"), Err(~" ab") ) ;
370
418
}
371
419
372
420
#[ test]
0 commit comments