diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-09-25 16:23:04 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-09-25 17:48:22 -0700 |
| commit | d05e2ad66c7bb2418b7c746f87486d4f74180193 (patch) | |
| tree | 1ecb6f12685ddbf53cdf671daffcc6fbeb0d291e /src/libcore | |
| parent | 62649f0412630f8bfe284996c82b6a723d2ffea9 (diff) | |
Demode core::result
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/comm.rs | 2 | ||||
| -rw-r--r-- | src/libcore/io.rs | 6 | ||||
| -rw-r--r-- | src/libcore/private.rs | 2 | ||||
| -rw-r--r-- | src/libcore/result.rs | 116 |
4 files changed, 67 insertions, 59 deletions
diff --git a/src/libcore/comm.rs b/src/libcore/comm.rs index b99eec8bb5b..a32d7af2ac6 100644 --- a/src/libcore/comm.rs +++ b/src/libcore/comm.rs @@ -459,7 +459,7 @@ fn test_recv_chan_wrong_task() { let po = Port(); let ch = Chan(po); send(ch, ~"flower"); - assert result::is_err(task::try(|| + assert result::is_err(&task::try(|| recv_chan(ch) )) } diff --git a/src/libcore/io.rs b/src/libcore/io.rs index 7c08e508d51..9a2a177c196 100644 --- a/src/libcore/io.rs +++ b/src/libcore/io.rs @@ -645,7 +645,7 @@ impl<T: Writer> T : WriterUtil { #[allow(non_implicitly_copyable_typarams)] fn file_writer(path: &Path, flags: ~[FileFlag]) -> Result<Writer, ~str> { - result::chain(mk_file_writer(path, flags), |w| result::Ok(w)) + mk_file_writer(path, flags).chain(|w| result::Ok(w)) } @@ -864,10 +864,10 @@ mod tests { { let out: io::Writer = result::get( - io::file_writer(tmpfile, ~[io::Create, io::Truncate])); + &io::file_writer(tmpfile, ~[io::Create, io::Truncate])); out.write_str(frood); } - let inp: io::Reader = result::get(io::file_reader(tmpfile)); + let inp: io::Reader = result::get(&io::file_reader(tmpfile)); let frood2: ~str = inp.read_c_str(); log(debug, frood2); assert frood == frood2; diff --git a/src/libcore/private.rs b/src/libcore/private.rs index 777aea7320c..9fe05723d35 100644 --- a/src/libcore/private.rs +++ b/src/libcore/private.rs @@ -283,7 +283,7 @@ fn test_weaken_task_fail() { } } }; - assert result::is_err(res); + assert result::is_err(&res); } /**************************************************************************** diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 205d375e9db..3735d555e2d 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -1,5 +1,9 @@ //! A type representing either success or failure +// NB: transitionary, de-mode-ing. +#[forbid(deprecated_mode)]; +#[forbid(deprecated_pattern)]; + use cmp::Eq; use either::Either; @@ -18,8 +22,8 @@ enum Result<T, U> { * * If the result is an error */ -pure fn get<T: Copy, U>(res: Result<T, U>) -> T { - match res { +pure fn get<T: Copy, U>(res: &Result<T, U>) -> T { + match *res { Ok(t) => t, Err(the_err) => unsafe { fail fmt!("get called on error result: %?", the_err) @@ -50,23 +54,23 @@ pure fn get_ref<T, U>(res: &a/Result<T, U>) -> &a/T { * * If the result is not an error */ -pure fn get_err<T, U: Copy>(res: Result<T, U>) -> U { - match res { +pure fn get_err<T, U: Copy>(res: &Result<T, U>) -> U { + match *res { Err(u) => u, Ok(_) => fail ~"get_err called on ok result" } } /// Returns true if the result is `ok` -pure fn is_ok<T, U>(res: Result<T, U>) -> bool { - match res { +pure fn is_ok<T, U>(res: &Result<T, U>) -> bool { + match *res { Ok(_) => true, Err(_) => false } } /// Returns true if the result is `err` -pure fn is_err<T, U>(res: Result<T, U>) -> bool { +pure fn is_err<T, U>(res: &Result<T, U>) -> bool { !is_ok(res) } @@ -76,8 +80,8 @@ pure fn is_err<T, U>(res: Result<T, U>) -> bool { * `ok` result variants are converted to `either::right` variants, `err` * result variants are converted to `either::left`. */ -pure fn to_either<T: Copy, U: Copy>(res: Result<U, T>) -> Either<T, U> { - match res { +pure fn to_either<T: Copy, U: Copy>(res: &Result<U, T>) -> Either<T, U> { + match *res { Ok(res) => either::Right(res), Err(fail_) => either::Left(fail_) } @@ -97,11 +101,13 @@ pure fn to_either<T: Copy, U: Copy>(res: Result<U, T>) -> Either<T, U> { * ok(parse_bytes(buf)) * } */ -fn chain<T, U: Copy, V: Copy>(res: Result<T, V>, op: fn(T) -> Result<U, V>) +fn chain<T, U: Copy, V: Copy>(+res: Result<T, V>, op: fn(+t: T) -> Result<U, V>) -> Result<U, V> { - match res { - Ok(t) => op(t), - Err(e) => Err(e) + // XXX: Should be writable with move + match + if res.is_ok() { + op(unwrap(res)) + } else { + Err(unwrap_err(res)) } } @@ -114,10 +120,10 @@ fn chain<T, U: Copy, V: Copy>(res: Result<T, V>, op: fn(T) -> Result<U, V>) * successful result while handling an error. */ fn chain_err<T: Copy, U: Copy, V: Copy>( - res: Result<T, V>, - op: fn(V) -> Result<T, U>) + +res: Result<T, V>, + op: fn(+t: V) -> Result<T, U>) -> Result<T, U> { - match res { + move match res { Ok(t) => Ok(t), Err(v) => op(v) } @@ -137,9 +143,9 @@ fn chain_err<T: Copy, U: Copy, V: Copy>( * print_buf(buf) * } */ -fn iter<T, E>(res: Result<T, E>, f: fn(T)) { - match res { - Ok(t) => f(t), +fn iter<T, E>(res: &Result<T, E>, f: fn((&T))) { + match *res { + Ok(t) => f(&t), Err(_) => () } } @@ -152,10 +158,10 @@ fn iter<T, E>(res: Result<T, E>, f: fn(T)) { * This function can be used to pass through a successful result while * handling an error. */ -fn iter_err<T, E>(res: Result<T, E>, f: fn(E)) { - match res { +fn iter_err<T, E>(res: &Result<T, E>, f: fn((&E))) { + match *res { Ok(_) => (), - Err(e) => f(e) + Err(e) => f(&e) } } @@ -173,10 +179,10 @@ fn iter_err<T, E>(res: Result<T, E>, f: fn(E)) { * parse_bytes(buf) * } */ -fn map<T, E: Copy, U: Copy>(res: Result<T, E>, op: fn(T) -> U) +fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn((&T)) -> U) -> Result<U, E> { - match res { - Ok(t) => Ok(op(t)), + match *res { + Ok(t) => Ok(op(&t)), Err(e) => Err(e) } } @@ -189,63 +195,65 @@ fn map<T, E: Copy, U: Copy>(res: Result<T, E>, op: fn(T) -> U) * is immediately returned. This function can be used to pass through a * successful result while handling an error. */ -fn map_err<T: Copy, E, F: Copy>(res: Result<T, E>, op: fn(E) -> F) +fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn((&E)) -> F) -> Result<T, F> { - match res { + match *res { Ok(t) => Ok(t), - Err(e) => Err(op(e)) + Err(e) => Err(op(&e)) } } impl<T, E> Result<T, E> { - fn is_ok() -> bool { is_ok(self) } + fn is_ok() -> bool { is_ok(&self) } - fn is_err() -> bool { is_err(self) } + fn is_err() -> bool { is_err(&self) } - fn iter(f: fn(T)) { + fn iter(f: fn((&T))) { match self { - Ok(t) => f(t), + Ok(t) => f(&t), Err(_) => () } } - fn iter_err(f: fn(E)) { + fn iter_err(f: fn((&E))) { match self { Ok(_) => (), - Err(e) => f(e) + Err(e) => f(&e) } } } impl<T: Copy, E> Result<T, E> { - fn get() -> T { get(self) } + fn get() -> T { get(&self) } - fn map_err<F:Copy>(op: fn(E) -> F) -> Result<T,F> { + fn map_err<F:Copy>(op: fn((&E)) -> F) -> Result<T,F> { match self { Ok(t) => Ok(t), - Err(e) => Err(op(e)) + Err(e) => Err(op(&e)) } } } impl<T, E: Copy> Result<T, E> { - fn get_err() -> E { get_err(self) } + fn get_err() -> E { get_err(&self) } - fn map<U:Copy>(op: fn(T) -> U) -> Result<U,E> { + fn map<U:Copy>(op: fn((&T)) -> U) -> Result<U,E> { match self { - Ok(t) => Ok(op(t)), + Ok(t) => Ok(op(&t)), Err(e) => Err(e) } } } impl<T: Copy, E: Copy> Result<T, E> { - fn chain<U:Copy>(op: fn(T) -> Result<U,E>) -> Result<U,E> { - chain(self, op) + fn chain<U:Copy>(op: fn(+t: T) -> Result<U,E>) -> Result<U,E> { + // XXX: Bad copy + chain(copy self, op) } - fn chain_err<F:Copy>(op: fn(E) -> Result<T,F>) -> Result<T,F> { - chain_err(self, op) + fn chain_err<F:Copy>(op: fn(+t: E) -> Result<T,F>) -> Result<T,F> { + // XXX: Bad copy + chain_err(copy self, op) } } @@ -280,11 +288,11 @@ fn map_vec<T,U:Copy,V:Copy>( } fn map_opt<T,U:Copy,V:Copy>( - o_t: Option<T>, op: fn(T) -> Result<V,U>) -> Result<Option<V>,U> { + o_t: &Option<T>, op: fn((&T)) -> Result<V,U>) -> Result<Option<V>,U> { - match o_t { + match *o_t { None => Ok(None), - Some(t) => match op(t) { + Some(t) => match op(&t) { Ok(v) => Ok(Some(v)), Err(e) => Err(e) } @@ -301,14 +309,14 @@ fn map_opt<T,U:Copy,V:Copy>( * to accommodate an error like the vectors being of different lengths. */ fn map_vec2<S,T,U:Copy,V:Copy>(ss: &[S], ts: &[T], - op: fn(S,T) -> Result<V,U>) -> Result<~[V],U> { + op: fn((&S),(&T)) -> Result<V,U>) -> Result<~[V],U> { assert vec::same_length(ss, ts); let n = vec::len(ts); let mut vs = vec::with_capacity(n); let mut i = 0u; while i < n { - match op(ss[i],ts[i]) { + match op(&ss[i],&ts[i]) { Ok(v) => vec::push(vs, v), Err(u) => return Err(u) } @@ -323,13 +331,13 @@ fn map_vec2<S,T,U:Copy,V:Copy>(ss: &[S], ts: &[T], * on its own as no result vector is built. */ fn iter_vec2<S,T,U:Copy>(ss: &[S], ts: &[T], - op: fn(S,T) -> Result<(),U>) -> Result<(),U> { + op: fn((&S),(&T)) -> Result<(),U>) -> Result<(),U> { assert vec::same_length(ss, ts); let n = vec::len(ts); let mut i = 0u; while i < n { - match op(ss[i],ts[i]) { + match op(&ss[i],&ts[i]) { Ok(()) => (), Err(u) => return Err(u) } @@ -380,7 +388,7 @@ mod tests { #[legacy_exports]; fn op1() -> result::Result<int, ~str> { result::Ok(666) } - fn op2(&&i: int) -> result::Result<uint, ~str> { + fn op2(+i: int) -> result::Result<uint, ~str> { result::Ok(i as uint + 1u) } @@ -388,12 +396,12 @@ mod tests { #[test] fn chain_success() { - assert get(chain(op1(), op2)) == 667u; + assert get(&chain(op1(), op2)) == 667u; } #[test] fn chain_failure() { - assert get_err(chain(op3(), op2)) == ~"sadface"; + assert get_err(&chain(op3(), op2)) == ~"sadface"; } #[test] |
