Skip to content

Commit 7bb498b

Browse files
committed
Mass rename if_ok! to try!
This "bubble up an error" macro was originally named if_ok! in order to get it landed, but after the fact it was discovered that this name is not exactly desirable. The name `if_ok!` isn't immediately clear that is has much to do with error handling, and it doesn't look fantastic in all contexts (if if_ok!(...) {}). In general, the agreed opinion about `if_ok!` is that is came in as subpar. The name `try!` is more invocative of error handling, it's shorter by 2 letters, and it looks fitting in almost all circumstances. One concern about the word `try!` is that it's too invocative of exceptions, but the belief is that this will be overcome with documentation and examples. Close #12037
1 parent 06e1281 commit 7bb498b

File tree

32 files changed

+1224
-1228
lines changed

32 files changed

+1224
-1228
lines changed

src/libextra/json.rs

+52-52
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ use serialize::Encodable;
246246
use serialize;
247247
use collections::TreeMap;
248248

249-
macro_rules! if_ok( ($e:expr) => (
249+
macro_rules! try( ($e:expr) => (
250250
match $e { Ok(e) => e, Err(e) => { self.error = Err(e); return } }
251251
) )
252252

@@ -342,7 +342,7 @@ impl<'a> Encoder<'a> {
342342
}
343343

344344
impl<'a> serialize::Encoder for Encoder<'a> {
345-
fn emit_nil(&mut self) { if_ok!(write!(self.wr, "null")) }
345+
fn emit_nil(&mut self) { try!(write!(self.wr, "null")) }
346346

347347
fn emit_uint(&mut self, v: uint) { self.emit_f64(v as f64); }
348348
fn emit_u64(&mut self, v: u64) { self.emit_f64(v as f64); }
@@ -358,20 +358,20 @@ impl<'a> serialize::Encoder for Encoder<'a> {
358358

359359
fn emit_bool(&mut self, v: bool) {
360360
if v {
361-
if_ok!(write!(self.wr, "true"));
361+
try!(write!(self.wr, "true"));
362362
} else {
363-
if_ok!(write!(self.wr, "false"));
363+
try!(write!(self.wr, "false"));
364364
}
365365
}
366366

367367
fn emit_f64(&mut self, v: f64) {
368-
if_ok!(write!(self.wr, "{}", f64::to_str_digits(v, 6u)))
368+
try!(write!(self.wr, "{}", f64::to_str_digits(v, 6u)))
369369
}
370370
fn emit_f32(&mut self, v: f32) { self.emit_f64(v as f64); }
371371

372372
fn emit_char(&mut self, v: char) { self.emit_str(str::from_char(v)) }
373373
fn emit_str(&mut self, v: &str) {
374-
if_ok!(write!(self.wr, "{}", escape_str(v)))
374+
try!(write!(self.wr, "{}", escape_str(v)))
375375
}
376376

377377
fn emit_enum(&mut self, _name: &str, f: |&mut Encoder<'a>|) { f(self) }
@@ -385,19 +385,19 @@ impl<'a> serialize::Encoder for Encoder<'a> {
385385
// Bunny => "Bunny"
386386
// Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
387387
if cnt == 0 {
388-
if_ok!(write!(self.wr, "{}", escape_str(name)));
388+
try!(write!(self.wr, "{}", escape_str(name)));
389389
} else {
390-
if_ok!(write!(self.wr, "\\{\"variant\":"));
391-
if_ok!(write!(self.wr, "{}", escape_str(name)));
392-
if_ok!(write!(self.wr, ",\"fields\":["));
390+
try!(write!(self.wr, "\\{\"variant\":"));
391+
try!(write!(self.wr, "{}", escape_str(name)));
392+
try!(write!(self.wr, ",\"fields\":["));
393393
f(self);
394-
if_ok!(write!(self.wr, "]\\}"));
394+
try!(write!(self.wr, "]\\}"));
395395
}
396396
}
397397

398398
fn emit_enum_variant_arg(&mut self, idx: uint, f: |&mut Encoder<'a>|) {
399399
if idx != 0 {
400-
if_ok!(write!(self.wr, ","));
400+
try!(write!(self.wr, ","));
401401
}
402402
f(self);
403403
}
@@ -418,17 +418,17 @@ impl<'a> serialize::Encoder for Encoder<'a> {
418418
}
419419

420420
fn emit_struct(&mut self, _: &str, _: uint, f: |&mut Encoder<'a>|) {
421-
if_ok!(write!(self.wr, r"\{"));
421+
try!(write!(self.wr, r"\{"));
422422
f(self);
423-
if_ok!(write!(self.wr, r"\}"));
423+
try!(write!(self.wr, r"\}"));
424424
}
425425

426426
fn emit_struct_field(&mut self,
427427
name: &str,
428428
idx: uint,
429429
f: |&mut Encoder<'a>|) {
430-
if idx != 0 { if_ok!(write!(self.wr, ",")) }
431-
if_ok!(write!(self.wr, "{}:", escape_str(name)));
430+
if idx != 0 { try!(write!(self.wr, ",")) }
431+
try!(write!(self.wr, "{}:", escape_str(name)));
432432
f(self);
433433
}
434434

@@ -454,31 +454,31 @@ impl<'a> serialize::Encoder for Encoder<'a> {
454454
fn emit_option_some(&mut self, f: |&mut Encoder<'a>|) { f(self); }
455455

456456
fn emit_seq(&mut self, _len: uint, f: |&mut Encoder<'a>|) {
457-
if_ok!(write!(self.wr, "["));
457+
try!(write!(self.wr, "["));
458458
f(self);
459-
if_ok!(write!(self.wr, "]"));
459+
try!(write!(self.wr, "]"));
460460
}
461461

462462
fn emit_seq_elt(&mut self, idx: uint, f: |&mut Encoder<'a>|) {
463463
if idx != 0 {
464-
if_ok!(write!(self.wr, ","));
464+
try!(write!(self.wr, ","));
465465
}
466466
f(self)
467467
}
468468

469469
fn emit_map(&mut self, _len: uint, f: |&mut Encoder<'a>|) {
470-
if_ok!(write!(self.wr, r"\{"));
470+
try!(write!(self.wr, r"\{"));
471471
f(self);
472-
if_ok!(write!(self.wr, r"\}"));
472+
try!(write!(self.wr, r"\}"));
473473
}
474474

475475
fn emit_map_elt_key(&mut self, idx: uint, f: |&mut Encoder<'a>|) {
476-
if idx != 0 { if_ok!(write!(self.wr, ",")) }
476+
if idx != 0 { try!(write!(self.wr, ",")) }
477477
f(self)
478478
}
479479

480480
fn emit_map_elt_val(&mut self, _idx: uint, f: |&mut Encoder<'a>|) {
481-
if_ok!(write!(self.wr, ":"));
481+
try!(write!(self.wr, ":"));
482482
f(self)
483483
}
484484
}
@@ -503,7 +503,7 @@ impl<'a> PrettyEncoder<'a> {
503503
}
504504

505505
impl<'a> serialize::Encoder for PrettyEncoder<'a> {
506-
fn emit_nil(&mut self) { if_ok!(write!(self.wr, "null")); }
506+
fn emit_nil(&mut self) { try!(write!(self.wr, "null")); }
507507

508508
fn emit_uint(&mut self, v: uint) { self.emit_f64(v as f64); }
509509
fn emit_u64(&mut self, v: u64) { self.emit_f64(v as f64); }
@@ -519,20 +519,20 @@ impl<'a> serialize::Encoder for PrettyEncoder<'a> {
519519

520520
fn emit_bool(&mut self, v: bool) {
521521
if v {
522-
if_ok!(write!(self.wr, "true"));
522+
try!(write!(self.wr, "true"));
523523
} else {
524-
if_ok!(write!(self.wr, "false"));
524+
try!(write!(self.wr, "false"));
525525
}
526526
}
527527

528528
fn emit_f64(&mut self, v: f64) {
529-
if_ok!(write!(self.wr, "{}", f64::to_str_digits(v, 6u)));
529+
try!(write!(self.wr, "{}", f64::to_str_digits(v, 6u)));
530530
}
531531
fn emit_f32(&mut self, v: f32) { self.emit_f64(v as f64); }
532532

533533
fn emit_char(&mut self, v: char) { self.emit_str(str::from_char(v)) }
534534
fn emit_str(&mut self, v: &str) {
535-
if_ok!(write!(self.wr, "{}", escape_str(v)));
535+
try!(write!(self.wr, "{}", escape_str(v)));
536536
}
537537

538538
fn emit_enum(&mut self, _name: &str, f: |&mut PrettyEncoder<'a>|) {
@@ -545,24 +545,24 @@ impl<'a> serialize::Encoder for PrettyEncoder<'a> {
545545
cnt: uint,
546546
f: |&mut PrettyEncoder<'a>|) {
547547
if cnt == 0 {
548-
if_ok!(write!(self.wr, "{}", escape_str(name)));
548+
try!(write!(self.wr, "{}", escape_str(name)));
549549
} else {
550550
self.indent += 2;
551-
if_ok!(write!(self.wr, "[\n{}{},\n", spaces(self.indent),
551+
try!(write!(self.wr, "[\n{}{},\n", spaces(self.indent),
552552
escape_str(name)));
553553
f(self);
554554
self.indent -= 2;
555-
if_ok!(write!(self.wr, "\n{}]", spaces(self.indent)));
555+
try!(write!(self.wr, "\n{}]", spaces(self.indent)));
556556
}
557557
}
558558

559559
fn emit_enum_variant_arg(&mut self,
560560
idx: uint,
561561
f: |&mut PrettyEncoder<'a>|) {
562562
if idx != 0 {
563-
if_ok!(write!(self.wr, ",\n"));
563+
try!(write!(self.wr, ",\n"));
564564
}
565-
if_ok!(write!(self.wr, "{}", spaces(self.indent)));
565+
try!(write!(self.wr, "{}", spaces(self.indent)));
566566
f(self)
567567
}
568568

@@ -587,13 +587,13 @@ impl<'a> serialize::Encoder for PrettyEncoder<'a> {
587587
len: uint,
588588
f: |&mut PrettyEncoder<'a>|) {
589589
if len == 0 {
590-
if_ok!(write!(self.wr, "\\{\\}"));
590+
try!(write!(self.wr, "\\{\\}"));
591591
} else {
592-
if_ok!(write!(self.wr, "\\{"));
592+
try!(write!(self.wr, "\\{"));
593593
self.indent += 2;
594594
f(self);
595595
self.indent -= 2;
596-
if_ok!(write!(self.wr, "\n{}\\}", spaces(self.indent)));
596+
try!(write!(self.wr, "\n{}\\}", spaces(self.indent)));
597597
}
598598
}
599599

@@ -602,11 +602,11 @@ impl<'a> serialize::Encoder for PrettyEncoder<'a> {
602602
idx: uint,
603603
f: |&mut PrettyEncoder<'a>|) {
604604
if idx == 0 {
605-
if_ok!(write!(self.wr, "\n"));
605+
try!(write!(self.wr, "\n"));
606606
} else {
607-
if_ok!(write!(self.wr, ",\n"));
607+
try!(write!(self.wr, ",\n"));
608608
}
609-
if_ok!(write!(self.wr, "{}{}: ", spaces(self.indent), escape_str(name)));
609+
try!(write!(self.wr, "{}{}: ", spaces(self.indent), escape_str(name)));
610610
f(self);
611611
}
612612

@@ -635,50 +635,50 @@ impl<'a> serialize::Encoder for PrettyEncoder<'a> {
635635

636636
fn emit_seq(&mut self, len: uint, f: |&mut PrettyEncoder<'a>|) {
637637
if len == 0 {
638-
if_ok!(write!(self.wr, "[]"));
638+
try!(write!(self.wr, "[]"));
639639
} else {
640-
if_ok!(write!(self.wr, "["));
640+
try!(write!(self.wr, "["));
641641
self.indent += 2;
642642
f(self);
643643
self.indent -= 2;
644-
if_ok!(write!(self.wr, "\n{}]", spaces(self.indent)));
644+
try!(write!(self.wr, "\n{}]", spaces(self.indent)));
645645
}
646646
}
647647

648648
fn emit_seq_elt(&mut self, idx: uint, f: |&mut PrettyEncoder<'a>|) {
649649
if idx == 0 {
650-
if_ok!(write!(self.wr, "\n"));
650+
try!(write!(self.wr, "\n"));
651651
} else {
652-
if_ok!(write!(self.wr, ",\n"));
652+
try!(write!(self.wr, ",\n"));
653653
}
654-
if_ok!(write!(self.wr, "{}", spaces(self.indent)));
654+
try!(write!(self.wr, "{}", spaces(self.indent)));
655655
f(self)
656656
}
657657

658658
fn emit_map(&mut self, len: uint, f: |&mut PrettyEncoder<'a>|) {
659659
if len == 0 {
660-
if_ok!(write!(self.wr, "\\{\\}"));
660+
try!(write!(self.wr, "\\{\\}"));
661661
} else {
662-
if_ok!(write!(self.wr, "\\{"));
662+
try!(write!(self.wr, "\\{"));
663663
self.indent += 2;
664664
f(self);
665665
self.indent -= 2;
666-
if_ok!(write!(self.wr, "\n{}\\}", spaces(self.indent)));
666+
try!(write!(self.wr, "\n{}\\}", spaces(self.indent)));
667667
}
668668
}
669669

670670
fn emit_map_elt_key(&mut self, idx: uint, f: |&mut PrettyEncoder<'a>|) {
671671
if idx == 0 {
672-
if_ok!(write!(self.wr, "\n"));
672+
try!(write!(self.wr, "\n"));
673673
} else {
674-
if_ok!(write!(self.wr, ",\n"));
674+
try!(write!(self.wr, ",\n"));
675675
}
676-
if_ok!(write!(self.wr, "{}", spaces(self.indent)));
676+
try!(write!(self.wr, "{}", spaces(self.indent)));
677677
f(self);
678678
}
679679

680680
fn emit_map_elt_val(&mut self, _idx: uint, f: |&mut PrettyEncoder<'a>|) {
681-
if_ok!(write!(self.wr, ": "));
681+
try!(write!(self.wr, ": "));
682682
f(self);
683683
}
684684
}

src/libextra/stats.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -376,48 +376,48 @@ pub fn write_boxplot(w: &mut io::Writer, s: &Summary,
376376
let range_width = width_hint - overhead_width;;
377377
let char_step = range / (range_width as f64);
378378

379-
if_ok!(write!(w, "{} |", lostr));
379+
try!(write!(w, "{} |", lostr));
380380

381381
let mut c = 0;
382382
let mut v = lo;
383383

384384
while c < range_width && v < s.min {
385-
if_ok!(write!(w, " "));
385+
try!(write!(w, " "));
386386
v += char_step;
387387
c += 1;
388388
}
389-
if_ok!(write!(w, "["));
389+
try!(write!(w, "["));
390390
c += 1;
391391
while c < range_width && v < q1 {
392-
if_ok!(write!(w, "-"));
392+
try!(write!(w, "-"));
393393
v += char_step;
394394
c += 1;
395395
}
396396
while c < range_width && v < q2 {
397-
if_ok!(write!(w, "*"));
397+
try!(write!(w, "*"));
398398
v += char_step;
399399
c += 1;
400400
}
401-
if_ok!(write!(w, r"\#"));
401+
try!(write!(w, r"\#"));
402402
c += 1;
403403
while c < range_width && v < q3 {
404-
if_ok!(write!(w, "*"));
404+
try!(write!(w, "*"));
405405
v += char_step;
406406
c += 1;
407407
}
408408
while c < range_width && v < s.max {
409-
if_ok!(write!(w, "-"));
409+
try!(write!(w, "-"));
410410
v += char_step;
411411
c += 1;
412412
}
413-
if_ok!(write!(w, "]"));
413+
try!(write!(w, "]"));
414414
while c < range_width {
415-
if_ok!(write!(w, " "));
415+
try!(write!(w, " "));
416416
v += char_step;
417417
c += 1;
418418
}
419419

420-
if_ok!(write!(w, "| {}", histr));
420+
try!(write!(w, "| {}", histr));
421421
Ok(())
422422
}
423423

src/libnative/io/pipe_unix.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ impl Drop for Inner {
7777
}
7878

7979
fn connect(addr: &CString, ty: libc::c_int) -> IoResult<Inner> {
80-
let (addr, len) = if_ok!(addr_to_sockaddr_un(addr));
81-
let inner = Inner { fd: if_ok!(unix_socket(ty)) };
80+
let (addr, len) = try!(addr_to_sockaddr_un(addr));
81+
let inner = Inner { fd: try!(unix_socket(ty)) };
8282
let addrp = &addr as *libc::sockaddr_storage;
8383
match retry(|| unsafe {
8484
libc::connect(inner.fd, addrp as *libc::sockaddr,
@@ -90,8 +90,8 @@ fn connect(addr: &CString, ty: libc::c_int) -> IoResult<Inner> {
9090
}
9191

9292
fn bind(addr: &CString, ty: libc::c_int) -> IoResult<Inner> {
93-
let (addr, len) = if_ok!(addr_to_sockaddr_un(addr));
94-
let inner = Inner { fd: if_ok!(unix_socket(ty)) };
93+
let (addr, len) = try!(addr_to_sockaddr_un(addr));
94+
let inner = Inner { fd: try!(unix_socket(ty)) };
9595
let addrp = &addr as *libc::sockaddr_storage;
9696
match unsafe {
9797
libc::bind(inner.fd, addrp as *libc::sockaddr, len as libc::socklen_t)
@@ -198,7 +198,7 @@ impl UnixDatagram {
198198
}
199199

200200
pub fn sendto(&mut self, buf: &[u8], dst: &CString) -> IoResult<()> {
201-
let (dst, len) = if_ok!(addr_to_sockaddr_un(dst));
201+
let (dst, len) = try!(addr_to_sockaddr_un(dst));
202202
let dstp = &dst as *libc::sockaddr_storage;
203203
let ret = retry(|| unsafe {
204204
libc::sendto(self.fd(),

0 commit comments

Comments
 (0)