Skip to content
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

Remove some of the uses of Either #9180

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/libextra/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use treemap::TreeMap;
use std::clone::Clone;
use std::comm::{stream, SharedChan, GenericPort, GenericChan};
use std::libc;
use std::either;
use std::io;
use std::result;
use std::task;
Expand Down Expand Up @@ -127,8 +126,8 @@ pub type MetricDiff = TreeMap<~str,MetricChange>;
pub fn test_main(args: &[~str], tests: ~[TestDescAndFn]) {
let opts =
match parse_opts(args) {
either::Left(o) => o,
either::Right(m) => fail!(m)
Ok(o) => o,
Err(msg) => fail!(msg)
};
if !run_tests_console(&opts, tests) { fail!("Some tests failed"); }
}
Expand Down Expand Up @@ -169,7 +168,7 @@ pub struct TestOpts {
logfile: Option<Path>
}

type OptRes = Either<TestOpts, ~str>;
type OptRes = Result<TestOpts, ~str>;

fn optgroups() -> ~[getopts::groups::OptGroup] {
~[groups::optflag("", "ignored", "Run ignored tests"),
Expand Down Expand Up @@ -228,7 +227,7 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
let matches =
match groups::getopts(args_, optgroups()) {
Ok(m) => m,
Err(f) => return either::Right(getopts::fail_str(f))
Err(f) => return Err(getopts::fail_str(f))
};

if getopts::opt_present(&matches, "h") { usage(args[0], "h"); }
Expand Down Expand Up @@ -274,7 +273,7 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
logfile: logfile
};

either::Left(test_opts)
Ok(test_opts)
}

