From 442c30e02271eef0d0bf3099a52e04f0aeff5fc5 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Sat, 22 Feb 2014 09:52:25 +1100 Subject: [PATCH] Rust update: s/if_ok!/try!/ (mozilla/rust#12401) --- src/codegen/branchify.rs | 8 +++---- src/codegen/read_method.rs | 4 ++-- src/codegen/status.rs | 32 ++++++++++++------------- src/http/buffer.rs | 30 +++++++++++------------ src/http/client/request.rs | 14 +++++------ src/http/common.rs | 14 +++++------ src/http/headers/accept_ranges.rs | 2 +- src/http/headers/content_type.rs | 6 ++--- src/http/headers/etag.rs | 2 +- src/http/headers/mod.rs | 10 ++++---- src/http/headers/serialization_utils.rs | 14 +++++------ src/http/headers/transfer_encoding.rs | 2 +- src/http/server/response.rs | 12 +++++----- 13 files changed, 75 insertions(+), 75 deletions(-) diff --git a/src/codegen/branchify.rs b/src/codegen/branchify.rs index dee55d5..0daf281 100644 --- a/src/codegen/branchify.rs +++ b/src/codegen/branchify.rs @@ -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) => @@ -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 }),")); diff --git a/src/codegen/read_method.rs b/src/codegen/read_method.rs index 93bb753..9180033 100644 --- a/src/codegen/read_method.rs +++ b/src/codegen/read_method.rs @@ -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}; @@ -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, diff --git a/src/codegen/status.rs b/src/codegen/status.rs index 98488ee..3f53c93 100644 --- a/src/codegen/status.rs +++ b/src/codegen/status.rs @@ -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; @@ -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), } @@ -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, } } @@ -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(), } } @@ -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), } } @@ -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); } }, diff --git a/src/http/buffer.rs b/src/http/buffer.rs index 188539a..93a66e3 100644 --- a/src/http/buffer.rs +++ b/src/http/buffer.rs @@ -76,7 +76,7 @@ impl BufferedStream { pub fn read_byte(&mut self) -> IoResult { 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]) @@ -90,9 +90,9 @@ impl BufferedStream { /// 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(()) } @@ -106,7 +106,7 @@ impl Reader for BufferedStream { fn read(&mut self, buf: &mut [u8]) -> IoResult { 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)); @@ -122,16 +122,16 @@ impl Writer for BufferedStream { // 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); } @@ -140,11 +140,11 @@ impl Writer for BufferedStream { 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; } @@ -156,11 +156,11 @@ impl Writer for BufferedStream { 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; } diff --git a/src/http/client/request.rs b/src/http/client/request.rs index 9488f08..d8fcf06 100644 --- a/src/http/client/request.rs +++ b/src/http/client/request.rs @@ -110,12 +110,12 @@ impl RequestWriter { }, }; - 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 { // 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, @@ -170,7 +170,7 @@ impl RequestWriter { 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"), @@ -196,19 +196,19 @@ impl RequestWriter { 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(()) } @@ -239,7 +239,7 @@ impl RequestWriter { impl Writer for RequestWriter { 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) diff --git a/src/http/common.rs b/src/http/common.rs index c096bfd..2b64a9c 100644 --- a/src/http/common.rs +++ b/src/http/common.rs @@ -149,11 +149,11 @@ pub fn read_http_version // 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) || @@ -162,8 +162,8 @@ pub fn read_http_version 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)) } diff --git a/src/http/headers/accept_ranges.rs b/src/http/headers/accept_ranges.rs index 6e434fb..c14c6f6 100644 --- a/src/http/headers/accept_ranges.rs +++ b/src/http/headers/accept_ranges.rs @@ -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(), })); diff --git a/src/http/headers/content_type.rs b/src/http/headers/content_type.rs index 1f324e6..2ae1176 100644 --- a/src/http/headers/content_type.rs +++ b/src/http/headers/content_type.rs @@ -59,9 +59,9 @@ impl super::HeaderConvertible for MediaType { } fn to_stream(&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) } diff --git a/src/http/headers/etag.rs b/src/http/headers/etag.rs index bbd6fbe..52e4dcd 100644 --- a/src/http/headers/etag.rs +++ b/src/http/headers/etag.rs @@ -59,7 +59,7 @@ impl super::HeaderConvertible for EntityTag { fn to_stream(&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) } diff --git a/src/http/headers/mod.rs b/src/http/headers/mod.rs index a8c0a06..b9b432b 100644 --- a/src/http/headers/mod.rs +++ b/src/http/headers/mod.rs @@ -576,9 +576,9 @@ impl HeaderConvertible for ~[T] { fn to_stream(&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(()) } @@ -915,7 +915,7 @@ macro_rules! headers_mod { /// signal end of headers. pub fn write_all(&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")) } @@ -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 }); diff --git a/src/http/headers/serialization_utils.rs b/src/http/headers/serialization_utils.rs index 50a8ace..95195bc 100644 --- a/src/http/headers/serialization_utils.rs +++ b/src/http/headers/serialization_utils.rs @@ -64,26 +64,26 @@ pub trait WriterUtil: Writer { } fn write_quoted_string(&mut self, s: &str) -> IoResult<()> { - if_ok!(self.write(['"' as u8])); + try!(self.write(['"' as u8])); for b in s.bytes() { if b == '\\' as u8 || b == '"' as u8 { - if_ok!(self.write(['\\' as u8])); + try!(self.write(['\\' as u8])); } - if_ok!(self.write([b])); + try!(self.write([b])); } self.write(['"' as u8]) } fn write_parameter(&mut self, k: &str, v: &str) -> IoResult<()> { - if_ok!(self.write(k.as_bytes())); - if_ok!(self.write(['=' as u8])); + try!(self.write(k.as_bytes())); + try!(self.write(['=' as u8])); self.write_maybe_quoted_string(v) } fn write_parameters(&mut self, parameters: &[(K, V)]) -> IoResult<()> { for &(ref k, ref v) in parameters.iter() { - if_ok!(self.write([';' as u8])); - if_ok!(self.write_parameter(k.as_slice(), v.as_slice())); + try!(self.write([';' as u8])); + try!(self.write_parameter(k.as_slice(), v.as_slice())); } Ok(()) } diff --git a/src/http/headers/transfer_encoding.rs b/src/http/headers/transfer_encoding.rs index a110f89..9afd27d 100644 --- a/src/http/headers/transfer_encoding.rs +++ b/src/http/headers/transfer_encoding.rs @@ -41,7 +41,7 @@ impl super::HeaderConvertible for TransferCoding { match *self { Chunked => writer.write(bytes!("chunked")), TransferExtension(ref token, ref parameters) => { - if_ok!(writer.write_token(*token)); + try!(writer.write_token(*token)); writer.write_parameters(*parameters) } } diff --git a/src/http/server/response.rs b/src/http/server/response.rs index 160e5dd..51385e5 100644 --- a/src/http/server/response.rs +++ b/src/http/server/response.rs @@ -45,7 +45,7 @@ impl<'a> ResponseWriter<'a> { self.headers.content_type = Some(content_type); let cbytes = content.as_bytes(); self.headers.content_length = Some(cbytes.len()); - if_ok!(self.write_headers()); + try!(self.write_headers()); self.write(cbytes) } @@ -76,7 +76,7 @@ impl<'a> ResponseWriter<'a> { // XXX: Rust's current lack of statement-duration lifetime handling prevents this from being // one statement ("error: borrowed value does not live long enough") let s = format!("HTTP/1.1 {}\r\n", self.status.to_str()); - if_ok!(self.writer.write(s.as_bytes())); + try!(self.writer.write(s.as_bytes())); // FIXME: this is not an impressive way of handling it, but so long as chunked is the only // transfer-coding we want to deal with it's tolerable. However, it is *meant* to be an @@ -88,20 +88,20 @@ impl<'a> ResponseWriter<'a> { } else { self.headers.transfer_encoding = None; } - if_ok!(self.headers.write_all(&mut *self.writer)); + try!(self.headers.write_all(&mut *self.writer)); self.headers_written = true; if self.headers.content_length == None { // Flush so that the chunked body stuff can start working correctly. TODO: don't // actually flush it entirely, or else it'll send the headers in a separate TCP packet, // which is bad for performance. - if_ok!(self.writer.flush()); + try!(self.writer.flush()); self.writer.writing_chunked_body = true; } Ok(()) } pub fn finish_response(&mut self) -> IoResult<()> { - if_ok!(self.writer.finish_response()); + try!(self.writer.finish_response()); // Ensure that we switch away from chunked in case another request comes on the same socket self.writer.writing_chunked_body = false; Ok(()) @@ -112,7 +112,7 @@ impl<'a> Writer for ResponseWriter<'a> { fn write(&mut self, buf: &[u8]) -> IoResult<()> { if !self.headers_written { - if_ok!(self.write_headers()); + try!(self.write_headers()); } self.writer.write(buf) }