Ver Fonte

try! → ?

whitequark há 7 anos atrás
pai
commit
1c1489e5fb

+ 15 - 15
src/iface/ethernet.rs

@@ -112,12 +112,12 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
 
         // First, transmit any outgoing packets.
         loop {
-            if try!(self.emit(sockets, timestamp)) { break }
+            if self.emit(sockets, timestamp)? { break }
         }
 
         // Now, receive any incoming packets.
-        let rx_buffer = try!(self.device.receive());
-        let eth_frame = try!(EthernetFrame::new_checked(&rx_buffer));
+        let rx_buffer = self.device.receive()?;
+        let eth_frame = EthernetFrame::new_checked(&rx_buffer)?;
 
         if !eth_frame.dst_addr().is_broadcast() &&
                 eth_frame.dst_addr() != self.hardware_addr {
@@ -128,8 +128,8 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
         match eth_frame.ethertype() {
             // Snoop all ARP traffic, and respond to ARP packets directed at us.
             EthernetProtocol::Arp => {
-                let arp_packet = try!(ArpPacket::new_checked(eth_frame.payload()));
-                match try!(ArpRepr::parse(&arp_packet)) {
+                let arp_packet = ArpPacket::new_checked(eth_frame.payload())?;
+                match ArpRepr::parse(&arp_packet)? {
                     // Respond to ARP requests aimed at us, and fill the ARP cache
                     // from all ARP requests, including gratuitous.
                     ArpRepr::EthernetIpv4 {
@@ -170,8 +170,8 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
 
             // Handle IP packets directed at us.
             EthernetProtocol::Ipv4 => {
-                let ipv4_packet = try!(Ipv4Packet::new_checked(eth_frame.payload()));
-                let ipv4_repr = try!(Ipv4Repr::parse(&ipv4_packet));
+                let ipv4_packet = Ipv4Packet::new_checked(eth_frame.payload())?;
+                let ipv4_repr = Ipv4Repr::parse(&ipv4_packet)?;
 
                 // Fill the ARP cache from IP header.
                 if ipv4_repr.src_addr.is_unicast() && eth_frame.src_addr().is_unicast() {
@@ -195,8 +195,8 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
 
                     // Respond to ICMP packets.
                     Ipv4Repr { protocol: IpProtocol::Icmp, src_addr, dst_addr, .. } => {
-                        let icmp_packet = try!(Icmpv4Packet::new_checked(ipv4_packet.payload()));
-                        let icmp_repr = try!(Icmpv4Repr::parse(&icmp_packet));
+                        let icmp_packet = Icmpv4Packet::new_checked(ipv4_packet.payload())?;
+                        let icmp_repr = Icmpv4Repr::parse(&icmp_packet)?;
                         match icmp_repr {
                             // Respond to echo requests.
                             Icmpv4Repr::EchoRequest {
@@ -251,7 +251,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
                         }
 
                         if !handled && protocol == IpProtocol::Tcp {
-                            let tcp_packet = try!(TcpPacket::new_checked(ipv4_packet.payload()));
+                            let tcp_packet = TcpPacket::new_checked(ipv4_packet.payload())?;
                             if !tcp_packet.rst() {
                                 let tcp_reply_repr = TcpRepr {
                                     src_port:     tcp_packet.dst_port(),
@@ -311,7 +311,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
 
                 let frame_len = EthernetFrame::<&[u8]>::buffer_len($ip_repr.buffer_len() +
                                                                    $ip_repr.payload_len);
-                $tx_buffer = try!(self.device.transmit(frame_len));
+                $tx_buffer = self.device.transmit(frame_len)?;
                 $frame = EthernetFrame::new_checked(&mut $tx_buffer)
                                        .expect("transmit frame too small");
                 $frame.set_src_addr(self.hardware_addr);
@@ -327,7 +327,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
         match response {
             Response::Arp(repr) => {
                 let tx_len = EthernetFrame::<&[u8]>::buffer_len(repr.buffer_len());
-                let mut tx_buffer = try!(self.device.transmit(tx_len));
+                let mut tx_buffer = self.device.transmit(tx_len)?;
                 let mut frame = EthernetFrame::new_checked(&mut tx_buffer)
                                               .expect("transmit frame too small");
                 frame.set_src_addr(self.hardware_addr);
@@ -383,13 +383,13 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
         let mut nothing_to_transmit = true;
         for socket in sockets.iter_mut() {
             let result = socket.dispatch(timestamp, &limits, &mut |repr, payload| {
-                let repr = try!(repr.lower(src_protocol_addrs));
+                let repr = repr.lower(src_protocol_addrs)?;
 
                 match arp_cache.lookup(&repr.dst_addr()) {
                     Some(dst_hardware_addr) => {
                         let tx_len = EthernetFrame::<&[u8]>::buffer_len(repr.buffer_len() +
                                                                         payload.buffer_len());
-                        let mut tx_buffer = try!(device.transmit(tx_len));
+                        let mut tx_buffer = device.transmit(tx_len)?;
                         let mut frame = EthernetFrame::new_checked(&mut tx_buffer)
                                                       .expect("transmit frame too small");
                         frame.set_src_addr(src_hardware_addr);
@@ -420,7 +420,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
                         };
 
                         let tx_len = EthernetFrame::<&[u8]>::buffer_len(payload.buffer_len());
-                        let mut tx_buffer = try!(device.transmit(tx_len));
+                        let mut tx_buffer = device.transmit(tx_len)?;
                         let mut frame = EthernetFrame::new_checked(&mut tx_buffer)
                                                       .expect("transmit frame too small");
                         frame.set_src_addr(src_hardware_addr);

+ 2 - 2
src/phy/fault_injector.rs

@@ -230,7 +230,7 @@ impl<T: Device> Device for FaultInjector<T>
     }
 
     fn receive(&mut self) -> Result<Self::RxBuffer, Error> {
-        let mut buffer = try!(self.lower.receive());
+        let mut buffer = self.lower.receive()?;
         if self.state.maybe(self.config.drop_pct) {
             net_trace!("rx: randomly dropping a packet");
             return Err(Error::Exhausted)
@@ -262,7 +262,7 @@ impl<T: Device> Device for FaultInjector<T>
             net_trace!("tx: dropping a packet because of rate limiting");
             buffer = None;
         } else {
-            buffer = Some(try!(self.lower.transmit(length)));
+            buffer = Some(self.lower.transmit(length)?);
         }
         Ok(TxBuffer {
             buffer: buffer,

+ 3 - 3
src/phy/raw_socket.rs

@@ -19,9 +19,9 @@ impl RawSocket {
     /// This requires superuser privileges or a corresponding capability bit
     /// set on the executable.
     pub fn new(name: &str) -> io::Result<RawSocket> {
-        let mut lower = try!(sys::RawSocketDesc::new(name));
-        try!(lower.bind_interface());
-        let mtu = try!(lower.interface_mtu());
+        let mut lower = sys::RawSocketDesc::new(name)?;
+        lower.bind_interface()?;
+        let mtu = lower.interface_mtu()?;
         Ok(RawSocket {
             lower: Rc::new(RefCell::new(lower)),
             mtu:   mtu

+ 1 - 1
src/phy/sys/raw_socket.rs

@@ -31,7 +31,7 @@ impl RawSocketDesc {
         let sockaddr = libc::sockaddr_ll {
             sll_family:   libc::AF_PACKET as u16,
             sll_protocol: imp::ETH_P_ALL.to_be() as u16,
-            sll_ifindex:  try!(ifreq_ioctl(self.lower, &mut self.ifreq, imp::SIOCGIFINDEX)),
+            sll_ifindex:  ifreq_ioctl(self.lower, &mut self.ifreq, imp::SIOCGIFINDEX)?,
             sll_hatype:   1,
             sll_pkttype:  0,
             sll_halen:    6,

+ 2 - 2
src/phy/tap_interface.rs

@@ -20,8 +20,8 @@ impl TapInterface {
     /// no special privileges are needed. Otherwise, this requires superuser privileges
     /// or a corresponding capability set on the executable.
     pub fn new(name: &str) -> io::Result<TapInterface> {
-        let mut lower = try!(sys::TapInterfaceDesc::new(name));
-        try!(lower.attach_interface());
+        let mut lower = sys::TapInterfaceDesc::new(name)?;
+        lower.attach_interface()?;
         Ok(TapInterface {
             lower: Rc::new(RefCell::new(lower)),
             mtu:   1536 // FIXME: get the real value somehow

+ 2 - 2
src/phy/tracer.rs

@@ -46,13 +46,13 @@ impl<T: Device, U: PrettyPrint> Device for Tracer<T, U> {
     fn limits(&self) -> DeviceLimits { self.lower.limits() }
 
     fn receive(&mut self) -> Result<Self::RxBuffer, Error> {
-        let buffer = try!(self.lower.receive());
+        let buffer = self.lower.receive()?;
         (self.writer)(PrettyPrinter::<U>::new("<- ", &buffer));
         Ok(buffer)
     }
 
     fn transmit(&mut self, length: usize) -> Result<Self::TxBuffer, Error> {
-        let buffer = try!(self.lower.transmit(length));
+        let buffer = self.lower.transmit(length)?;
         Ok(TxBuffer {
             buffer:  buffer,
             writer:  self.writer

+ 6 - 6
src/socket/tcp.rs

@@ -571,7 +571,7 @@ impl<'a> TcpSocket<'a> {
     ///
     /// See also [send](#method.send).
     pub fn send_slice(&mut self, data: &[u8]) -> Result<usize, ()> {
-        let buffer = try!(self.send(data.len()));
+        let buffer = self.send(data.len())?;
         let data = &data[..buffer.len()];
         buffer.copy_from_slice(data);
         Ok(buffer.len())
@@ -608,7 +608,7 @@ impl<'a> TcpSocket<'a> {
     ///
     /// See also [recv](#method.recv).
     pub fn recv_slice(&mut self, data: &mut [u8]) -> Result<usize, ()> {
-        let buffer = try!(self.recv(data.len()));
+        let buffer = self.recv(data.len())?;
         let data = &mut data[..buffer.len()];
         data.copy_from_slice(buffer);
         Ok(buffer.len())
@@ -636,8 +636,8 @@ impl<'a> TcpSocket<'a> {
 
         if ip_repr.protocol() != IpProtocol::Tcp { return Err(Error::Rejected) }
 
-        let packet = try!(TcpPacket::new_checked(&payload[..ip_repr.payload_len()]));
-        let repr = try!(TcpRepr::parse(&packet, &ip_repr.src_addr(), &ip_repr.dst_addr()));
+        let packet = TcpPacket::new_checked(&payload[..ip_repr.payload_len()])?;
+        let repr = TcpRepr::parse(&packet, &ip_repr.src_addr(), &ip_repr.dst_addr())?;
 
         // Reject packets with a wrong destination.
         if self.local_endpoint.port != repr.dst_port { return Err(Error::Rejected) }
@@ -1101,7 +1101,7 @@ impl<'a> TcpSocket<'a> {
                 protocol:     IpProtocol::Tcp,
                 payload_len:  repr.buffer_len()
             };
-            let ip_repr = try!(ip_repr.lower(&[]));
+            let ip_repr = ip_repr.lower(&[])?;
 
             let mut max_segment_size = limits.max_transmission_unit;
             max_segment_size -= header_len;
@@ -1240,7 +1240,7 @@ mod test {
             buffer.resize(payload.buffer_len(), 0);
             payload.emit(&ip_repr, &mut buffer[..]);
             let packet = TcpPacket::new(&buffer[..]);
-            let repr = try!(TcpRepr::parse(&packet, &ip_repr.src_addr(), &ip_repr.dst_addr()));
+            let repr = TcpRepr::parse(&packet, &ip_repr.src_addr(), &ip_repr.dst_addr())?;
             trace!("recv: {}", repr);
             Ok(f(Ok(repr)))
         });

+ 8 - 8
src/socket/udp.rs

@@ -109,7 +109,7 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
     /// This function returns `Err(())` if the size is greater than what
     /// the transmit buffer can accomodate.
     pub fn send(&mut self, size: usize, endpoint: IpEndpoint) -> Result<&mut [u8], ()> {
-        let packet_buf = try!(self.tx_buffer.enqueue());
+        let packet_buf = self.tx_buffer.enqueue()?;
         packet_buf.endpoint = endpoint;
         packet_buf.size = size;
         net_trace!("[{}]{}:{}: buffer to send {} octets",
@@ -122,7 +122,7 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
     ///
     /// See also [send](#method.send).
     pub fn send_slice(&mut self, data: &[u8], endpoint: IpEndpoint) -> Result<usize, ()> {
-        let buffer = try!(self.send(data.len(), endpoint));
+        let buffer = self.send(data.len(), endpoint)?;
         let data = &data[..buffer.len()];
         buffer.copy_from_slice(data);
         Ok(data.len())
@@ -133,7 +133,7 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
     ///
     /// This function returns `Err(())` if the receive buffer is empty.
     pub fn recv(&mut self) -> Result<(&[u8], IpEndpoint), ()> {
-        let packet_buf = try!(self.rx_buffer.dequeue());
+        let packet_buf = self.rx_buffer.dequeue()?;
         net_trace!("[{}]{}:{}: receive {} buffered octets",
                    self.debug_id, self.endpoint,
                    packet_buf.endpoint, packet_buf.size);
@@ -145,7 +145,7 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
     ///
     /// See also [recv](#method.recv).
     pub fn recv_slice(&mut self, data: &mut [u8]) -> Result<(usize, IpEndpoint), ()> {
-        let (buffer, endpoint) = try!(self.recv());
+        let (buffer, endpoint) = self.recv()?;
         data[..buffer.len()].copy_from_slice(buffer);
         Ok((buffer.len(), endpoint))
     }
@@ -155,15 +155,15 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
                    payload: &[u8]) -> Result<(), Error> {
         if ip_repr.protocol() != IpProtocol::Udp { return Err(Error::Rejected) }
 
-        let packet = try!(UdpPacket::new_checked(&payload[..ip_repr.payload_len()]));
-        let repr = try!(UdpRepr::parse(&packet, &ip_repr.src_addr(), &ip_repr.dst_addr()));
+        let packet = UdpPacket::new_checked(&payload[..ip_repr.payload_len()])?;
+        let repr = UdpRepr::parse(&packet, &ip_repr.src_addr(), &ip_repr.dst_addr())?;
 
         if repr.dst_port != self.endpoint.port { return Err(Error::Rejected) }
         if !self.endpoint.addr.is_unspecified() {
             if self.endpoint.addr != ip_repr.dst_addr() { return Err(Error::Rejected) }
         }
 
-        let packet_buf = try!(self.rx_buffer.enqueue().map_err(|()| Error::Exhausted));
+        let packet_buf = self.rx_buffer.enqueue().map_err(|()| Error::Exhausted)?;
         packet_buf.endpoint = IpEndpoint { addr: ip_repr.src_addr(), port: repr.src_port };
         packet_buf.size = repr.payload.len();
         packet_buf.as_mut()[..repr.payload.len()].copy_from_slice(repr.payload);
@@ -177,7 +177,7 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
     pub fn dispatch<F, R>(&mut self, _timestamp: u64, _limits: &DeviceLimits,
                           emit: &mut F) -> Result<R, Error>
             where F: FnMut(&IpRepr, &IpPayload) -> Result<R, Error> {
-        let packet_buf = try!(self.tx_buffer.dequeue().map_err(|()| Error::Exhausted));
+        let packet_buf = self.tx_buffer.dequeue().map_err(|()| Error::Exhausted)?;
         net_trace!("[{}]{}:{}: sending {} octets",
                    self.debug_id, self.endpoint,
                    packet_buf.endpoint, packet_buf.size);

+ 9 - 9
src/wire/arp.rs

@@ -73,7 +73,7 @@ impl<T: AsRef<[u8]>> Packet<T> {
     /// [check_len]: #method.check_len
     pub fn new_checked(buffer: T) -> Result<Packet<T>, Error> {
         let packet = Self::new(buffer);
-        try!(packet.check_len());
+        packet.check_len()?;
         Ok(packet)
     }
 
@@ -317,14 +317,14 @@ impl<T: AsRef<[u8]>> fmt::Display for Packet<T> {
         match Repr::parse(self) {
             Ok(repr) => write!(f, "{}", repr),
             _ => {
-                try!(write!(f, "ARP (unrecognized)"));
-                try!(write!(f, " htype={:?} ptype={:?} hlen={:?} plen={:?} op={:?}",
-                            self.hardware_type(), self.protocol_type(),
-                            self.hardware_len(), self.protocol_len(),
-                            self.operation()));
-                try!(write!(f, " sha={:?} spa={:?} tha={:?} tpa={:?}",
-                            self.source_hardware_addr(), self.source_protocol_addr(),
-                            self.target_hardware_addr(), self.target_protocol_addr()));
+                write!(f, "ARP (unrecognized)")?;
+                write!(f, " htype={:?} ptype={:?} hlen={:?} plen={:?} op={:?}",
+                       self.hardware_type(), self.protocol_type(),
+                       self.hardware_len(), self.protocol_len(),
+                       self.operation())?;
+                write!(f, " sha={:?} spa={:?} tha={:?} tpa={:?}",
+                       self.source_hardware_addr(), self.source_protocol_addr(),
+                       self.target_hardware_addr(), self.target_protocol_addr())?;
                 Ok(())
             }
         }

+ 2 - 2
src/wire/ethernet.rs

@@ -102,7 +102,7 @@ impl<T: AsRef<[u8]>> Frame<T> {
     /// [check_len]: #method.check_len
     pub fn new_checked(buffer: T) -> Result<Frame<T>, Error> {
         let packet = Self::new(buffer);
-        try!(packet.check_len());
+        packet.check_len()?;
         Ok(packet)
     }
 
@@ -213,7 +213,7 @@ impl<T: AsRef<[u8]>> PrettyPrint for Frame<T> {
             Err(err)  => return write!(f, "{}({})\n", indent, err),
             Ok(frame) => frame
         };
-        try!(write!(f, "{}{}\n", indent, frame));
+        write!(f, "{}{}\n", indent, frame)?;
         indent.increase();
 
         match frame.ethertype() {

+ 7 - 8
src/wire/icmpv4.rs

@@ -195,7 +195,7 @@ impl<T: AsRef<[u8]>> Packet<T> {
     /// [check_len]: #method.check_len
     pub fn new_checked(buffer: T) -> Result<Packet<T>, Error> {
         let packet = Self::new(buffer);
-        try!(packet.check_len());
+        packet.check_len()?;
         Ok(packet)
     }
 
@@ -403,7 +403,7 @@ impl<'a> Repr<'a> {
             },
 
             (Message::DstUnreachable, code) => {
-                let ip_packet = try!(Ipv4Packet::new_checked(packet.data()));
+                let ip_packet = Ipv4Packet::new_checked(packet.data())?;
 
                 let payload = &packet.data()[ip_packet.header_len() as usize..];
                 // RFC 792 requires exactly eight bytes to be returned.
@@ -483,14 +483,13 @@ impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> {
         match Repr::parse(self) {
             Ok(repr) => write!(f, "{}", repr),
             Err(err) => {
-                try!(write!(f, "ICMPv4 ({})", err));
-                try!(write!(f, " type={:?}", self.msg_type()));
+                write!(f, "ICMPv4 ({})", err)?;
+                write!(f, " type={:?}", self.msg_type())?;
                 match self.msg_type() {
                     Message::DstUnreachable =>
-                        try!(write!(f, " code={:?}", DstUnreachable::from(self.msg_code()))),
-                    _ => try!(write!(f, " code={}", self.msg_code()))
+                        write!(f, " code={:?}", DstUnreachable::from(self.msg_code())),
+                    _ => write!(f, " code={}", self.msg_code())
                 }
-                Ok(())
             }
         }
     }
@@ -522,7 +521,7 @@ impl<T: AsRef<[u8]>> PrettyPrint for Packet<T> {
             Err(err)   => return write!(f, "{}({})\n", indent, err),
             Ok(packet) => packet
         };
-        try!(write!(f, "{}{}\n", indent, packet));
+        write!(f, "{}{}\n", indent, packet)?;
 
         indent.increase();
         match packet.msg_type() {

+ 1 - 1
src/wire/ip.rs

@@ -227,7 +227,7 @@ impl IpRepr {
                     }
                 }
                 Ok(IpRepr::Ipv4(Ipv4Repr {
-                    src_addr:    try!(src_addr.ok_or(Error::Unaddressable)),
+                    src_addr:    src_addr.ok_or(Error::Unaddressable)?,
                     dst_addr:    dst_addr,
                     protocol:    protocol,
                     payload_len: payload_len

+ 14 - 14
src/wire/ipv4.rs

@@ -108,7 +108,7 @@ impl<T: AsRef<[u8]>> Packet<T> {
     /// [check_len]: #method.check_len
     pub fn new_checked(buffer: T) -> Result<Packet<T>, Error> {
         let packet = Self::new(buffer);
-        try!(packet.check_len());
+        packet.check_len()?;
         Ok(packet)
     }
 
@@ -456,33 +456,33 @@ impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> {
         match Repr::parse(self) {
             Ok(repr) => write!(f, "{}", repr),
             Err(err) => {
-                try!(write!(f, "IPv4 ({})", err));
-                try!(write!(f, " src={} dst={} proto={} ttl={}",
-                            self.src_addr(), self.dst_addr(), self.protocol(), self.ttl()));
+                write!(f, "IPv4 ({})", err)?;
+                write!(f, " src={} dst={} proto={} ttl={}",
+                       self.src_addr(), self.dst_addr(), self.protocol(), self.ttl())?;
                 if self.version() != 4 {
-                    try!(write!(f, " ver={}", self.version()))
+                    write!(f, " ver={}", self.version())?;
                 }
                 if self.header_len() != 20 {
-                    try!(write!(f, " hlen={}", self.header_len()))
+                    write!(f, " hlen={}", self.header_len())?;
                 }
                 if self.dscp() != 0 {
-                    try!(write!(f, " dscp={}", self.dscp()))
+                    write!(f, " dscp={}", self.dscp())?;
                 }
                 if self.ecn() != 0 {
-                    try!(write!(f, " ecn={}", self.ecn()))
+                    write!(f, " ecn={}", self.ecn())?;
                 }
-                try!(write!(f, " tlen={}", self.total_len()));
+                write!(f, " tlen={}", self.total_len())?;
                 if self.dont_frag() {
-                    try!(write!(f, " df"))
+                    write!(f, " df")?;
                 }
                 if self.more_frags() {
-                    try!(write!(f, " mf"))
+                    write!(f, " mf")?;
                 }
                 if self.frag_offset() != 0 {
-                    try!(write!(f, " off={}", self.frag_offset()))
+                    write!(f, " off={}", self.frag_offset())?;
                 }
                 if self.more_frags() || self.frag_offset() != 0 {
-                    try!(write!(f, " id={}", self.ident()))
+                    write!(f, " id={}", self.ident())?;
                 }
                 Ok(())
             }
@@ -505,7 +505,7 @@ impl<T: AsRef<[u8]>> PrettyPrint for Packet<T> {
         let (ip_repr, payload) = match Packet::new_checked(buffer) {
             Err(err) => return write!(f, "{}({})\n", indent, err),
             Ok(ip_packet) => {
-                try!(write!(f, "{}{}\n", indent, ip_packet));
+                write!(f, "{}{}\n", indent, ip_packet)?;
                 match Repr::parse(&ip_packet) {
                     Err(_) => return Ok(()),
                     Ok(ip_repr) => (ip_repr, &ip_packet.payload()[..ip_repr.payload_len])

+ 29 - 30
src/wire/tcp.rs

@@ -112,7 +112,7 @@ impl<T: AsRef<[u8]>> Packet<T> {
     /// [check_len]: #method.check_len
     pub fn new_checked(buffer: T) -> Result<Packet<T>, Error> {
         let packet = Self::new(buffer);
-        try!(packet.check_len());
+        packet.check_len()?;
         Ok(packet)
     }
 
@@ -728,29 +728,28 @@ impl<'a> Repr<'a> {
 impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         // Cannot use Repr::parse because we don't have the IP addresses.
-        try!(write!(f, "TCP src={} dst={}",
-                    self.src_port(), self.dst_port()));
-        if self.syn() { try!(write!(f, " syn")) }
-        if self.fin() { try!(write!(f, " fin")) }
-        if self.rst() { try!(write!(f, " rst")) }
-        if self.psh() { try!(write!(f, " psh")) }
-        if self.ece() { try!(write!(f, " ece")) }
-        if self.cwr() { try!(write!(f, " cwr")) }
-        if self.ns()  { try!(write!(f, " ns" )) }
-        try!(write!(f, " seq={}", self.seq_number()));
+        write!(f, "TCP src={} dst={}",
+               self.src_port(), self.dst_port())?;
+        if self.syn() { write!(f, " syn")? }
+        if self.fin() { write!(f, " fin")? }
+        if self.rst() { write!(f, " rst")? }
+        if self.psh() { write!(f, " psh")? }
+        if self.ece() { write!(f, " ece")? }
+        if self.cwr() { write!(f, " cwr")? }
+        if self.ns()  { write!(f, " ns" )? }
+        write!(f, " seq={}", self.seq_number())?;
         if self.ack() {
-            try!(write!(f, " ack={}", self.ack_number()));
+            write!(f, " ack={}", self.ack_number())?;
         }
-        try!(write!(f, " win={}", self.window_len()));
+        write!(f, " win={}", self.window_len())?;
         if self.urg() {
-            try!(write!(f, " urg={}", self.urgent_at()))
+            write!(f, " urg={}", self.urgent_at())?;
         }
-        try!(write!(f, " len={}", self.payload().len()));
+        write!(f, " len={}", self.payload().len())?;
 
         let header_len = self.header_len() as usize;
         if header_len < field::URGENT.end {
-            try!(write!(f, " {}", Error::Truncated));
-            return Ok(())
+            return write!(f, " ({})", Error::Truncated)
         }
 
         let mut options = self.options();
@@ -764,11 +763,11 @@ impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> {
                 TcpOption::EndOfList => break,
                 TcpOption::NoOperation => (),
                 TcpOption::MaxSegmentSize(value) =>
-                    try!(write!(f, " mss={}", value)),
+                    write!(f, " mss={}", value)?,
                 TcpOption::WindowScale(value) =>
-                    try!(write!(f, " ws={}", value)),
+                    write!(f, " ws={}", value)?,
                 TcpOption::Unknown { kind, .. } =>
-                    try!(write!(f, " opt({})", kind)),
+                    write!(f, " opt({})", kind)?,
             }
             options = next_options;
         }
@@ -778,22 +777,22 @@ impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> {
 
 impl<'a> fmt::Display for Repr<'a> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(f, "TCP src={} dst={}",
-                    self.src_port, self.dst_port));
+        write!(f, "TCP src={} dst={}",
+               self.src_port, self.dst_port)?;
         match self.control {
-            Control::Syn => try!(write!(f, " syn")),
-            Control::Fin => try!(write!(f, " fin")),
-            Control::Rst => try!(write!(f, " rst")),
+            Control::Syn => write!(f, " syn")?,
+            Control::Fin => write!(f, " fin")?,
+            Control::Rst => write!(f, " rst")?,
             Control::None => ()
         }
-        try!(write!(f, " seq={}", self.seq_number));
+        write!(f, " seq={}", self.seq_number)?;
         if let Some(ack_number) = self.ack_number {
-            try!(write!(f, " ack={}", ack_number));
+            write!(f, " ack={}", ack_number)?;
         }
-        try!(write!(f, " win={}", self.window_len));
-        try!(write!(f, " len={}", self.payload.len()));
+        write!(f, " win={}", self.window_len)?;
+        write!(f, " len={}", self.payload.len())?;
         if let Some(max_seg_size) = self.max_seg_size {
-            try!(write!(f, " mss={}", max_seg_size));
+            write!(f, " mss={}", max_seg_size)?;
         }
         Ok(())
     }

+ 1 - 1
src/wire/udp.rs

@@ -38,7 +38,7 @@ impl<T: AsRef<[u8]>> Packet<T> {
     /// [check_len]: #method.check_len
     pub fn new_checked(buffer: T) -> Result<Packet<T>, Error> {
         let packet = Self::new(buffer);
-        try!(packet.check_len());
+        packet.check_len()?;
         Ok(packet)
     }