pub fn opt_shard(maybestr: Option<~str>) -> Option<(uint,uint)> {
Expand Down Expand Up @@ -1155,7 +1154,6 @@ mod tests {
StaticTestName, DynTestName, DynTestFn};
use test::{TestOpts, run_test};

use std::either;
use std::comm::{stream, SharedChan};
use tempfile;
use std::os;
Expand Down Expand Up @@ -1236,8 +1234,8 @@ mod tests {
fn first_free_arg_should_be_a_filter() {
let args = ~[~"progname", ~"filter"];
let opts = match parse_opts(args) {
either::Left(o) => o,
_ => fail!("Malformed arg in first_free_arg_should_be_a_filter")
Ok(o) => o,
_ => fail!("Malformed arg in first_free_arg_should_be_a_filter")
};
assert!("filter" == opts.filter.clone().unwrap());
}
Expand All @@ -1246,8 +1244,8 @@ mod tests {
fn parse_ignored_flag() {
let args = ~[~"progname", ~"filter", ~"--ignored"];
let opts = match parse_opts(args) {
either::Left(o) => o,
_ => fail!("Malformed arg in parse_ignored_flag")
Ok(o) => o,
_ => fail!("Malformed arg in parse_ignored_flag")
};
assert!((opts.run_ignored));
}
Expand Down
32 changes: 16 additions & 16 deletions src/libextra/workcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use arc::{Arc,RWArc};
use treemap::TreeMap;
use std::cell::Cell;
use std::comm::{PortOne, oneshot};
use std::either::{Either, Left, Right};
use std::{io, os, task};

/**
Expand Down Expand Up @@ -252,9 +251,9 @@ struct Exec {
discovered_outputs: WorkMap
}

struct Work<'self, T> {
prep: &'self Prep<'self>,
res: Option<Either<T,PortOne<(Exec,T)>>>
enum Work<'self, T> {
WorkValue(T),
WorkFromTask(&'self Prep<'self>, PortOne<(Exec, T)>),
}

fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str {
Expand Down Expand Up @@ -426,15 +425,15 @@ impl<'self> Prep<'self> {
db.prepare(self.fn_name, &self.declared_inputs)
};

let res = match cached {
match cached {
Some((ref disc_in, ref disc_out, ref res))
if self.all_fresh("declared input",&self.declared_inputs) &&
self.all_fresh("discovered input", disc_in) &&
self.all_fresh("discovered output", disc_out) => {
debug!("Cache hit!");
debug!("Trying to decode: %? / %? / %?",
disc_in, disc_out, *res);
Left(json_decode(*res))
Work::from_value(json_decode(*res))
}

_ => {
Expand All @@ -453,10 +452,9 @@ impl<'self> Prep<'self> {
let v = blk(&mut exe);
chan.send((exe, v));
}
Right(port)
Work::from_task(self, port)
}
};
Work::new(self, res)
}
}
}

Expand All @@ -465,16 +463,18 @@ impl<'self, T:Send +
Decodable<json::Decoder>>
Work<'self, T> { // FIXME(#5121)

pub fn new(p: &'self Prep<'self>, e: Either<T,PortOne<(Exec,T)>>) -> Work<'self, T> {
Work { prep: p, res: Some(e) }
pub fn from_value(elt: T) -> Work<'self, T> {
WorkValue(elt)
}
pub fn from_task(prep: &'self Prep<'self>, port: PortOne<(Exec, T)>)
-> Work<'self, T> {
WorkFromTask(prep, port)
}

pub fn unwrap(self) -> T {
let Work { prep, res } = self;
match res {
None => fail!(),
Some(Left(v)) => v,
Some(Right(port)) => {
match self {
WorkValue(v) => v,
WorkFromTask(prep, port) => {
let (exe, v) = port.recv();
let s = json_encode(&v);
do prep.ctxt.db.write |db| {
Expand Down
7 changes: 3 additions & 4 deletions src/libstd/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

use option::*;
use os;
use either::*;
use rt;
use rt::logging::{Logger, StdErrLogger};
use rt::logging::{Logger, StdErrLogger, OwnedString};

/// Turns on logging to stdout globally
pub fn console_on() {
Expand Down Expand Up @@ -57,12 +56,12 @@ fn newsched_log_str(msg: ~str) {
match optional_task {
Some(local) => {
// Use the available logger
(*local).logger.log(Left(msg));
(*local).logger.log(OwnedString(msg));
}
None => {
// There is no logger anywhere, just write to stderr
let mut logger = StdErrLogger;
logger.log(Left(msg));
logger.log(OwnedString(msg));
}
}
}
Expand Down
24 changes: 13 additions & 11 deletions src/libstd/rt/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use either::*;
use libc::{uintptr_t, exit, STDERR_FILENO};
use option::{Some, None, Option};
use rt::util::dumb_println;
Expand Down Expand Up @@ -168,29 +167,32 @@ fn update_log_settings(crate_map: *u8, settings: ~str) {
}
}

/// Represent a string with `Send` bound.
pub enum SendableString {
OwnedString(~str),
StaticString(&'static str)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SendableString should probably live somewhere in libstd, as it will probably be useful for task failure too.

Maybe talk with @Aatch about implementing what he started in #7599?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a notice that I started working on a modernized version of #7599.

pub trait Logger {
fn log(&mut self, msg: Either<~str, &'static str>);
fn log(&mut self, msg: SendableString);
}

pub struct StdErrLogger;

impl Logger for StdErrLogger {
fn log(&mut self, msg: Either<~str, &'static str>) {
fn log(&mut self, msg: SendableString) {
use io::{Writer, WriterUtil};

if !should_log_console() {
return;
}

let s: &str = match msg {
Left(ref s) => {
let s: &str = *s;
s
}
Right(ref s) => {
let s: &str = *s;
s
}
OwnedString(ref s) => {
let slc: &str = *s;
slc
},
StaticString(s) => s,
};

// Truncate the string
Expand Down
5 changes: 2 additions & 3 deletions src/libstd/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,11 @@ impl FailWithCause for &'static str {

// FIXME #4427: Temporary until rt::rt_fail_ goes away
pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
use either::Left;
use option::{Some, None};
use rt::in_green_task_context;
use rt::task::Task;
use rt::local::Local;
use rt::logging::Logger;
use rt::logging::{Logger, OwnedString};
use str::Str;

unsafe {
Expand All @@ -164,7 +163,7 @@ pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
msg, file, line as int)
};

task.logger.log(Left(msg));
task.logger.log(OwnedString(msg));
}
} else {
rterrln!("failed in non-task context at '%s', %s:%i",
Expand Down
36 changes: 15 additions & 21 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ use parse::{new_sub_parser_from_file, ParseSess};
use opt_vec;
use opt_vec::OptVec;

use std::either::Either;
use std::either;
use std::hashmap::HashSet;
use std::util;
use std::vec;
Expand All @@ -94,7 +92,6 @@ enum restriction {
RESTRICT_NO_BAR_OR_DOUBLEBAR_OP,
}

type arg_or_capture_item = Either<arg, ()>;
type item_info = (Ident, item_, Option<~[Attribute]>);

/// How to parse a path. There are four different kinds of paths, all of which
Expand Down Expand Up @@ -936,7 +933,7 @@ impl Parser {
let (explicit_self, d) = do self.parse_fn_decl_with_self() |p| {
// This is somewhat dubious; We don't want to allow argument
// names to be left off if there is a definition...
either::Left(p.parse_arg_general(false))
p.parse_arg_general(false)
};

let hi = p.last_span.hi;
Expand Down Expand Up @@ -1290,12 +1287,12 @@ impl Parser {
}

// parse a single function argument
pub fn parse_arg(&self) -> arg_or_capture_item {
either::Left(self.parse_arg_general(true))
pub fn parse_arg(&self) -> arg {
self.parse_arg_general(true)
}

// parse an argument in a lambda header e.g. |arg, arg|
pub fn parse_fn_block_arg(&self) -> arg_or_capture_item {
pub fn parse_fn_block_arg(&self) -> arg {
self.parse_arg_mode();
let is_mutbl = self.eat_keyword(keywords::Mut);
let pat = self.parse_pat();
Expand All @@ -1308,12 +1305,12 @@ impl Parser {
span: mk_sp(self.span.lo, self.span.hi),
}
};
either::Left(ast::arg {
ast::arg {
is_mutbl: is_mutbl,
ty: t,
pat: pat,
id: ast::DUMMY_NODE_ID
})
}
}

pub fn maybe_parse_fixed_vstore(&self) -> Option<@ast::Expr> {
Expand Down Expand Up @@ -3500,19 +3497,17 @@ impl Parser {

// parse the argument list and result type of a function declaration
pub fn parse_fn_decl(&self) -> fn_decl {
let args_or_capture_items: ~[arg_or_capture_item] =
let args: ~[arg] =
self.parse_unspanned_seq(
&token::LPAREN,
&token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA),
|p| p.parse_arg()
);

let inputs = either::lefts(args_or_capture_items.move_iter()).collect();

let (ret_style, ret_ty) = self.parse_ret_ty();
ast::fn_decl {
inputs: inputs,
inputs: args,
output: ret_ty,
cf: ret_style,
}
Expand Down Expand Up @@ -3542,7 +3537,7 @@ impl Parser {
fn parse_fn_decl_with_self(
&self,
parse_arg_fn:
&fn(&Parser) -> arg_or_capture_item
&fn(&Parser) -> arg
) -> (explicit_self, fn_decl) {
fn maybe_parse_explicit_self(
cnstr: &fn(v: Mutability) -> ast::explicit_self_,
Expand Down Expand Up @@ -3650,20 +3645,20 @@ impl Parser {
};

// If we parsed a self type, expect a comma before the argument list.
let args_or_capture_items;
let fn_inputs;
if explicit_self != sty_static {
match *self.token {
token::COMMA => {
self.bump();
let sep = seq_sep_trailing_disallowed(token::COMMA);
args_or_capture_items = self.parse_seq_to_before_end(
fn_inputs = self.parse_seq_to_before_end(
&token::RPAREN,
sep,
parse_arg_fn
);
}
token::RPAREN => {
args_or_capture_items = ~[];
fn_inputs = ~[];
}
_ => {
self.fatal(
Expand All @@ -3676,7 +3671,7 @@ impl Parser {
}
} else {
let sep = seq_sep_trailing_disallowed(token::COMMA);
args_or_capture_items = self.parse_seq_to_before_end(
fn_inputs = self.parse_seq_to_before_end(
&token::RPAREN,
sep,
parse_arg_fn
Expand All @@ -3687,11 +3682,10 @@ impl Parser {

let hi = self.span.hi;

let inputs = either::lefts(args_or_capture_items.move_iter()).collect();
let (ret_style, ret_ty) = self.parse_ret_ty();

let fn_decl = ast::fn_decl {
inputs: inputs,
inputs: fn_inputs,
output: ret_ty,
cf: ret_style
};
Expand Down Expand Up @@ -3720,7 +3714,7 @@ impl Parser {
};

ast::fn_decl {
inputs: either::lefts(inputs_captures.move_iter()).collect(),
inputs: inputs_captures,
output: output,
cf: return_val,
}
Expand Down