Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: smoltcp-rs/smoltcp
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 53a3875452f8
Choose a base ref
...
head repository: smoltcp-rs/smoltcp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5bf64586cd29
Choose a head ref
  • 2 commits
  • 21 files changed
  • 1 contributor

Commits on Jul 27, 2017

  1. Copy the full SHA
    1a11e4d View commit details
  2. Get rid of Result<_, ()>.

    The use of this type has several drawbacks:
      * It does not allow distinguishing between different error
        conditions. In fact, we wrongly conflated some of them
        before this commit.
      * It does not allow propagation via ? and requires manual use
        of map_err, which is especially tiresome for downstream code.
      * It prevents us from expanding the set of error conditions
        even if right now we have only one.
      * It prevents us from blanket using Result<T> everywhere
        (a nitpick at most).
    
    Instead, use Result<T, Error> everywhere, and differentiate error
    conditions where applicable.
    whitequark committed Jul 27, 2017
    Copy the full SHA
    5bf6458 View commit details
18 changes: 9 additions & 9 deletions src/iface/ethernet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use managed::{Managed, ManagedSlice};

use Error;
use {Error, Result};
use phy::Device;
use wire::{EthernetAddress, EthernetProtocol, EthernetFrame};
use wire::{ArpPacket, ArpRepr, ArpOperation};
@@ -109,7 +109,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
/// handling the given set of sockets.
///
/// The timestamp is a monotonically increasing number of milliseconds.
pub fn poll(&mut self, sockets: &mut SocketSet, timestamp: u64) -> Result<(), Error> {
pub fn poll(&mut self, sockets: &mut SocketSet, timestamp: u64) -> Result<()> {
// First, transmit any outgoing packets.
loop {
if self.emit(sockets, timestamp)? { break }
@@ -140,7 +140,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
// Snoop all ARP traffic, and respond to ARP packets directed at us.
fn process_arp<'frame, T: AsRef<[u8]>>
(&mut self, eth_frame: &EthernetFrame<&'frame T>) ->
Result<Response<'frame>, Error> {
Result<Response<'frame>> {
let arp_packet = ArpPacket::new_checked(eth_frame.payload())?;
let arp_repr = ArpRepr::parse(&arp_packet)?;

@@ -189,7 +189,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
fn process_ipv4<'frame, T: AsRef<[u8]>>
(&mut self, sockets: &mut SocketSet, timestamp: u64,
eth_frame: &EthernetFrame<&'frame T>) ->
Result<Response<'frame>, Error> {
Result<Response<'frame>> {
let ipv4_packet = Ipv4Packet::new_checked(eth_frame.payload())?;
let ipv4_repr = Ipv4Repr::parse(&ipv4_packet)?;

@@ -251,7 +251,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
}

fn process_icmpv4<'frame>(ipv4_repr: Ipv4Repr, ip_payload: &'frame [u8]) ->
Result<Response<'frame>, Error> {
Result<Response<'frame>> {
let icmp_packet = Icmpv4Packet::new_checked(ip_payload)?;
let icmp_repr = Icmpv4Repr::parse(&icmp_packet)?;

@@ -284,7 +284,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {

fn process_udpv4<'frame>(sockets: &mut SocketSet, timestamp: u64,
ipv4_repr: Ipv4Repr, ip_payload: &'frame [u8]) ->
Result<Response<'frame>, Error> {
Result<Response<'frame>> {
let ip_repr = IpRepr::Ipv4(ipv4_repr);

for udp_socket in sockets.iter_mut().filter_map(
@@ -316,7 +316,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {

fn process_tcpv4<'frame>(sockets: &mut SocketSet, timestamp: u64,
ipv4_repr: Ipv4Repr, ip_payload: &'frame [u8]) ->
Result<Response<'frame>, Error> {
Result<Response<'frame>> {
let ip_repr = IpRepr::Ipv4(ipv4_repr);

for tcp_socket in sockets.iter_mut().filter_map(
@@ -359,7 +359,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
Ok(Response::Tcpv4(ipv4_reply_repr, tcp_reply_repr))
}

fn send_response(&mut self, timestamp: u64, response: Response) -> Result<(), Error> {
fn send_response(&mut self, timestamp: u64, response: Response) -> Result<()> {
macro_rules! ip_response {
($tx_buffer:ident, $frame:ident, $ip_repr:ident) => ({
let dst_hardware_addr =
@@ -428,7 +428,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
}
}

fn emit(&mut self, sockets: &mut SocketSet, timestamp: u64) -> Result<bool, Error> {
fn emit(&mut self, sockets: &mut SocketSet, timestamp: u64) -> Result<bool> {
// Borrow checker is being overly careful around closures, so we have
// to hack around that.
let src_hardware_addr = self.hardware_addr;
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -134,6 +134,9 @@ pub enum Error {
__Nonexhaustive
}

/// The result type for the networking stack.
pub type Result<T> = core::result::Result<T, Error>;

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
6 changes: 3 additions & 3 deletions src/phy/fault_injector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Error;
use {Error, Result};
use super::{DeviceLimits, Device};

// We use our own RNG to stay compatible with #![no_std].
@@ -196,7 +196,7 @@ impl<D: Device> Device for FaultInjector<D>
limits
}

fn receive(&mut self, timestamp: u64) -> Result<Self::RxBuffer, Error> {
fn receive(&mut self, timestamp: u64) -> Result<Self::RxBuffer> {
let mut buffer = self.inner.receive(timestamp)?;
if self.state.maybe(self.config.drop_pct) {
net_trace!("rx: randomly dropping a packet");
@@ -217,7 +217,7 @@ impl<D: Device> Device for FaultInjector<D>
Ok(buffer)
}

fn transmit(&mut self, timestamp: u64, length: usize) -> Result<Self::TxBuffer, Error> {
fn transmit(&mut self, timestamp: u64, length: usize) -> Result<Self::TxBuffer> {
let buffer;
if self.state.maybe(self.config.drop_pct) {
net_trace!("tx: randomly dropping a packet");
6 changes: 3 additions & 3 deletions src/phy/loopback.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ use std::collections::VecDeque;
#[cfg(feature = "collections")]
use collections::{Vec, VecDeque};

use Error;
use {Error, Result};
use super::{Device, DeviceLimits};

/// A loopback device.
@@ -39,14 +39,14 @@ impl Device for Loopback {
}
}

fn receive(&mut self, _timestamp: u64) -> Result<Self::RxBuffer, Error> {
fn receive(&mut self, _timestamp: u64) -> Result<Self::RxBuffer> {
match self.0.borrow_mut().pop_front() {
Some(packet) => Ok(packet),
None => Err(Error::Exhausted)
}
}

fn transmit(&mut self, _timestamp: u64, length: usize) -> Result<Self::TxBuffer, Error> {
fn transmit(&mut self, _timestamp: u64, length: usize) -> Result<Self::TxBuffer> {
let mut buffer = Vec::new();
buffer.resize(length, 0);
Ok(TxBuffer {
12 changes: 6 additions & 6 deletions src/phy/mod.rs
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
/*!
```rust
use std::slice;
use smoltcp::Error;
use smoltcp::{Error, Result};
use smoltcp::phy::{DeviceLimits, Device};
const TX_BUFFERS: [*mut u8; 2] = [0x10000000 as *mut u8, 0x10001000 as *mut u8];
@@ -61,7 +61,7 @@ impl Device for EthernetDevice {
limits
}
fn receive(&mut self, _timestamp: u64) -> Result<Self::RxBuffer, Error> {
fn receive(&mut self, _timestamp: u64) -> Result<Self::RxBuffer> {
if rx_full() {
let index = self.rx_next;
self.rx_next = (self.rx_next + 1) % RX_BUFFERS.len();
@@ -75,7 +75,7 @@ impl Device for EthernetDevice {
}
}
fn transmit(&mut self, _timestamp: u64, length: usize) -> Result<Self::TxBuffer, Error> {
fn transmit(&mut self, _timestamp: u64, length: usize) -> Result<Self::TxBuffer> {
if tx_empty() {
let index = self.tx_next;
self.tx_next = (self.tx_next + 1) % TX_BUFFERS.len();
@@ -104,7 +104,7 @@ impl Drop for EthernetTxBuffer {
```
*/

use Error;
use {Error, Result};

#[cfg(any(feature = "raw_socket", feature = "tap_interface"))]
mod sys;
@@ -177,12 +177,12 @@ pub trait Device {
/// It is expected that a `receive` implementation, once a packet is written to memory
/// through DMA, would gain ownership of the underlying buffer, provide it for parsing,
/// and return it to the network device once it is dropped.
fn receive(&mut self, timestamp: u64) -> Result<Self::RxBuffer, Error>;
fn receive(&mut self, timestamp: u64) -> Result<Self::RxBuffer>;

/// Transmit a frame.
///
/// It is expected that a `transmit` implementation would gain ownership of a buffer with
/// the requested length, provide it for emission, and schedule it to be read from
/// memory by the network device once it is dropped.
fn transmit(&mut self, timestamp: u64, length: usize) -> Result<Self::TxBuffer, Error>;
fn transmit(&mut self, timestamp: u64, length: usize) -> Result<Self::TxBuffer>;
}
6 changes: 3 additions & 3 deletions src/phy/pcap_writer.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use core::cell::RefCell;
use std::io::Write;
use byteorder::{ByteOrder, NativeEndian};

use Error;
use {Error, Result};
use super::{DeviceLimits, Device};

enum_with_unknown! {
@@ -136,7 +136,7 @@ impl<D: Device, S: PcapSink + Clone> Device for PcapWriter<D, S> {

fn limits(&self) -> DeviceLimits { self.lower.limits() }

fn receive(&mut self, timestamp: u64) -> Result<Self::RxBuffer, Error> {
fn receive(&mut self, timestamp: u64) -> Result<Self::RxBuffer> {
let buffer = self.lower.receive(timestamp)?;
match self.mode {
PcapMode::Both | PcapMode::RxOnly =>
@@ -146,7 +146,7 @@ impl<D: Device, S: PcapSink + Clone> Device for PcapWriter<D, S> {
Ok(buffer)
}

fn transmit(&mut self, timestamp: u64, length: usize) -> Result<Self::TxBuffer, Error> {
fn transmit(&mut self, timestamp: u64, length: usize) -> Result<Self::TxBuffer> {
let buffer = self.lower.transmit(timestamp, length)?;
Ok(TxBuffer { buffer, timestamp, sink: self.sink.clone(), mode: self.mode })
}
6 changes: 3 additions & 3 deletions src/phy/raw_socket.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use std::vec::Vec;
use std::rc::Rc;
use std::io;

use Error;
use {Error, Result};
use super::{sys, DeviceLimits, Device};

/// A socket that captures or transmits the complete frame.
@@ -40,15 +40,15 @@ impl Device for RawSocket {
}
}

fn receive(&mut self, _timestamp: u64) -> Result<Self::RxBuffer, Error> {
fn receive(&mut self, _timestamp: u64) -> Result<Self::RxBuffer> {
let mut lower = self.lower.borrow_mut();
let mut buffer = vec![0; self.mtu];
let size = lower.recv(&mut buffer[..]).unwrap();
buffer.resize(size, 0);
Ok(buffer)
}

fn transmit(&mut self, _timestamp: u64, length: usize) -> Result<Self::TxBuffer, Error> {
fn transmit(&mut self, _timestamp: u64, length: usize) -> Result<Self::TxBuffer> {
Ok(TxBuffer {
lower: self.lower.clone(),
buffer: vec![0; length]
6 changes: 3 additions & 3 deletions src/phy/tap_interface.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use std::vec::Vec;
use std::rc::Rc;
use std::io;

use Error;
use {Error, Result};
use super::{sys, DeviceLimits, Device};

/// A virtual Ethernet interface.
@@ -41,7 +41,7 @@ impl Device for TapInterface {
}
}

fn receive(&mut self, _timestamp: u64) -> Result<Self::RxBuffer, Error> {
fn receive(&mut self, _timestamp: u64) -> Result<Self::RxBuffer> {
let mut lower = self.lower.borrow_mut();
let mut buffer = vec![0; self.mtu];
match lower.recv(&mut buffer[..]) {
@@ -56,7 +56,7 @@ impl Device for TapInterface {
}
}

fn transmit(&mut self, _timestamp: u64, length: usize) -> Result<Self::TxBuffer, Error> {
fn transmit(&mut self, _timestamp: u64, length: usize) -> Result<Self::TxBuffer> {
Ok(TxBuffer {
lower: self.lower.clone(),
buffer: vec![0; length]
6 changes: 3 additions & 3 deletions src/phy/tracer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Error;
use {Error, Result};
use wire::pretty_print::{PrettyPrint, PrettyPrinter};
use super::{DeviceLimits, Device};

@@ -33,13 +33,13 @@ impl<D: Device, P: PrettyPrint> Device for Tracer<D, P> {

fn limits(&self) -> DeviceLimits { self.inner.limits() }

fn receive(&mut self, timestamp: u64) -> Result<Self::RxBuffer, Error> {
fn receive(&mut self, timestamp: u64) -> Result<Self::RxBuffer> {
let buffer = self.inner.receive(timestamp)?;
(self.writer)(timestamp, PrettyPrinter::<P>::new("<- ", &buffer));
Ok(buffer)
}

fn transmit(&mut self, timestamp: u64, length: usize) -> Result<Self::TxBuffer, Error> {
fn transmit(&mut self, timestamp: u64, length: usize) -> Result<Self::TxBuffer> {
let buffer = self.inner.transmit(timestamp, length)?;
Ok(TxBuffer { buffer, timestamp, writer: self.writer })
}
6 changes: 3 additions & 3 deletions src/socket/mod.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
//! The interface implemented by this module uses explicit buffering: you decide on the good
//! size for a buffer, allocate it, and let the networking stack use it.
use Error;
use {Error, Result};
use phy::DeviceLimits;
use wire::IpRepr;

@@ -82,8 +82,8 @@ impl<'a, 'b> Socket<'a, 'b> {
}

pub(crate) fn dispatch<F, R>(&mut self, timestamp: u64, limits: &DeviceLimits,
emit: &mut F) -> Result<R, Error>
where F: FnMut(&IpRepr, &IpPayload) -> Result<R, Error> {
emit: &mut F) -> Result<R>
where F: FnMut(&IpRepr, &IpPayload) -> Result<R> {
dispatch_socket!(self, |socket [mut]| socket.dispatch(timestamp, limits, emit))
}
}
Loading