Skip to content

Commit 1c26513

Browse files
committed
auto merge of #9180 : blake2-ppc/rust/reduce-either, r=catamorphism
Work a bit towards #9157 "Remove Either". These instances don't need to use Either and are better expressed in other ways (removing allocations and simplifying types).
2 parents 5d905f1 + 15c9dc7 commit 1c26513

File tree

6 files changed

+58
-66
lines changed

6 files changed

+58
-66
lines changed

src/libextra/test.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use treemap::TreeMap;
3131
use std::clone::Clone;
3232
use std::comm::{stream, SharedChan, GenericPort, GenericChan};
3333
use std::libc;
34-
use std::either;
3534
use std::io;
3635
use std::result;
3736
use std::task;
@@ -127,8 +126,8 @@ pub type MetricDiff = TreeMap<~str,MetricChange>;
127126
pub fn test_main(args: &[~str], tests: ~[TestDescAndFn]) {
128127
let opts =
129128
match parse_opts(args) {
130-
either::Left(o) => o,
131-
either::Right(m) => fail!(m)
129+
Ok(o) => o,
130+
Err(msg) => fail!(msg)
132131
};
133132
if !run_tests_console(&opts, tests) { fail!("Some tests failed"); }
134133
}
@@ -169,7 +168,7 @@ pub struct TestOpts {
169168
logfile: Option<Path>
170169
}
171170

172-
type OptRes = Either<TestOpts, ~str>;
171+
type OptRes = Result<TestOpts, ~str>;
173172

