Skip to content

Cleanup std::result, part 2 #8069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
19 changes: 9 additions & 10 deletions src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1867,35 +1867,34 @@ mod tests {
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\": [" +
"true," +
"\"foo\\nbar\", " +
"{ \"c\": {\"d\": null} } " +
"]" +
"}")),
"}").unwrap(),
mk_object([
(~"a", Number(1.0f)),
(~"b", List(~[
Expand Down
8 changes: 4 additions & 4 deletions src/libextra/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,13 +1132,13 @@ mod tests {
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("%", "%%"));

Expand Down
3 changes: 1 addition & 2 deletions src/libextra/workcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::cell::Cell;
use std::comm::{PortOne, oneshot, send_one, recv_one};
use std::either::{Either, Left, Right};
use std::io;
use std::result;
use std::run;
use std::task;

Expand Down Expand Up @@ -208,7 +207,7 @@ fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str {
// FIXME(#5121)
fn json_decode<T:Decodable<json::Decoder>>(s: &str) -> T {
do io::with_str_reader(s) |rdr| {
let j = result::unwrap(json::from_reader(rdr));
let j = json::from_reader(rdr).unwrap();
let mut decoder = json::Decoder(j);
Decodable::decode(&mut decoder)
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/metadata/filesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn get_rustpkg_root() -> Result<Path, ~str> {
}

pub fn get_rustpkg_root_nearest() -> Result<Path, ~str> {
do result::chain(get_rustpkg_root()) |p| {
do get_rustpkg_root().chain |p| {
let cwd = os::getcwd();
let cwd_rustpkg = cwd.push(".rustpkg");
let rustpkg_is_non_root_file =
Expand All @@ -173,13 +173,13 @@ pub fn get_rustpkg_root_nearest() -> Result<Path, ~str> {
}

fn get_rustpkg_lib_path() -> Result<Path, ~str> {
do result::chain(get_rustpkg_root()) |p| {
do get_rustpkg_root().chain |p| {
result::Ok(p.push(libdir()))
}
}

fn get_rustpkg_lib_path_nearest() -> Result<Path, ~str> {
do result::chain(get_rustpkg_root_nearest()) |p| {
do get_rustpkg_root_nearest().chain |p| {
result::Ok(p.push(libdir()))
}
}
Expand Down
26 changes: 10 additions & 16 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,36 +138,30 @@ fn config_from_opts(

let config = default_config(input_crate);
let result = result::Ok(config);
let result = do result::chain(result) |config| {
let result = do result.chain |config| {
let output_dir = getopts::opt_maybe_str(matches, opt_output_dir());
let output_dir = output_dir.map(|s| Path(*s));
result::Ok(Config {
output_dir: output_dir.get_or_default(config.output_dir.clone()),
.. config
})
};
let result = do result::chain(result) |config| {
let output_format = getopts::opt_maybe_str(
matches, opt_output_format());
do output_format.map_default(result::Ok(config.clone()))
|output_format| {
do result::chain(parse_output_format(*output_format))
|output_format| {

let result = do result.chain |config| {
let output_format = getopts::opt_maybe_str(matches, opt_output_format());
do output_format.map_default(result::Ok(config.clone())) |output_format| {
do parse_output_format(*output_format).chain |output_format| {
result::Ok(Config {
output_format: output_format,
.. config.clone()
})
}
}
};
let result = do result::chain(result) |config| {
let result = do result.chain |config| {
let output_style =
getopts::opt_maybe_str(matches, opt_output_style());
do output_style.map_default(result::Ok(config.clone()))
|output_style| {
do result::chain(parse_output_style(*output_style))
|output_style| {
do output_style.map_default(result::Ok(config.clone())) |output_style| {
do parse_output_style(*output_style).chain |output_style| {
result::Ok(Config {
output_style: output_style,
.. config.clone()
Expand All @@ -176,11 +170,11 @@ fn config_from_opts(
}
};
let process_output = Cell::new(process_output);
let result = do result::chain(result) |config| {
let result = do result.chain |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 result::chain(pandoc_cmd) |pandoc_cmd| {
do pandoc_cmd.chain |pandoc_cmd| {
result::Ok(Config {
pandoc_cmd: pandoc_cmd,
.. config.clone()
Expand Down
4 changes: 1 addition & 3 deletions src/librustpkg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
66 changes: 31 additions & 35 deletions src/libstd/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,26 @@ implement `Reader` and `Writer`, where appropriate.

#[allow(missing_doc)];

use result::Result;

use cast;
use clone::Clone;
use container::Container;
use int;
use libc;
use libc::{c_int, c_long, c_void, size_t, ssize_t};
use iterator::IteratorUtil;
use libc::consts::os::posix88::*;
use libc::{c_int, c_long, c_void, size_t, ssize_t};
use libc;
use num;
use ops::Drop;
use os;
use cast;
use path::Path;
use ops::Drop;
use iterator::IteratorUtil;
use ptr;
use result;
use str;
use result::{Result, Ok, Err};
use str::{StrSlice, OwnedStr};
use str;
use to_str::ToStr;
use uint;
use vec;
use vec::{MutableVector, ImmutableVector, OwnedVector, OwnedCopyableVector, CopyableVector};
use vec;

#[allow(non_camel_case_types)] // not sure what to do about this
pub type fd_t = c_int;
Expand Down Expand Up @@ -1038,9 +1036,9 @@ pub fn file_reader(path: &Path) -> Result<@Reader, ~str> {
};

if f as uint == 0u {
result::Err(~"error opening " + path.to_str())
Err(~"error opening " + path.to_str())
} else {
result::Ok(FILE_reader(f, true))
Ok(FILE_reader(f, true))
}
}

Expand Down Expand Up @@ -1287,10 +1285,9 @@ pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
}
};
if fd < (0 as c_int) {
result::Err(fmt!("error opening %s: %s", path.to_str(),
os::last_os_error()))
Err(fmt!("error opening %s: %s", path.to_str(), os::last_os_error()))
} else {
result::Ok(fd_writer(fd, true))
Ok(fd_writer(fd, true))
}
}

Expand Down Expand Up @@ -1559,7 +1556,7 @@ impl<T:Writer> WriterUtil for T {
}

pub fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str> {
mk_file_writer(path, flags).chain(|w| result::Ok(w))
mk_file_writer(path, flags).chain(|w| Ok(w))
}


Expand All @@ -1572,9 +1569,9 @@ pub fn buffered_file_writer(path: &Path) -> Result<@Writer, ~str> {
}
};
return if f as uint == 0u {
result::Err(~"error opening " + path.to_str())
Err(~"error opening " + path.to_str())
} else {
result::Ok(FILE_writer(f, true))
Ok(FILE_writer(f, true))
}
}
}
Expand Down Expand Up @@ -1726,21 +1723,21 @@ pub fn seek_in_buf(offset: int, pos: uint, len: uint, whence: SeekStyle) ->
}

pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> {
result::chain(read_whole_file(file), |bytes| {
do read_whole_file(file).chain |bytes| {
if str::is_utf8(bytes) {
result::Ok(str::from_bytes(bytes))
Ok(str::from_bytes(bytes))
} else {
result::Err(file.to_str() + " is not UTF-8")
Err(file.to_str() + " is not UTF-8")
}
})
}
}

// FIXME (#2004): implement this in a low-level way. Going through the
// abstractions is pointless.
pub fn read_whole_file(file: &Path) -> Result<~[u8], ~str> {
result::chain(file_reader(file), |rdr| {
result::Ok(rdr.read_whole_stream())
})
do file_reader(file).chain |rdr| {
Ok(rdr.read_whole_stream())
}
}

// fsync related
Expand Down Expand Up @@ -1839,6 +1836,7 @@ mod tests {
use io::{BytesWriter, SeekCur, SeekEnd, SeekSet};
use io;
use path::Path;
use result::{Ok, Err};
use result;
use u64;
use vec;
Expand All @@ -1851,12 +1849,10 @@ mod tests {
~"A hoopy frood who really knows where his towel is.";
debug!(frood.clone());
{
let out: @io::Writer =
result::unwrap(
io::file_writer(tmpfile, [io::Create, io::Truncate]));
let out = io::file_writer(tmpfile, [io::Create, io::Truncate]).unwrap();
out.write_str(frood);
}
let inp: @io::Reader = result::unwrap(io::file_reader(tmpfile));
let inp = io::file_reader(tmpfile).unwrap();
let frood2: ~str = inp.read_c_str();
debug!(frood2.clone());
assert_eq!(frood, frood2);
Expand Down Expand Up @@ -1941,10 +1937,10 @@ mod tests {
#[test]
fn file_reader_not_exist() {
match io::file_reader(&Path("not a file")) {
result::Err(e) => {
Err(e) => {
assert_eq!(e, ~"error opening not a file");
}
result::Ok(_) => fail!()
Ok(_) => fail!()
}
}

Expand Down Expand Up @@ -1982,20 +1978,20 @@ mod tests {
#[test]
fn file_writer_bad_name() {
match io::file_writer(&Path("?/?"), []) {
result::Err(e) => {
Err(e) => {
assert!(e.starts_with("error opening"));
}
result::Ok(_) => fail!()
Ok(_) => fail!()
}
}

#[test]
fn buffered_file_writer_bad_name() {
match io::buffered_file_writer(&Path("?/?")) {
result::Err(e) => {
Err(e) => {
assert!(e.starts_with("error opening"));
}
result::Ok(_) => fail!()
Ok(_) => fail!()
}
}

Expand Down
Loading