From 3f8d5508e5a81f036152e7228692b2a716811787 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 11 Sep 2013 06:44:17 -0700 Subject: [PATCH] std: rename Result::chain{,_err} to chain{_move,_err_move}, add chain{,_err} cc #7887 --- src/libextra/time.rs | 64 ++++++++++----------- src/librustc/middle/typeck/infer/combine.rs | 61 ++++++++++---------- src/librustc/middle/typeck/infer/glb.rs | 2 +- src/librustc/middle/typeck/infer/lattice.rs | 2 +- src/librustc/middle/typeck/infer/lub.rs | 4 +- src/librustc/middle/typeck/infer/mod.rs | 4 +- src/librustc/middle/typeck/rscope.rs | 6 +- src/librustdoc/config.rs | 14 ++--- src/libstd/io.rs | 4 +- src/libstd/result.rs | 56 +++++++++++++++--- 10 files changed, 127 insertions(+), 90 deletions(-) diff --git a/src/libextra/time.rs b/src/libextra/time.rs index 7515326a0dbb2..5f44481d602d0 100644 --- a/src/libextra/time.rs +++ b/src/libextra/time.rs @@ -442,21 +442,21 @@ fn do_strptime(s: &str, format: &str) -> Result { }, 'c' => { parse_type(s, pos, 'a', &mut *tm) - .chain(|pos| parse_char(s, pos, ' ')) - .chain(|pos| parse_type(s, pos, 'b', &mut *tm)) - .chain(|pos| parse_char(s, pos, ' ')) - .chain(|pos| parse_type(s, pos, 'e', &mut *tm)) - .chain(|pos| parse_char(s, pos, ' ')) - .chain(|pos| parse_type(s, pos, 'T', &mut *tm)) - .chain(|pos| parse_char(s, pos, ' ')) - .chain(|pos| parse_type(s, pos, 'Y', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, ' ')) + .chain_move(|pos| parse_type(s, pos, 'b', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, ' ')) + .chain_move(|pos| parse_type(s, pos, 'e', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, ' ')) + .chain_move(|pos| parse_type(s, pos, 'T', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, ' ')) + .chain_move(|pos| parse_type(s, pos, 'Y', &mut *tm)) } 'D' | 'x' => { parse_type(s, pos, 'm', &mut *tm) - .chain(|pos| parse_char(s, pos, '/')) - .chain(|pos| parse_type(s, pos, 'd', &mut *tm)) - .chain(|pos| parse_char(s, pos, '/')) - .chain(|pos| parse_type(s, pos, 'y', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, '/')) + .chain_move(|pos| parse_type(s, pos, 'd', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, '/')) + .chain_move(|pos| parse_type(s, pos, 'y', &mut *tm)) } 'd' => match match_digits_in_range(s, pos, 2u, false, 1_i32, 31_i32) { @@ -475,10 +475,10 @@ fn do_strptime(s: &str, format: &str) -> Result { } 'F' => { parse_type(s, pos, 'Y', &mut *tm) - .chain(|pos| parse_char(s, pos, '-')) - .chain(|pos| parse_type(s, pos, 'm', &mut *tm)) - .chain(|pos| parse_char(s, pos, '-')) - .chain(|pos| parse_type(s, pos, 'd', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, '-')) + .chain_move(|pos| parse_type(s, pos, 'm', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, '-')) + .chain_move(|pos| parse_type(s, pos, 'd', &mut *tm)) } 'H' => { match match_digits_in_range(s, pos, 2u, false, 0_i32, 23_i32) { @@ -553,17 +553,17 @@ fn do_strptime(s: &str, format: &str) -> Result { }, 'R' => { parse_type(s, pos, 'H', &mut *tm) - .chain(|pos| parse_char(s, pos, ':')) - .chain(|pos| parse_type(s, pos, 'M', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, ':')) + .chain_move(|pos| parse_type(s, pos, 'M', &mut *tm)) } 'r' => { parse_type(s, pos, 'I', &mut *tm) - .chain(|pos| parse_char(s, pos, ':')) - .chain(|pos| parse_type(s, pos, 'M', &mut *tm)) - .chain(|pos| parse_char(s, pos, ':')) - .chain(|pos| parse_type(s, pos, 'S', &mut *tm)) - .chain(|pos| parse_char(s, pos, ' ')) - .chain(|pos| parse_type(s, pos, 'p', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, ':')) + .chain_move(|pos| parse_type(s, pos, 'M', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, ':')) + .chain_move(|pos| parse_type(s, pos, 'S', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, ' ')) + .chain_move(|pos| parse_type(s, pos, 'p', &mut *tm)) } 'S' => { match match_digits_in_range(s, pos, 2u, false, 0_i32, 60_i32) { @@ -578,10 +578,10 @@ fn do_strptime(s: &str, format: &str) -> Result { //'s' {} 'T' | 'X' => { parse_type(s, pos, 'H', &mut *tm) - .chain(|pos| parse_char(s, pos, ':')) - .chain(|pos| parse_type(s, pos, 'M', &mut *tm)) - .chain(|pos| parse_char(s, pos, ':')) - .chain(|pos| parse_type(s, pos, 'S', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, ':')) + .chain_move(|pos| parse_type(s, pos, 'M', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, ':')) + .chain_move(|pos| parse_type(s, pos, 'S', &mut *tm)) } 't' => parse_char(s, pos, '\t'), 'u' => { @@ -596,10 +596,10 @@ fn do_strptime(s: &str, format: &str) -> Result { } 'v' => { parse_type(s, pos, 'e', &mut *tm) - .chain(|pos| parse_char(s, pos, '-')) - .chain(|pos| parse_type(s, pos, 'b', &mut *tm)) - .chain(|pos| parse_char(s, pos, '-')) - .chain(|pos| parse_type(s, pos, 'Y', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, '-')) + .chain_move(|pos| parse_type(s, pos, 'b', &mut *tm)) + .chain_move(|pos| parse_char(s, pos, '-')) + .chain_move(|pos| parse_type(s, pos, 'Y', &mut *tm)) } //'W' {} 'w' => { diff --git a/src/librustc/middle/typeck/infer/combine.rs b/src/librustc/middle/typeck/infer/combine.rs index 9fc29250ed052..0605ba3025488 100644 --- a/src/librustc/middle/typeck/infer/combine.rs +++ b/src/librustc/middle/typeck/infer/combine.rs @@ -108,7 +108,7 @@ pub trait Combine { (Some(a), Some(b)) => { // FIXME(#5781) this should be eq_tys // eq_tys(self, a, b).then(|| Ok(Some(a)) ) - self.contratys(a, b).chain(|t| Ok(Some(t))) + self.contratys(a, b).chain_move(|t| Ok(Some(t))) } (None, Some(_)) | (Some(_), None) => { @@ -162,13 +162,13 @@ pub trait Combine { } ty::rv_covariant => { - do this.regions(a_r, b_r).chain |r| { + do this.regions(a_r, b_r).chain_move |r| { Ok(ty::NonerasedRegions(opt_vec::with(r))) } } ty::rv_contravariant => { - do this.contraregions(a_r, b_r).chain |r| { + do this.contraregions(a_r, b_r).chain_move |r| { Ok(ty::NonerasedRegions(opt_vec::with(r))) } } @@ -179,12 +179,12 @@ pub trait Combine { } } - do self.tps(as_.tps, bs.tps).chain |tps| { - do self.self_tys(as_.self_ty, bs.self_ty).chain |self_ty| { + do self.tps(as_.tps, bs.tps).chain_move |tps| { + do self.self_tys(as_.self_ty, bs.self_ty).chain_move |self_ty| { do relate_region_params(self, generics, &as_.regions, - &bs.regions).chain |regions| { + &bs.regions).chain_move |regions| { Ok(substs { regions: regions, self_ty: self_ty, @@ -227,8 +227,8 @@ pub trait Combine { fn flds(&self, a: ty::field, b: ty::field) -> cres { if a.ident == b.ident { self.mts(&a.mt, &b.mt) - .chain(|mt| Ok(ty::field {ident: a.ident, mt: mt}) ) - .chain_err(|e| Err(ty::terr_in_field(@e, a.ident)) ) + .chain_move(|mt| Ok(ty::field {ident: a.ident, mt: mt}) ) + .chain_err_move(|e| Err(ty::terr_in_field(@e, a.ident)) ) } else { Err(ty::terr_record_fields( expected_found(self, @@ -238,7 +238,7 @@ pub trait Combine { } fn args(&self, a: ty::t, b: ty::t) -> cres { - do self.contratys(a, b).chain |t| { + do self.contratys(a, b).chain_move |t| { Ok(t) } } @@ -274,7 +274,7 @@ pub trait Combine { match (a, b) { (ty::vstore_slice(a_r), ty::vstore_slice(b_r)) => { - do self.contraregions(a_r, b_r).chain |r| { + do self.contraregions(a_r, b_r).chain_move |r| { Ok(ty::vstore_slice(r)) } } @@ -299,7 +299,7 @@ pub trait Combine { match (a, b) { (ty::RegionTraitStore(a_r), ty::RegionTraitStore(b_r)) => { - do self.contraregions(a_r, b_r).chain |r| { + do self.contraregions(a_r, b_r).chain_move |r| { Ok(ty::RegionTraitStore(r)) } } @@ -357,7 +357,7 @@ pub fn expected_found( pub fn eq_tys(this: &C, a: ty::t, b: ty::t) -> ures { let suber = this.sub(); do this.infcx().try { - do suber.tys(a, b).chain |_ok| { + do suber.tys(a, b).chain_move |_ok| { suber.contratys(a, b) }.to_ures() } @@ -371,10 +371,10 @@ pub fn eq_regions(this: &C, a: ty::Region, b: ty::Region) let sub = this.sub(); do indent { this.infcx().try(|| { - do sub.regions(a, b).chain |_r| { + do sub.regions(a, b).chain_move |_r| { sub.contraregions(a, b) } - }).chain_err(|e| { + }).chain_err_move(|e| { // substitute a better error, but use the regions // found in the original error match e { @@ -426,9 +426,8 @@ pub fn super_fn_sigs( } } - do argvecs(this, a.inputs, b.inputs) - .chain |inputs| { - do this.tys(a.output, b.output).chain |output| { + do argvecs(this, a.inputs, b.inputs).chain_move |inputs| { + do this.tys(a.output, b.output).chain_move |output| { Ok(FnSig {bound_lifetime_names: opt_vec::Empty, // FIXME(#4846) inputs: inputs.clone(), output: output}) @@ -508,7 +507,7 @@ pub fn super_tys( &ty::ty_enum(b_id, ref b_substs)) if a_id == b_id => { let type_def = ty::lookup_item_type(tcx, a_id); - do this.substs(&type_def.generics, a_substs, b_substs).chain |substs| { + do this.substs(&type_def.generics, a_substs, b_substs).chain_move |substs| { Ok(ty::mk_enum(tcx, a_id, substs)) } } @@ -517,9 +516,9 @@ pub fn super_tys( &ty::ty_trait(b_id, ref b_substs, b_store, b_mutbl, b_bounds)) if a_id == b_id && a_mutbl == b_mutbl => { let trait_def = ty::lookup_trait_def(tcx, a_id); - do this.substs(&trait_def.generics, a_substs, b_substs).chain |substs| { - do this.trait_stores(ty::terr_trait, a_store, b_store).chain |s| { - do this.bounds(a_bounds, b_bounds).chain |bounds| { + do this.substs(&trait_def.generics, a_substs, b_substs).chain_move |substs| { + do this.trait_stores(ty::terr_trait, a_store, b_store).chain_move |s| { + do this.bounds(a_bounds, b_bounds).chain_move |bounds| { Ok(ty::mk_trait(tcx, a_id, substs.clone(), @@ -534,25 +533,25 @@ pub fn super_tys( (&ty::ty_struct(a_id, ref a_substs), &ty::ty_struct(b_id, ref b_substs)) if a_id == b_id => { let type_def = ty::lookup_item_type(tcx, a_id); - do this.substs(&type_def.generics, a_substs, b_substs).chain |substs| { + do this.substs(&type_def.generics, a_substs, b_substs).chain_move |substs| { Ok(ty::mk_struct(tcx, a_id, substs)) } } (&ty::ty_box(ref a_mt), &ty::ty_box(ref b_mt)) => { - do this.mts(a_mt, b_mt).chain |mt| { + do this.mts(a_mt, b_mt).chain_move |mt| { Ok(ty::mk_box(tcx, mt)) } } (&ty::ty_uniq(ref a_mt), &ty::ty_uniq(ref b_mt)) => { - do this.mts(a_mt, b_mt).chain |mt| { + do this.mts(a_mt, b_mt).chain_move |mt| { Ok(ty::mk_uniq(tcx, mt)) } } (&ty::ty_ptr(ref a_mt), &ty::ty_ptr(ref b_mt)) => { - do this.mts(a_mt, b_mt).chain |mt| { + do this.mts(a_mt, b_mt).chain_move |mt| { Ok(ty::mk_ptr(tcx, mt)) } } @@ -564,15 +563,15 @@ pub fn super_tys( } (&ty::ty_evec(ref a_mt, vs_a), &ty::ty_evec(ref b_mt, vs_b)) => { - do this.mts(a_mt, b_mt).chain |mt| { - do this.vstores(ty::terr_vec, vs_a, vs_b).chain |vs| { + do this.mts(a_mt, b_mt).chain_move |mt| { + do this.vstores(ty::terr_vec, vs_a, vs_b).chain_move |vs| { Ok(ty::mk_evec(tcx, mt, vs)) } } } (&ty::ty_estr(vs_a), &ty::ty_estr(vs_b)) => { - do this.vstores(ty::terr_str, vs_a, vs_b).chain |vs| { + do this.vstores(ty::terr_str, vs_a, vs_b).chain_move |vs| { Ok(ty::mk_estr(tcx,vs)) } } @@ -581,7 +580,7 @@ pub fn super_tys( if as_.len() == bs.len() { result::collect(as_.iter().zip(bs.iter()) .map(|(a, b)| this.tys(*a, *b))) - .chain(|ts| Ok(ty::mk_tup(tcx, ts)) ) + .chain_move(|ts| Ok(ty::mk_tup(tcx, ts)) ) } else { Err(ty::terr_tuple_size( expected_found(this, as_.len(), bs.len()))) @@ -589,13 +588,13 @@ pub fn super_tys( } (&ty::ty_bare_fn(ref a_fty), &ty::ty_bare_fn(ref b_fty)) => { - do this.bare_fn_tys(a_fty, b_fty).chain |fty| { + do this.bare_fn_tys(a_fty, b_fty).chain_move |fty| { Ok(ty::mk_bare_fn(tcx, fty)) } } (&ty::ty_closure(ref a_fty), &ty::ty_closure(ref b_fty)) => { - do this.closure_tys(a_fty, b_fty).chain |fty| { + do this.closure_tys(a_fty, b_fty).chain_move |fty| { Ok(ty::mk_closure(tcx, fty)) } } diff --git a/src/librustc/middle/typeck/infer/glb.rs b/src/librustc/middle/typeck/infer/glb.rs index 1939987d7f078..df9e7d201c99b 100644 --- a/src/librustc/middle/typeck/infer/glb.rs +++ b/src/librustc/middle/typeck/infer/glb.rs @@ -61,7 +61,7 @@ impl Combine for Glb { // If one side or both is immutable, we can use the GLB of // both sides but mutbl must be `MutImmutable`. (MutImmutable, MutImmutable) => { - self.tys(a.ty, b.ty).chain(|t| { + self.tys(a.ty, b.ty).chain_move(|t| { Ok(ty::mt {ty: t, mutbl: MutImmutable}) }) } diff --git a/src/librustc/middle/typeck/infer/lattice.rs b/src/librustc/middle/typeck/infer/lattice.rs index 0ed30bb61b9f3..e926d5196e8b8 100644 --- a/src/librustc/middle/typeck/infer/lattice.rs +++ b/src/librustc/middle/typeck/infer/lattice.rs @@ -232,7 +232,7 @@ impl CombineFieldsLatticeMethods for CombineFields { (&Some(_), &None) => Ok((*a).clone()), (&None, &Some(_)) => Ok((*b).clone()), (&Some(ref v_a), &Some(ref v_b)) => { - do lattice_op(self, v_a, v_b).chain |v| { + do lattice_op(self, v_a, v_b).chain_move |v| { Ok(Some(v)) } } diff --git a/src/librustc/middle/typeck/infer/lub.rs b/src/librustc/middle/typeck/infer/lub.rs index db5873f09fe2c..6ad3d71336b62 100644 --- a/src/librustc/middle/typeck/infer/lub.rs +++ b/src/librustc/middle/typeck/infer/lub.rs @@ -62,7 +62,7 @@ impl Combine for Lub { let m = a.mutbl; match m { MutImmutable => { - self.tys(a.ty, b.ty).chain(|t| Ok(ty::mt {ty: t, mutbl: m}) ) + self.tys(a.ty, b.ty).chain_move(|t| Ok(ty::mt {ty: t, mutbl: m}) ) } MutMutable => { @@ -70,7 +70,7 @@ impl Combine for Lub { eq_tys(self, a.ty, b.ty).then(|| { Ok(ty::mt {ty: a.ty, mutbl: m}) }) - }).chain_err(|e| Err(e)) + }).chain_err_move(|e| Err(e)) } } } diff --git a/src/librustc/middle/typeck/infer/mod.rs b/src/librustc/middle/typeck/infer/mod.rs index a2d2cd92e135a..0db5d02b13c4f 100644 --- a/src/librustc/middle/typeck/infer/mod.rs +++ b/src/librustc/middle/typeck/infer/mod.rs @@ -451,7 +451,7 @@ trait then { impl then for ures { fn then(&self, f: &fn() -> Result) -> Result { - self.chain(|_i| f()) + self.chain_move(|_i| f()) } } @@ -474,7 +474,7 @@ trait CresCompare { impl CresCompare for cres { fn compare(&self, t: T, f: &fn() -> ty::type_err) -> cres { - do (*self).clone().chain |s| { + do (*self).clone().chain_move |s| { if s == t { (*self).clone() } else { diff --git a/src/librustc/middle/typeck/rscope.rs b/src/librustc/middle/typeck/rscope.rs index d3dc3d1fb1c9a..6832fd3048c1f 100644 --- a/src/librustc/middle/typeck/rscope.rs +++ b/src/librustc/middle/typeck/rscope.rs @@ -202,7 +202,7 @@ impl RegionScope for MethodRscope { if !self.region_param_names.has_ident(id) { return RegionParamNames::undeclared_name(None); } - do EmptyRscope.named_region(span, id).chain_err |_e| { + do EmptyRscope.named_region(span, id).chain_err_move |_e| { result::Err(RegionError { msg: ~"lifetime is not in scope", replacement: ty::re_bound(ty::br_self) @@ -251,7 +251,7 @@ impl RegionScope for TypeRscope { } fn named_region(&self, span: Span, id: ast::Ident) -> Result { - do EmptyRscope.named_region(span, id).chain_err |_e| { + do EmptyRscope.named_region(span, id).chain_err_move |_e| { result::Err(RegionError { msg: ~"only 'self is allowed as part of a type declaration", replacement: self.replacement() @@ -310,7 +310,7 @@ impl RegionScope for BindingRscope { span: Span, id: ast::Ident) -> Result { - do self.base.named_region(span, id).chain_err |_e| { + do self.base.named_region(span, id).chain_err_move |_e| { let result = ty::re_bound(ty::br_named(id)); if self.region_param_names.has_ident(id) { result::Ok(result) diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index c8dd38394c96e..d2a7fb1ca0d2a 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -138,7 +138,7 @@ fn config_from_opts( let config = default_config(input_crate); let result = result::Ok(config); - let result = do result.chain |config| { + let result = do result.chain_move |config| { let output_dir = getopts::opt_maybe_str(matches, opt_output_dir()); let output_dir = output_dir.map_move(|s| Path(s)); result::Ok(Config { @@ -146,10 +146,10 @@ fn config_from_opts( .. config }) }; - let result = do result.chain |config| { + let result = do result.chain_move |config| { let output_format = getopts::opt_maybe_str(matches, opt_output_format()); do output_format.map_move_default(result::Ok(config.clone())) |output_format| { - do parse_output_format(output_format).chain |output_format| { + do parse_output_format(output_format).chain_move |output_format| { result::Ok(Config { output_format: output_format, .. config.clone() @@ -157,11 +157,11 @@ fn config_from_opts( } } }; - let result = do result.chain |config| { + let result = do result.chain_move |config| { let output_style = getopts::opt_maybe_str(matches, opt_output_style()); do output_style.map_move_default(result::Ok(config.clone())) |output_style| { - do parse_output_style(output_style).chain |output_style| { + do parse_output_style(output_style).chain_move |output_style| { result::Ok(Config { output_style: output_style, .. config.clone() @@ -170,11 +170,11 @@ fn config_from_opts( } }; let process_output = Cell::new(process_output); - let result = do result.chain |config| { + let result = do result.chain_move |config| { let pandoc_cmd = getopts::opt_maybe_str(matches, opt_pandoc_cmd()); let pandoc_cmd = maybe_find_pandoc( &config, pandoc_cmd, process_output.take()); - do pandoc_cmd.chain |pandoc_cmd| { + do pandoc_cmd.chain_move |pandoc_cmd| { result::Ok(Config { pandoc_cmd: pandoc_cmd, .. config.clone() diff --git a/src/libstd/io.rs b/src/libstd/io.rs index e9b704c2686fb..a67911fcc5564 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -1618,7 +1618,7 @@ impl WriterUtil for T { } pub fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str> { - mk_file_writer(path, flags).chain(|w| Ok(w)) + mk_file_writer(path, flags).chain_move(|w| Ok(w)) } @@ -1779,7 +1779,7 @@ pub fn seek_in_buf(offset: int, pos: uint, len: uint, whence: SeekStyle) -> } pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> { - do read_whole_file(file).chain |bytes| { + do read_whole_file(file).chain_move |bytes| { if str::is_utf8(bytes) { Ok(str::from_utf8(bytes)) } else { diff --git a/src/libstd/result.rs b/src/libstd/result.rs index a06381849bf13..7f18b29351f5c 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -184,7 +184,7 @@ impl Result { /// Ok(parse_bytes(buf)) /// }; #[inline] - pub fn chain(self, op: &fn(T) -> Result) -> Result { + pub fn chain_move(self, op: &fn(T) -> Result) -> Result { match self { Ok(t) => op(t), Err(e) => Err(e), @@ -198,7 +198,7 @@ impl Result { /// immediately returned. This function can be used to pass through a /// successful result while handling an error. #[inline] - pub fn chain_err(self, op: &fn(E) -> Result) -> Result { + pub fn chain_err_move(self, op: &fn(E) -> Result) -> Result { match self { Ok(t) => Ok(t), Err(e) => op(e), @@ -220,6 +220,20 @@ impl Result { Err(ref e) => Err(op(e)) } } + + /// Call a function based on a previous result + /// + /// If `self` is `Err` then the value is extracted and passed to `op` + /// whereupon `op`s result is returned. if `self` is `Ok` then it is + /// immediately returned. This function can be used to pass through a + /// successful result while handling an error. + #[inline] + pub fn chain_err(&self, op: &fn(&E) -> Result) -> Result { + match *self { + Ok(ref t) => Ok(t.clone()), + Err(ref e) => op(e), + } + } } impl Result { @@ -242,6 +256,21 @@ impl Result { Err(ref e) => Err(e.clone()) } } + + /// Call a function based on a previous result + /// + /// If `self` is `Err` then the value is extracted and passed to `op` + /// whereupon `op`s result is returned. if `self` is `Ok` then it is + /// immediately returned. This function can be used to pass through a + /// successful result while handling an error. + #[inline] + pub fn chain(&self, op: &fn(&T) -> Result) -> Result { + match *self { + Ok(ref t) => op(t), + Err(ref e) => Err(e.clone()), + } + } + } /// A generic trait for converting a value to a `Result` @@ -394,21 +423,30 @@ mod tests { use vec::ImmutableVector; pub fn op1() -> Result { Ok(666) } + pub fn op2() -> Result { Err(~"sadface") } - pub fn op2(i: int) -> Result { - Ok(i as uint + 1u) + #[test] + pub fn chain() { + assert_eq!(op1().chain(|i| Ok(*i as uint + 1u)).unwrap(), 667u); + assert_eq!(op2().chain(|i| Ok(*i as uint + 1u)).unwrap_err(), ~"sadface"); } - pub fn op3() -> Result { Err(~"sadface") } + #[test] + pub fn chain_err() { + assert_eq!(op1().chain_err(|e| Err(e.to_str() + "!")).unwrap(), 666); + assert_eq!(op2().chain_err(|e| Err(e.to_str() + "!")).unwrap_err(), ~"sadface!"); + } #[test] - pub fn chain_success() { - assert_eq!(op1().chain(op2).unwrap(), 667u); + pub fn chain_move() { + assert_eq!(op1().chain_move(|i| Ok(i as uint + 1u)).unwrap(), 667u); + assert_eq!(op2().chain_move(|i| Ok(i as uint + 1u)).unwrap_err(), ~"sadface"); } #[test] - pub fn chain_failure() { - assert_eq!(op3().chain( op2).unwrap_err(), ~"sadface"); + pub fn chain_err_move() { + assert_eq!(op1().chain_err_move(|e| Err(e + "!")).unwrap(), 666); + assert_eq!(op2().chain_err_move(|e| Err(e + "!")).unwrap_err(), ~"sadface!"); } #[test]