Skip to content

Commit

Permalink
Use #[non_exhaustive] instead of manual variant
Browse files Browse the repository at this point in the history
  • Loading branch information
niclashoyer authored and Dirbaio committed Jan 9, 2021
1 parent 0d120af commit 633e7c2
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 103 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
continue-on-error: ${{ matrix.rust == 'nightly' }}
strategy:
matrix:
# Test on stable, MSRV 1.36, and nightly.
# Test on stable, MSRV 1.40, and nightly.
# Failure is permitted on nightly.
rust:
- stable
- 1.36.0
- 1.40.0
- nightly

features:
Expand Down Expand Up @@ -57,11 +57,11 @@ jobs:
continue-on-error: ${{ matrix.rust == 'nightly' }}
strategy:
matrix:
# Test on stable, MSRV 1.36, and nightly.
# Test on stable, MSRV 1.40, and nightly.
# Failure is permitted on nightly.
rust:
- stable
- 1.36.0
- 1.40.0
- nightly

features:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include complicated compile-time computations, such as macro or type tricks, eve
at cost of performance degradation.

_smoltcp_ does not need heap allocation *at all*, is [extensively documented][docs],
and compiles on stable Rust 1.36 and later.
and compiles on stable Rust 1.40 and later.

_smoltcp_ achieves [~Gbps of throughput](#examplesbenchmarkrs) when tested against
the Linux TCP stack in loopback mode.
Expand Down
15 changes: 4 additions & 11 deletions src/iface/ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,6 @@ impl<'b, 'c, 'e, DeviceT> Interface<'b, 'c, 'e, DeviceT>
Socket::Tcp(ref mut socket) =>
socket.dispatch(timestamp, &caps, |response|
respond!(IpPacket::Tcp(response))),
Socket::__Nonexhaustive(_) => unreachable!()
};

match (device_result, socket_result) {
Expand Down Expand Up @@ -800,7 +799,8 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
.filter_map(
|addr| match *addr {
IpCidr::Ipv4(cidr) => Some(cidr.address()),
_ => None,
#[cfg(feature = "proto-ipv6")]
IpCidr::Ipv6(_) => None
})
.next()
}
Expand Down Expand Up @@ -886,8 +886,6 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
Ok(None)
}
}

_ => Err(Error::Unrecognized)
}
}

Expand Down Expand Up @@ -1253,7 +1251,6 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
}
}
}
_ => return Err(Error::Unrecognized),
}
}
self.process_nxt_hdr(sockets, timestamp, ipv6_repr, hbh_repr.next_header,
Expand Down Expand Up @@ -1421,8 +1418,7 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
};
Ok(self.icmpv6_reply(ipv6_repr, icmpv6_reply_repr))
},
IpRepr::Unspecified { .. } |
IpRepr::__Nonexhaustive => Err(Error::Unaddressable),
IpRepr::Unspecified { .. } => Err(Error::Unaddressable),
}
}

Expand Down Expand Up @@ -1467,7 +1463,6 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
let dst_hardware_addr =
match arp_repr {
ArpRepr::EthernetIpv4 { target_hardware_addr, .. } => target_hardware_addr,
_ => unreachable!()
};

