提交 2a68c719 编写于 作者: E Erick Tryzelaar

to_either + fixes

上级 aad53cb6
......@@ -1867,27 +1867,26 @@ fn test_read_object() {
col: 8u,
msg: @~"EOF while parsing object"}));
assert_eq!(result::unwrap(from_str("{}")), mk_object([]));
assert_eq!(result::unwrap(from_str("{\"a\": 3}")),
assert_eq!(from_str("{}").unwrap(), mk_object([]));
assert_eq!(from_str("{\"a\": 3}").unwrap(),
mk_object([(~"a", Number(3.0f))]));
assert_eq!(result::unwrap(from_str(
"{ \"a\": null, \"b\" : true }")),
assert_eq!(from_str(
"{ \"a\": null, \"b\" : true }").unwrap(),
mk_object([
(~"a", Null),
(~"b", Boolean(true))]));
assert_eq!(result::unwrap(
from_str("\n{ \"a\": null, \"b\" : true }\n")),
assert_eq!(from_str("\n{ \"a\": null, \"b\" : true }\n").unwrap(),
mk_object([
(~"a", Null),
(~"b", Boolean(true))]));
assert_eq!(result::unwrap(from_str(
"{\"a\" : 1.0 ,\"b\": [ true ]}")),
assert_eq!(from_str(
"{\"a\" : 1.0 ,\"b\": [ true ]}").unwrap(),
mk_object([
(~"a", Number(1.0)),
(~"b", List(~[Boolean(true)]))
]));
assert_eq!(result::unwrap(from_str(
assert_eq!(from_str(
~"{" +
"\"a\": 1.0, " +
"\"b\": [" +
......@@ -1895,7 +1894,7 @@ fn test_read_object() {
"\"foo\\nbar\", " +
"{ \"c\": {\"d\": null} } " +
"]" +
"}")),
"}").unwrap(),
mk_object([
(~"a", Number(1.0f)),
(~"b", List(~[
......
......@@ -1132,13 +1132,13 @@ fn test(s: &str, format: &str) -> bool {
assert!(test("6", "%w"));
assert!(test("2009", "%Y"));
assert!(test("09", "%y"));
assert!(result::unwrap(strptime("UTC", "%Z")).tm_zone ==
assert!(strptime("UTC", "%Z").unwrap().tm_zone ==
~"UTC");
assert!(result::unwrap(strptime("PST", "%Z")).tm_zone ==
assert!(strptime("PST", "%Z").unwrap().tm_zone ==
~"");
assert!(result::unwrap(strptime("-0000", "%z")).tm_gmtoff ==
assert!(strptime("-0000", "%z").unwrap().tm_gmtoff ==
0);
assert!(result::unwrap(strptime("-0800", "%z")).tm_gmtoff ==
assert!(strptime("-0800", "%z").unwrap().tm_gmtoff ==
0);
assert!(test("%", "%%"));
......
......@@ -62,9 +62,7 @@ fn git_repo_pkg() -> PkgId {
}
fn writeFile(file_path: &Path, contents: &str) {
let out: @io::Writer =
result::unwrap(io::file_writer(file_path,
[io::Create, io::Truncate]));
let out = io::file_writer(file_path, [io::Create, io::Truncate]).unwrap();
out.write_line(contents);
}
......
......@@ -31,27 +31,21 @@ pub enum Result<T, U> {
Err(U)
}
/**
* Convert to the `either` type
*
* `ok` result variants are converted to `either::right` variants, `err`
* result variants are converted to `either::left`.
*/
#[inline]
pub fn to_either<T:Clone,U:Clone>(res: &Result<U, T>)
-> Either<T, U> {
match *res {
Ok(ref res) => either::Right((*res).clone()),
Err(ref fail_) => either::Left((*fail_).clone())
impl<T, E> Result<T, E> {
/**
* Convert to the `either` type
*
* `ok` result variants are converted to `either::right` variants, `err`
* result variants are converted to `either::left`.
*/
#[inline]
pub fn to_either(self)-> Either<E, T>{
match self {
Ok(t) => either::Right(t),
Err(e) => either::Left(e),
}
}
}
impl<T, E> Result<T, E> {
/**
* Get a reference to the value out of a successful result
*
......@@ -84,7 +78,7 @@ pub fn is_err(&self) -> bool {
}
/**
* Call a function based on a previous result
* Call a method based on a previous result
*
* If `*self` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is returned. if `*self` is `err` then it is immediately
......@@ -106,7 +100,7 @@ pub fn iter(&self, f: &fn(&T)) {
}
/**
* Call a function based on a previous result
* Call a method 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.
......@@ -140,7 +134,7 @@ pub fn unwrap_err(self) -> E {
}
/**
* Call a function based on a previous result
* Call a method based on a previous result
*
* If `self` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is returned. if `self` is `err` then it is immediately
......@@ -149,9 +143,9 @@ pub fn unwrap_err(self) -> E {
*
* Example:
*
* let res = read_file(file).chain(op) { |buf|
* let res = do read_file(file).chain |buf| {
* ok(parse_bytes(buf))
* }
* };
*/
#[inline]
pub fn chain<U>(self, op: &fn(T) -> Result<U,E>) -> Result<U,E> {
......@@ -162,7 +156,7 @@ pub fn chain<U>(self, op: &fn(T) -> Result<U,E>) -> Result<U,E> {
}
/**
* Call a function based on a previous result
* Call a method 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
......@@ -195,13 +189,13 @@ pub fn get(&self) -> T {
}
/**
* 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 wrapped in an `err` and 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.
*/
* Call a method based on a previous result
*
* If `*self` is `err` then the value is extracted and passed to `op` whereupon
* `op`s result is wrapped in an `err` and 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 map_err<F:Clone>(&self, op: &fn(&E) -> F) -> Result<T,F> {
match *self {
......@@ -228,19 +222,19 @@ pub fn get_err(&self) -> E {
}
/**
* Call a function based on a previous result
*
* If `res` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is wrapped in `ok` and returned. if `res` is `err` then it is
* immediately returned. This function can be used to compose the results of
* two functions.
*
* Example:
*
* let res = map(read_file(file)) { |buf|
* parse_bytes(buf)
* }
*/
* Call a method based on a previous result
*
* If `res` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is wrapped in `ok` and returned. if `res` is `err` then it is
* immediately returned. This function can be used to compose the results of
* two functions.
*
* Example:
*
* let res = read_file(file).map() { |buf|
* parse_bytes(buf)
* });
*/
#[inline]
pub fn map<U:Clone>(&self, op: &fn(&T) -> U) -> Result<U,E> {
match *self {
......@@ -351,6 +345,7 @@ pub fn iter_vec2<S,T,U>(ss: &[S], ts: &[T],
mod tests {
use result::{Err, Ok, Result};
use result;
use either;
pub fn op1() -> result::Result<int, ~str> { result::Ok(666) }
......@@ -408,4 +403,13 @@ pub fn test_get_ref_method() {
let foo: Result<int, ()> = Ok(100);
assert_eq!(*foo.get_ref(), 100);
}
#[test]
pub fn test_to_either() {
let r: Result<int, ()> = Ok(100);
let err: Result<(), int> = Err(404);
assert_eq!(r.to_either(), either::Right(100));
assert_eq!(err.to_either(), either::Left(404));
}
}
......@@ -75,7 +75,7 @@ fn read_line() {
.push_rel(&Path("src/test/bench/shootout-k-nucleotide.data"));
for int::range(0, 3) |_i| {
let reader = result::unwrap(io::file_reader(&path));
let reader = io::file_reader(&path).unwrap();
while !reader.eof() {
reader.read_line();
}
......
......@@ -124,8 +124,8 @@ fn main() {
};
let writer = if os::getenv("RUST_BENCH").is_some() {
result::unwrap(io::file_writer(&Path("./shootout-fasta.data"),
[io::Truncate, io::Create]))
io::file_writer(&Path("./shootout-fasta.data"),
[io::Truncate, io::Create]).unwrap()
} else {
io::stdout()
};
......
......@@ -161,7 +161,7 @@ fn main() {
// get to this massive data set, but include_bin! chokes on it (#2598)
let path = Path(env!("CFG_SRC_DIR"))
.push_rel(&Path("src/test/bench/shootout-k-nucleotide.data"));
result::unwrap(io::file_reader(&path))
io::file_reader(&path).unwrap()
} else {
io::stdin()
};
......
......@@ -13,5 +13,5 @@
use std::result;
fn main() {
error!(result::get(&result::Err::<int,~str>(~"kitty")));
error!(result::Err::<int,~str>(~"kitty").get());
}
......@@ -16,7 +16,7 @@
fn adder(x: @int, y: @int) -> int { return *x + *y; }
fn failer() -> @int { fail!(); }
pub fn main() {
assert!(result::is_err(&task::try(|| {
assert!(task::try(|| {
adder(@2, failer()); ()
})));
}).is_err());
}
......@@ -11,14 +11,13 @@
extern mod extra;
use std::result;
use extra::json;
use extra::serialize::Decodable;
trait JD : Decodable<json::Decoder> { }
fn exec<T: JD>() {
let doc = result::unwrap(json::from_str(""));
let doc = json::from_str("").unwrap();
let mut decoder = json::Decoder(doc);
let _v: T = Decodable::decode(&mut decoder);
fail!()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册