174173
fn optgroups() -> ~[getopts::groups::OptGroup] {
175174
~[groups::optflag("", "ignored", "Run ignored tests"),
@@ -228,7 +227,7 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
228227
let matches =
229228
match groups::getopts(args_, optgroups()) {
230229
Ok(m) => m,
231-
Err(f) => return either::Right(getopts::fail_str(f))
230+
Err(f) => return Err(getopts::fail_str(f))
232231
};
233232

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

277-
either::Left(test_opts)
276+
Ok(test_opts)
278277
}
279278

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

1158-
use std::either;
11591157
use std::comm::{stream, SharedChan};
11601158
use tempfile;
11611159
use std::os;
@@ -1236,8 +1234,8 @@ mod tests {
12361234
fn first_free_arg_should_be_a_filter() {
12371235
let args = ~[~"progname", ~"filter"];
12381236
let opts = match parse_opts(args) {
1239-
either::Left(o) => o,
1240-
_ => fail!("Malformed arg in first_free_arg_should_be_a_filter")
1237+
Ok(o) => o,
1238+
_ => fail!("Malformed arg in first_free_arg_should_be_a_filter")
12411239
};
12421240
assert!("filter" == opts.filter.clone().unwrap());
12431241
}
@@ -1246,8 +1244,8 @@ mod tests {
12461244
fn parse_ignored_flag() {
12471245
let args = ~[~"progname", ~"filter", ~"--ignored"];
12481246
let opts = match parse_opts(args) {
1249-
either::Left(o) => o,
1250-
_ => fail!("Malformed arg in parse_ignored_flag")
1247+
Ok(o) => o,
1248+
_ => fail!("Malformed arg in parse_ignored_flag")
12511249
};
12521250
assert!((opts.run_ignored));
12531251
}

src/libextra/workcache.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use arc::{Arc,RWArc};
1919
use treemap::TreeMap;
2020
use std::cell::Cell;
2121
use std::comm::{PortOne, oneshot};
22-
use std::either::{Either, Left, Right};
2322
use std::{io, os, task};
2423

2524
/**
@@ -252,9 +251,9 @@ struct Exec {
252251
discovered_outputs: WorkMap
253252
}
254253
255-
struct Work<'self, T> {
256-
prep: &'self Prep<'self>,
257-
res: Option<Either<T,PortOne<(Exec,T)>>>
254+
enum Work<'self, T> {
255+
WorkValue(T),
256+
WorkFromTask(&'self Prep<'self>, PortOne<(Exec, T)>),
258257
}
259258
260259
fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str {
@@ -426,15 +425,15 @@ impl<'self> Prep<'self> {
426425
db.prepare(self.fn_name, &self.declared_inputs)
427426
};
428427

429-
let res = match cached {
428+
match cached {
430429
Some((ref disc_in, ref disc_out, ref res))
431430
if self.all_fresh("declared input",&self.declared_inputs) &&
432431
self.all_fresh("discovered input", disc_in) &&
433432
self.all_fresh("discovered output", disc_out) => {
434433
debug!("Cache hit!");
435434
debug!("Trying to decode: %? / %? / %?",
436435
disc_in, disc_out, *res);
437-
Left(json_decode(*res))
436+
Work::from_value(json_decode(*res))
438437
}
439438

440439
_ => {
@@ -453,10 +452,9 @@ impl<'self> Prep<'self> {
453452
let v = blk(&mut exe);
454453
chan.send((exe, v));
455454
}
456-
Right(port)
455+
Work::from_task(self, port)
457456
}
458-
};
459-
Work::new(self, res)
457+
}
460458
}
461459
}
462460

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

468-
pub fn new(p: &'self Prep<'self>, e: Either<T,PortOne<(Exec,T)>>) -> Work<'self, T> {
469-
Work { prep: p, res: Some(e) }
466+
pub fn from_value(elt: T) -> Work<'self, T> {
467+
WorkValue(elt)
468+
}
469+
pub fn from_task(prep: &'self Prep<'self>, port: PortOne<(Exec, T)>)
470+
-> Work<'self, T> {
471+
WorkFromTask(prep, port)
470472
}
471473

472474
pub fn unwrap(self) -> T {
473-
let Work { prep, res } = self;
474-
match res {
475-
None => fail!(),
476-
Some(Left(v)) => v,
477-
Some(Right(port)) => {
475+
match self {
476+
WorkValue(v) => v,
477+
WorkFromTask(prep, port) => {
478478
let (exe, v) = port.recv();
479479
let s = json_encode(&v);
480480
do prep.ctxt.db.write |db| {

src/libstd/logging.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
1313
use option::*;
1414
use os;
15-
use either::*;
1615
use rt;
17-
use rt::logging::{Logger, StdErrLogger};
16+
use rt::logging::{Logger, StdErrLogger, OwnedString};
1817

1918
/// Turns on logging to stdout globally
2019
pub fn console_on() {
@@ -57,12 +56,12 @@ fn newsched_log_str(msg: ~str) {
5756
match optional_task {
5857
Some(local) => {
5958
// Use the available logger
60-
(*local).logger.log(Left(msg));
59+
(*local).logger.log(OwnedString(msg));
6160
}
6261
None => {
6362
// There is no logger anywhere, just write to stderr
6463
let mut logger = StdErrLogger;
65-
logger.log(Left(msg));
64+
logger.log(OwnedString(msg));
6665
}
6766
}
6867
}

src/libstd/rt/logging.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10-
use either::*;
1110
use libc::{uintptr_t, exit, STDERR_FILENO};
1211
use option::{Some, None, Option};
1312
use rt::util::dumb_println;
@@ -168,29 +167,32 @@ fn update_log_settings(crate_map: *u8, settings: ~str) {
168167
}
169168
}
170169

170+
/// Represent a string with `Send` bound.
171+
pub enum SendableString {
172+
OwnedString(~str),
173+
StaticString(&'static str)
174+
}
175+
171176
pub trait Logger {
172-
fn log(&mut self, msg: Either<~str, &'static str>);
177+
fn log(&mut self, msg: SendableString);
173178
}
174179

175180
pub struct StdErrLogger;
176181

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

181186
if !should_log_console() {
182187
return;
183188
}
184189

185190
let s: &str = match msg {
186-
Left(ref s) => {
187-
let s: &str = *s;
188-
s
189-
}
190-
Right(ref s) => {
191-
let s: &str = *s;
192-
s
193-
}
191+
OwnedString(ref s) => {
192+
let slc: &str = *s;
193+
slc
194+
},
195+
StaticString(s) => s,
194196
};
195197

196198
// Truncate the string

src/libstd/sys.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,11 @@ impl FailWithCause for &'static str {
136136

137137
// FIXME #4427: Temporary until rt::rt_fail_ goes away
138138
pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
139-
use either::Left;
140139
use option::{Some, None};
141140
use rt::in_green_task_context;
142141
use rt::task::Task;
143142
use rt::local::Local;
144-
use rt::logging::Logger;
143+
use rt::logging::{Logger, OwnedString};
145144
use str::Str;
146145

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

167-
task.logger.log(Left(msg));
166+
task.logger.log(OwnedString(msg));
168167
}
169168
} else {
170169
rterrln!("failed in non-task context at '%s', %s:%i",

src/libsyntax/parse/parser.rs

+15-21
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ use parse::{new_sub_parser_from_file, ParseSess};
8080
use opt_vec;
8181
use opt_vec::OptVec;
8282

83-
use std::either::Either;
84-
use std::either;
8583
use std::hashmap::HashSet;
8684
use std::util;
8785
use std::vec;
@@ -94,7 +92,6 @@ enum restriction {
9492
RESTRICT_NO_BAR_OR_DOUBLEBAR_OP,
9593
}
9694

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

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

942939
let hi = p.last_span.hi;
@@ -1290,12 +1287,12 @@ impl Parser {
12901287
}
12911288

12921289
// parse a single function argument
1293-
pub fn parse_arg(&self) -> arg_or_capture_item {
1294-
either::Left(self.parse_arg_general(true))
1290+
pub fn parse_arg(&self) -> arg {
1291+
self.parse_arg_general(true)
12951292
}
12961293

12971294
// parse an argument in a lambda header e.g. |arg, arg|
1298-
pub fn parse_fn_block_arg(&self) -> arg_or_capture_item {
1295+
pub fn parse_fn_block_arg(&self) -> arg {
12991296
self.parse_arg_mode();
13001297
let is_mutbl = self.eat_keyword(keywords::Mut);
13011298
let pat = self.parse_pat();
@@ -1308,12 +1305,12 @@ impl Parser {
13081305
span: mk_sp(self.span.lo, self.span.hi),
13091306
}
13101307
};
1311-
either::Left(ast::arg {
1308+
ast::arg {
13121309
is_mutbl: is_mutbl,
13131310
ty: t,
13141311
pat: pat,
13151312
id: ast::DUMMY_NODE_ID
1316-
})
1313+
}
13171314
}
13181315

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

35013498
// parse the argument list and result type of a function declaration
35023499
pub fn parse_fn_decl(&self) -> fn_decl {
3503-
let args_or_capture_items: ~[arg_or_capture_item] =
3500+
let args: ~[arg] =
35043501
self.parse_unspanned_seq(
35053502
&token::LPAREN,
35063503
&token::RPAREN,
35073504
seq_sep_trailing_disallowed(token::COMMA),
35083505
|p| p.parse_arg()
35093506
);
35103507

3511-
let inputs = either::lefts(args_or_capture_items.move_iter()).collect();
3512-
35133508
let (ret_style, ret_ty) = self.parse_ret_ty();
35143509
ast::fn_decl {
3515-
inputs: inputs,
3510+
inputs: args,
35163511
output: ret_ty,
35173512
cf: ret_style,
35183513
}
@@ -3542,7 +3537,7 @@ impl Parser {
35423537
fn parse_fn_decl_with_self(
35433538
&self,
35443539
parse_arg_fn:
3545-
&fn(&Parser) -> arg_or_capture_item
3540+
&fn(&Parser) -> arg
35463541
) -> (explicit_self, fn_decl) {
35473542
fn maybe_parse_explicit_self(
35483543
cnstr: &fn(v: Mutability) -> ast::explicit_self_,
@@ -3650,20 +3645,20 @@ impl Parser {
36503645
};
36513646

36523647
// If we parsed a self type, expect a comma before the argument list.
3653-
let args_or_capture_items;
3648+
let fn_inputs;
36543649
if explicit_self != sty_static {
36553650
match *self.token {
36563651
token::COMMA => {
36573652
self.bump();
36583653
let sep = seq_sep_trailing_disallowed(token::COMMA);
3659-
args_or_capture_items = self.parse_seq_to_before_end(
3654+
fn_inputs = self.parse_seq_to_before_end(
36603655
&token::RPAREN,
36613656
sep,
36623657
parse_arg_fn
36633658
);
36643659
}
36653660
token::RPAREN => {
3666-
args_or_capture_items = ~[];
3661+
fn_inputs = ~[];
36673662
}
36683663
_ => {
36693664
self.fatal(
@@ -3676,7 +3671,7 @@ impl Parser {
36763671
}
36773672
} else {
36783673
let sep = seq_sep_trailing_disallowed(token::COMMA);
3679-
args_or_capture_items = self.parse_seq_to_before_end(
3674+
fn_inputs = self.parse_seq_to_before_end(
36803675
&token::RPAREN,
36813676
sep,
36823677
parse_arg_fn
@@ -3687,11 +3682,10 @@ impl Parser {
36873682

36883683
let hi = self.span.hi;
36893684

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

36933687
let fn_decl = ast::fn_decl {
3694-
inputs: inputs,
3688+
inputs: fn_inputs,
36953689
output: ret_ty,
36963690
cf: ret_style
36973691
};
@@ -3720,7 +3714,7 @@ impl Parser {
37203714
};
37213715

37223716
ast::fn_decl {
3723-
inputs: either::lefts(inputs_captures.move_iter()).collect(),
3717+
inputs: inputs_captures,
37243718
output: output,
37253719
cf: return_val,
37263720
}

0 commit comments

Comments
 (0)