self.dispatch_ethernet(tx_token, timestamp, arp_repr.buffer_len(), |mut frame| {
Expand Down Expand Up @@ -1555,8 +1550,6 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
b[12], b[13],
b[14], b[15],
])),
IpAddress::__Nonexhaustive =>
unreachable!()
};
if let Some(hardware_addr) = hardware_addr {
return Ok((hardware_addr, tx_token))
Expand Down Expand Up @@ -1770,7 +1763,7 @@ mod test {
impl phy::TxToken for MockTxToken {
fn consume<R, F>(self, _: Instant, _: usize, _: F) -> Result<R>
where F: FnOnce(&mut [u8]) -> Result<R> {
Err(Error::__Nonexhaustive)
Err(Error::Unaddressable)
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub mod dhcp;

/// The error type for the networking stack.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
/// An operation cannot proceed because a buffer is empty or full.
Exhausted,
Expand Down Expand Up @@ -148,9 +149,6 @@ pub enum Error {
/// An incoming packet was recognized but contradicted internal state.
/// E.g. a TCP packet addressed to a socket that doesn't exist.
Dropped,

#[doc(hidden)]
__Nonexhaustive
}

/// The result type for the networking stack.
Expand All @@ -169,7 +167,6 @@ impl fmt::Display for Error {
Error::Fragmented => write!(f, "fragmented packet"),
Error::Malformed => write!(f, "malformed packet"),
Error::Dropped => write!(f, "dropped by socket"),
Error::__Nonexhaustive => unreachable!()
}
}
}
11 changes: 4 additions & 7 deletions src/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ The interface implemented by this module uses explicit buffering: you decide on
size for a buffer, allocate it, and let the networking stack use it.
*/

use core::marker::PhantomData;
use crate::time::Instant;

mod meta;
Expand Down Expand Up @@ -91,8 +90,6 @@ pub enum Socket<'a> {
Udp(UdpSocket<'a>),
#[cfg(feature = "socket-tcp")]
Tcp(TcpSocket<'a>),
#[doc(hidden)]
__Nonexhaustive(PhantomData<&'a ()>)
}

macro_rules! dispatch_socket {
Expand All @@ -112,7 +109,6 @@ macro_rules! dispatch_socket {
&$( $mut_ )* Socket::Udp(ref $( $mut_ )* $socket) => $code,
#[cfg(feature = "socket-tcp")]
&$( $mut_ )* Socket::Tcp(ref $( $mut_ )* $socket) => $code,
&$( $mut_ )* Socket::__Nonexhaustive(_) => unreachable!()
}
};
}
Expand Down Expand Up @@ -154,9 +150,10 @@ macro_rules! from_socket {
impl<'a> AnySocket<'a> for $socket {
fn downcast<'c>(ref_: SocketRef<'c, Socket<'a>>) ->
Option<SocketRef<'c, Self>> {
match SocketRef::into_inner(ref_) {
&mut Socket::$variant(ref mut socket) => Some(SocketRef::new(socket)),
_ => None,
if let Socket::$variant(ref mut socket) = SocketRef::into_inner(ref_) {
Some(SocketRef::new(socket))
} else {
None
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/socket/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ impl<'a> RawSocket<'a> {
Ok((IpRepr::Ipv6(ipv6_repr), packet.payload()))
}
IpVersion::Unspecified => unreachable!(),
IpVersion::__Nonexhaustive => unreachable!()
}
}

Expand Down
1 change: 0 additions & 1 deletion src/socket/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ impl<'a, 'b: 'a> Set<'a, 'b> {
} else {
socket.close()
},
Socket::__Nonexhaustive(_) => unreachable!()
}
}
if may_remove {
Expand Down
6 changes: 1 addition & 5 deletions src/wire/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ use crate::wire::{EthernetAddress, Ipv4Address};

/// A high-level representation of an Address Resolution Protocol packet.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
pub enum Repr {
/// An Ethernet and IPv4 Address Resolution Protocol packet.
EthernetIpv4 {
Expand All @@ -261,8 +262,6 @@ pub enum Repr {
target_hardware_addr: EthernetAddress,
target_protocol_addr: Ipv4Address
},
#[doc(hidden)]
__Nonexhaustive
}

impl Repr {
Expand Down Expand Up @@ -292,7 +291,6 @@ impl Repr {
pub fn buffer_len(&self) -> usize {
match *self {
Repr::EthernetIpv4 { .. } => field::TPA(6, 4).end,
Repr::__Nonexhaustive => unreachable!()
}
}

Expand All @@ -314,7 +312,6 @@ impl Repr {
packet.set_target_hardware_addr(target_hardware_addr.as_bytes());
packet.set_target_protocol_addr(target_protocol_addr.as_bytes());
},
Repr::__Nonexhaustive => unreachable!()
}
}
}
Expand Down Expand Up @@ -351,7 +348,6 @@ impl fmt::Display for Repr {
target_hardware_addr, target_protocol_addr,
operation)
},
Repr::__Nonexhaustive => unreachable!()
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/wire/icmpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ impl<T: AsRef<[u8]>> AsRef<[u8]> for Packet<T> {

/// A high-level representation of an Internet Control Message Protocol version 4 packet header.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
pub enum Repr<'a> {
EchoRequest {
ident: u16,
Expand All @@ -382,8 +383,6 @@ pub enum Repr<'a> {
header: Ipv4Repr,
data: &'a [u8]
},
#[doc(hidden)]
__Nonexhaustive
}

impl<'a> Repr<'a> {
Expand Down Expand Up @@ -446,7 +445,6 @@ impl<'a> Repr<'a> {
&Repr::DstUnreachable { header, data, .. } => {
field::UNUSED.end + header.buffer_len() + data.len()
}
&Repr::__Nonexhaustive => unreachable!()
}
}

Expand Down Expand Up @@ -483,8 +481,6 @@ impl<'a> Repr<'a> {
let payload = &mut ip_packet.into_inner()[header.buffer_len()..];
payload.copy_from_slice(&data[..])
}

Repr::__Nonexhaustive => unreachable!()
}

if checksum_caps.icmpv4.tx() {
Expand Down Expand Up @@ -526,7 +522,6 @@ impl<'a> fmt::Display for Repr<'a> {
Repr::DstUnreachable { reason, .. } =>
write!(f, "ICMPv4 destination unreachable ({})",
reason),
Repr::__Nonexhaustive => unreachable!()
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/wire/icmpv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ impl<T: AsRef<[u8]>> AsRef<[u8]> for Packet<T> {

/// A high-level representation of an Internet Control Message Protocol version 6 packet header.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
pub enum Repr<'a> {
DstUnreachable {
reason: DstUnreachable,
Expand Down Expand Up @@ -538,8 +539,6 @@ pub enum Repr<'a> {
#[cfg(feature = "ethernet")]
Ndisc(NdiscRepr<'a>),
Mld(MldRepr<'a>),
#[doc(hidden)]
__Nonexhaustive
}

impl<'a> Repr<'a> {
Expand Down Expand Up @@ -647,7 +646,6 @@ impl<'a> Repr<'a> {
&Repr::Mld(mld) => {
mld.buffer_len()
},
&Repr::__Nonexhaustive => unreachable!()
}
}

Expand Down Expand Up @@ -720,8 +718,6 @@ impl<'a> Repr<'a> {
Repr::Mld(mld) => {
mld.emit(packet)
},

Repr::__Nonexhaustive => unreachable!(),
}

if checksum_caps.icmpv6.tx() {
Expand Down
Loading

0 comments on commit 633e7c2

Please sign in to comment.