Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Rust update: s/if_ok!/try!/ (rust-lang/rust#12401)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-morgan committed Feb 21, 2014
1 parent a6a22f0 commit 442c30e
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 75 deletions.
8 changes: 4 additions & 4 deletions src/codegen/branchify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ pub fn generate_branchified_method(
let indentstr = " ".repeat(indent * 4);
macro_rules! w (
($s:expr) => {
if_ok!(write!(writer, "{}{}\n", indentstr, $s))
try!(write!(writer, "{}{}\n", indentstr, $s))
}
)
for &c in branch.matches.iter() {
let next_prefix = format!("{}{}", prefix, c as char);
w!(format!("Ok(b) if b == '{}' as u8 => match {} \\{", c as char, read_call));
for b in branch.children.iter() {
if_ok!(r(writer, b, next_prefix, indent + 1, read_call, end, max_len, valid, unknown));
try!(r(writer, b, next_prefix, indent + 1, read_call, end, max_len, valid, unknown));
}
match branch.result {
Some(ref result) =>
Expand All @@ -117,13 +117,13 @@ pub fn generate_branchified_method(
let indentstr = " ".repeat(indent * 4);
macro_rules! w (
($s:expr) => {
if_ok!(write!(writer, "{}{}\n", indentstr, $s))
try!(write!(writer, "{}{}\n", indentstr, $s))
}
)

w!(format!("let (s, next_byte) = match {} \\{", read_call));
for b in branches.iter() {
if_ok!(r(writer, b, "", indent + 1, read_call, end, max_len, valid, unknown));
try!(r(writer, b, "", indent + 1, read_call, end, max_len, valid, unknown));
}
w!(format!(" Ok(b) if {} => (\"\", b),", valid));
w!( (" Ok(_) => return Err(::std::io::IoError { kind: ::std::io::OtherIoError, desc: \"bad value\", detail: None }),"));
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/read_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::IoResult;

pub fn generate(output_dir: &Path) -> IoResult<()> {
let mut writer = get_writer(output_dir, "read_method.rs");
if_ok!(writer.write(bytes!("\
try!(writer.write(bytes!("\
// This automatically generated file is included in request.rs.
{
use method::{Connect, Delete, Get, Head, Options, Patch, Post, Put, Trace, ExtensionMethod};
Expand All @@ -13,7 +13,7 @@ pub fn generate(output_dir: &Path) -> IoResult<()> {
")));

if_ok!(generate_branchified_method(
try!(generate_branchified_method(
writer,
branchify!(case sensitive,
"CONNECT" => Connect,
Expand Down
32 changes: 16 additions & 16 deletions src/codegen/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn generate(output_dir: &Path) -> IoResult<()> {
Status(status) => status.reason.len(),
}).max_by(|&i| i).unwrap();
}
if_ok!(out.write("// This file is automatically generated file is used as http::status.
try!(out.write("// This file is automatically generated file is used as http::status.
use std::fmt;
use std::ascii::StrAsciiExt;
Expand All @@ -168,15 +168,15 @@ pub enum Status {
".as_bytes()));
for &entry in entries.iter() {
match entry {
Heading(heading) => if_ok!(write!(out, "\n // {}\n", heading)),
Heading(heading) => try!(write!(out, "\n // {}\n", heading)),
Status(status) => match status.comment {
None => if_ok!(write!(out, " {},\n", status.ident())),
Some(comment) => if_ok!(write!(out, " {}, // {}\n", status.ident(), comment)),
None => try!(write!(out, " {},\n", status.ident())),
Some(comment) => try!(write!(out, " {}, // {}\n", status.ident(), comment)),
},
}
}

if_ok!(out.write("
try!(out.write("
UnregisteredStatus(u16, ~str),
}
Expand All @@ -188,12 +188,12 @@ impl Status {
".as_bytes()));
for &entry in entries.iter() {
match entry {
Heading(heading) => if_ok!(write!(out, "\n // {}\n", heading)),
Status(status) => if_ok!(write!(out, " {} => {},\n",
Heading(heading) => try!(write!(out, "\n // {}\n", heading)),
Status(status) => try!(write!(out, " {} => {},\n",
status.padded_ident(), status.code)),
}
}
if_ok!(out.write("
try!(out.write("
UnregisteredStatus(code, _) => code,
}
}
Expand All @@ -204,12 +204,12 @@ impl Status {
".as_bytes()));
for &entry in entries.iter() {
match entry {
Heading(heading) => if_ok!(write!(out, "\n // {}\n", heading)),
Status(status) => if_ok!(write!(out, " {} => ~\"{}\",\n",
Heading(heading) => try!(write!(out, "\n // {}\n", heading)),
Status(status) => try!(write!(out, " {} => ~\"{}\",\n",
status.padded_ident(), status.reason))
}
}
if_ok!(out.write("
try!(out.write("
UnregisteredStatus(_, ref reason) => (*reason).clone(),
}
}
Expand All @@ -221,15 +221,15 @@ impl Status {
".as_bytes()));
for &entry in entries.iter() {
match entry {
Heading(heading) => if_ok!(write!(out, "\n // {}\n", heading)),
Status(status) => if_ok!(write!(out, " ({}, \"{}\"){} => {},\n",
Heading(heading) => try!(write!(out, "\n // {}\n", heading)),
Status(status) => try!(write!(out, " ({}, \"{}\"){} => {},\n",
status.code,
status.reason.to_ascii_lower(),
status.reason_padding_spaces(),
status.ident())),
}
}
if_ok!(out.write("
try!(out.write("
(_, _) => UnregisteredStatus(status, reason),
}
}
Expand Down Expand Up @@ -295,12 +295,12 @@ impl FromPrimitive for Status {
let mut matched_numbers = HashSet::new();
for &entry in entries.iter() {
match entry {
Heading(heading) => if_ok!(write!(out, "\n // {}\n", heading)),
Heading(heading) => try!(write!(out, "\n // {}\n", heading)),
Status(status) => {
if !matched_numbers.contains(&status.code) {
// Purpose: FailedDependency and MethodFailure both use 424,
// but clearly they mustn't both go in here
if_ok!(write!(out, " {:u} => {},\n", status.code, status.ident()));
try!(write!(out, " {:u} => {},\n", status.code, status.ident()));
matched_numbers.insert(status.code);
}
},
Expand Down
30 changes: 15 additions & 15 deletions src/http/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<T: Reader> BufferedStream<T> {
pub fn read_byte(&mut self) -> IoResult<u8> {
if self.read_pos == self.read_max {
// Fill the buffer, giving up if we've run out of buffered content
if_ok!(self.fill_buffer());
try!(self.fill_buffer());
}
self.read_pos += 1;
Ok(self.read_buffer[self.read_pos - 1])
Expand All @@ -90,9 +90,9 @@ impl<T: Writer> BufferedStream<T> {
/// At the time of calling this, headers MUST have been written, including the
/// ending CRLF, or else an invalid HTTP response may be written.
pub fn finish_response(&mut self) -> IoResult<()> {
if_ok!(self.flush());
try!(self.flush());
if self.writing_chunked_body {
if_ok!(self.wrapped.write(bytes!("0\r\n\r\n")));
try!(self.wrapped.write(bytes!("0\r\n\r\n")));
}
Ok(())
}
Expand All @@ -106,7 +106,7 @@ impl<T: Reader> Reader for BufferedStream<T> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
if self.read_pos == self.read_max {
// Fill the buffer, giving up if we've run out of buffered content
if_ok!(self.fill_buffer());
try!(self.fill_buffer());
}
let size = min(self.read_max - self.read_pos, buf.len());
vec::bytes::copy_memory(buf, self.read_buffer.slice_from(self.read_pos).slice_to(size));
Expand All @@ -122,16 +122,16 @@ impl<T: Writer> Writer for BufferedStream<T> {
// warranted. Maybe deal with that later.
if self.writing_chunked_body {
let s = format!("{}\r\n", (self.write_len + buf.len()).to_str_radix(16));
if_ok!(self.wrapped.write(s.as_bytes()));
try!(self.wrapped.write(s.as_bytes()));
}
if self.write_len > 0 {
if_ok!(self.wrapped.write(self.write_buffer.slice_to(self.write_len)));
try!(self.wrapped.write(self.write_buffer.slice_to(self.write_len)));
self.write_len = 0;
}
if_ok!(self.wrapped.write(buf));
try!(self.wrapped.write(buf));
self.write_len = 0;
if self.writing_chunked_body {
if_ok!(self.wrapped.write(bytes!("\r\n")));
try!(self.wrapped.write(bytes!("\r\n")));
}
} else {
unsafe { self.write_buffer.mut_slice_from(self.write_len).copy_memory(buf); }
Expand All @@ -140,11 +140,11 @@ impl<T: Writer> Writer for BufferedStream<T> {
if self.write_len == self.write_buffer.len() {
if self.writing_chunked_body {
let s = format!("{}\r\n", self.write_len.to_str_radix(16));
if_ok!(self.wrapped.write(s.as_bytes()));
if_ok!(self.wrapped.write(self.write_buffer));
if_ok!(self.wrapped.write(bytes!("\r\n")));
try!(self.wrapped.write(s.as_bytes()));
try!(self.wrapped.write(self.write_buffer));
try!(self.wrapped.write(bytes!("\r\n")));
} else {
if_ok!(self.wrapped.write(self.write_buffer));
try!(self.wrapped.write(self.write_buffer));
}
self.write_len = 0;
}
Expand All @@ -156,11 +156,11 @@ impl<T: Writer> Writer for BufferedStream<T> {
if self.write_len > 0 {
if self.writing_chunked_body {
let s = format!("{}\r\n", self.write_len.to_str_radix(16));
if_ok!(self.wrapped.write(s.as_bytes()));
try!(self.wrapped.write(s.as_bytes()));
}
if_ok!(self.wrapped.write(self.write_buffer.slice_to(self.write_len)));
try!(self.wrapped.write(self.write_buffer.slice_to(self.write_len)));
if self.writing_chunked_body {
if_ok!(self.wrapped.write(bytes!("\r\n")));
try!(self.wrapped.write(bytes!("\r\n")));
}
self.write_len = 0;
}
Expand Down
14 changes: 7 additions & 7 deletions src/http/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ impl<S: Reader + Writer> RequestWriter<S> {
},
};

let remote_addr = if_ok!(url_to_socket_addr(&url));
let remote_addr = try!(url_to_socket_addr(&url));
info!("using ip address {} for {}", remote_addr.to_str(), url.host);

fn url_to_socket_addr(url: &Url) -> IoResult<SocketAddr> {
// Just grab the first IPv4 address
let addrs = if_ok!(get_host_addresses(url.host));
let addrs = try!(get_host_addresses(url.host));
let addr = addrs.move_iter().find(|&a| {
match a {
Ipv4Addr(..) => true,
Expand Down Expand Up @@ -170,7 +170,7 @@ impl<S: Connecter + Reader + Writer> RequestWriter<S> {

self.stream = match self.remote_addr {
Some(addr) => {
let stream = if_ok!(Connecter::connect(addr));
let stream = try!(Connecter::connect(addr));
Some(BufferedStream::new(stream))
},
None => fail!("connect() called before remote_addr was set"),
Expand All @@ -196,19 +196,19 @@ impl<S: Connecter + Reader + Writer> RequestWriter<S> {
fail!("RequestWriter.write_headers() called, but headers already written");
}
if self.stream.is_none() {
if_ok!(self.connect());
try!(self.connect());
}

// Write the Request-Line (RFC2616 §5.1)
// TODO: get to the point where we can say HTTP/1.1 with good conscience
if_ok!(write!(self.stream.get_mut_ref() as &mut Writer,
try!(write!(self.stream.get_mut_ref() as &mut Writer,
"{} {}{}{} HTTP/1.0\r\n",
self.method.to_str(),
if self.url.path.len() > 0 { self.url.path.as_slice() } else { "/" },
if self.url.query.len() > 0 { "?" } else { "" },
url::query_to_str(&self.url.query)));

if_ok!(self.headers.write_all(self.stream.get_mut_ref()));
try!(self.headers.write_all(self.stream.get_mut_ref()));
self.headers_written = true;
Ok(())
}
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<S: Connecter + Reader + Writer> RequestWriter<S> {
impl<S: Reader + Writer + Connecter> Writer for RequestWriter<S> {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
if !self.headers_written {
if_ok!(self.write_headers());
try!(self.write_headers());
}
// TODO: decide whether using get_mut_ref() is sound
// (it will cause failure if None)
Expand Down
14 changes: 7 additions & 7 deletions src/http/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ pub fn read_http_version<R: Reader>
// filled, so I must read it byte by byte to guarantee correctness.
// (Sure, no sane person/library would send the first packet with "HTT"
// and leave the "P/1.1" to the next packet, but it's *possible*.)
let b0 = if_ok!(reader.read_byte());
let b1 = if_ok!(reader.read_byte());
let b2 = if_ok!(reader.read_byte());
let b3 = if_ok!(reader.read_byte());
let b4 = if_ok!(reader.read_byte());
let b0 = try!(reader.read_byte());
let b1 = try!(reader.read_byte());
let b2 = try!(reader.read_byte());
let b3 = try!(reader.read_byte());
let b4 = try!(reader.read_byte());
if (b0 != 'h' as u8 && b0 != 'H' as u8) ||
(b1 != 't' as u8 && b1 != 'T' as u8) ||
(b2 != 't' as u8 && b2 != 'T' as u8) ||
Expand All @@ -162,8 +162,8 @@ pub fn read_http_version<R: Reader>
return Err(bad_input());
}

let major = if_ok!(read_decimal(reader, |b| b == '.' as u8));
let minor = if_ok!(read_decimal(reader, expected_end));
let major = try!(read_decimal(reader, |b| b == '.' as u8));
let minor = try!(read_decimal(reader, expected_end));
Ok((major, minor))
}

Expand Down
2 changes: 1 addition & 1 deletion src/http/headers/accept_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl super::HeaderConvertible for AcceptableRanges {
NoAcceptableRanges => writer.write(bytes!("none")),
RangeUnits(ref range_units) => {
for ru in range_units.iter() {
if_ok!(writer.write(match *ru {
try!(writer.write(match *ru {
Bytes => BYTES,
OtherRangeUnit(ref ru) => ru.as_bytes(),
}));
Expand Down
6 changes: 3 additions & 3 deletions src/http/headers/content_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ impl super::HeaderConvertible for MediaType {
}

fn to_stream<W: Writer>(&self, writer: &mut W) -> IoResult<()> {
if_ok!(writer.write_token(self.type_));
if_ok!(writer.write(['/' as u8]));
if_ok!(writer.write_token(self.subtype));
try!(writer.write_token(self.type_));
try!(writer.write(['/' as u8]));
try!(writer.write_token(self.subtype));
writer.write_parameters(self.parameters)
}

Expand Down
2 changes: 1 addition & 1 deletion src/http/headers/etag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl super::HeaderConvertible for EntityTag {

fn to_stream<W: Writer>(&self, writer: &mut W) -> IoResult<()> {
if self.weak {
if_ok!(writer.write(bytes!("W/")));
try!(writer.write(bytes!("W/")));
}
writer.write_quoted_string(self.opaque_tag)
}
Expand Down
10 changes: 5 additions & 5 deletions src/http/headers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,9 @@ impl<T: CommaListHeaderConvertible> HeaderConvertible for ~[T] {
fn to_stream<W: Writer>(&self, writer: &mut W) -> IoResult<()> {
for (i, item) in self.iter().enumerate() {
if i != 0 {
if_ok!(writer.write(bytes!(", ")))
try!(writer.write(bytes!(", ")))
}
if_ok!(item.to_stream(writer))
try!(item.to_stream(writer))
}
Ok(())
}
Expand Down Expand Up @@ -915,7 +915,7 @@ macro_rules! headers_mod {
/// signal end of headers.
pub fn write_all<W: Writer>(&self, writer: &mut W) -> IoResult<()> {
for header in self.iter() {
if_ok!(header.write_header(&mut *writer));
try!(header.write_header(&mut *writer));
}
writer.write(bytes!("\r\n"))
}
Expand Down Expand Up @@ -975,13 +975,13 @@ macro_rules! headers_mod {
_ => (),
}

if_ok!(write!(&mut *writer as &mut Writer, "{}: ", match *self {
try!(write!(&mut *writer as &mut Writer, "{}: ", match *self {
$($caps_ident(_) => $output_name,)*
ExtensionHeader(..) => unreachable!(), // Already returned
}));

// FIXME: all the `h` cases satisfy HeaderConvertible, can it be simplified?
if_ok!(match *self {
try!(match *self {
$($caps_ident(ref h) => h.to_stream(writer),)*
ExtensionHeader(..) => unreachable!(), // Already returned
});
Expand Down
Loading

0 comments on commit 442c30e

Please sign in to comment.