From c09ca370b239046bea441827a94bb70461e8b887 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 9 Jan 2021 01:52:08 +0100 Subject: [PATCH] Simplify Socket lifetimes --- src/dhcp/clientv4.rs | 3 +-- src/iface/ethernet.rs | 2 +- src/socket/icmp.rs | 24 +++++++++---------- src/socket/mod.rs | 28 +++++++++++----------- src/socket/raw.rs | 32 ++++++++++++------------- src/socket/ref_.rs | 6 ++--- src/socket/set.rs | 46 ++++++++++++++++++------------------ src/socket/tcp.rs | 4 ++-- src/socket/udp.rs | 26 ++++++++++---------- src/storage/packet_buffer.rs | 12 +++++----- 10 files changed, 91 insertions(+), 92 deletions(-) diff --git a/src/dhcp/clientv4.rs b/src/dhcp/clientv4.rs index 0828d1f..7e344af 100644 --- a/src/dhcp/clientv4.rs +++ b/src/dhcp/clientv4.rs @@ -94,8 +94,7 @@ impl Client { /// Instant::now() /// ); /// ``` - pub fn new<'a, 'b, 'c>(sockets: &mut SocketSet<'a, 'b, 'c>, rx_buffer: RawSocketBuffer<'b, 'c>, tx_buffer: RawSocketBuffer<'b, 'c>, now: Instant) -> Self - where 'b: 'c, + pub fn new<'a, 'b>(sockets: &mut SocketSet<'a, 'b>, rx_buffer: RawSocketBuffer<'b>, tx_buffer: RawSocketBuffer<'b>, now: Instant) -> Self { let raw_socket = RawSocket::new(IpVersion::Ipv4, IpProtocol::Udp, rx_buffer, tx_buffer); let raw_handle = sockets.add(raw_socket); diff --git a/src/iface/ethernet.rs b/src/iface/ethernet.rs index 7e12548..f6a1267 100644 --- a/src/iface/ethernet.rs +++ b/src/iface/ethernet.rs @@ -1727,7 +1727,7 @@ mod test { use super::{EthernetPacket, IpPacket}; fn create_loopback<'a, 'b, 'c>() -> (EthernetInterface<'static, 'b, 'c, Loopback>, - SocketSet<'static, 'a, 'b>) { + SocketSet<'static, 'a>) { // Create a basic device let device = Loopback::new(); let ip_addrs = [ diff --git a/src/socket/icmp.rs b/src/socket/icmp.rs index 2afd8b8..e6f6555 100644 --- a/src/socket/icmp.rs +++ b/src/socket/icmp.rs @@ -46,7 +46,7 @@ impl Default for Endpoint { pub type IcmpPacketMetadata = PacketMetadata; /// An ICMP packet ring buffer. -pub type IcmpSocketBuffer<'a, 'b> = PacketBuffer<'a, 'b, IpAddress>; +pub type IcmpSocketBuffer<'a> = PacketBuffer<'a, IpAddress>; /// A ICMP socket /// @@ -58,10 +58,10 @@ pub type IcmpSocketBuffer<'a, 'b> = PacketBuffer<'a, 'b, IpAddress>; /// [IcmpEndpoint]: enum.IcmpEndpoint.html /// [bind]: #method.bind #[derive(Debug)] -pub struct IcmpSocket<'a, 'b: 'a> { +pub struct IcmpSocket<'a> { pub(crate) meta: SocketMeta, - rx_buffer: IcmpSocketBuffer<'a, 'b>, - tx_buffer: IcmpSocketBuffer<'a, 'b>, + rx_buffer: IcmpSocketBuffer<'a>, + tx_buffer: IcmpSocketBuffer<'a>, /// The endpoint this socket is communicating with endpoint: Endpoint, /// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets. @@ -72,10 +72,10 @@ pub struct IcmpSocket<'a, 'b: 'a> { tx_waker: WakerRegistration, } -impl<'a, 'b> IcmpSocket<'a, 'b> { +impl<'a> IcmpSocket<'a> { /// Create an ICMP socket with the given buffers. - pub fn new(rx_buffer: IcmpSocketBuffer<'a, 'b>, - tx_buffer: IcmpSocketBuffer<'a, 'b>) -> IcmpSocket<'a, 'b> { + pub fn new(rx_buffer: IcmpSocketBuffer<'a>, + tx_buffer: IcmpSocketBuffer<'a>) -> IcmpSocket<'a> { IcmpSocket { meta: SocketMeta::default(), rx_buffer: rx_buffer, @@ -455,8 +455,8 @@ impl<'a, 'b> IcmpSocket<'a, 'b> { } } -impl<'a, 'b> Into> for IcmpSocket<'a, 'b> { - fn into(self) -> Socket<'a, 'b> { +impl<'a> Into> for IcmpSocket<'a> { + fn into(self) -> Socket<'a> { Socket::Icmp(self) } } @@ -467,12 +467,12 @@ mod tests_common { pub use crate::wire::IpAddress; pub use super::*; - pub fn buffer(packets: usize) -> IcmpSocketBuffer<'static, 'static> { + pub fn buffer(packets: usize) -> IcmpSocketBuffer<'static> { IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY; packets], vec![0; 66 * packets]) } - pub fn socket(rx_buffer: IcmpSocketBuffer<'static, 'static>, - tx_buffer: IcmpSocketBuffer<'static, 'static>) -> IcmpSocket<'static, 'static> { + pub fn socket(rx_buffer: IcmpSocketBuffer<'static>, + tx_buffer: IcmpSocketBuffer<'static>) -> IcmpSocket<'static> { IcmpSocket::new(rx_buffer, tx_buffer) } diff --git a/src/socket/mod.rs b/src/socket/mod.rs index 964e9b9..c81b2f0 100644 --- a/src/socket/mod.rs +++ b/src/socket/mod.rs @@ -82,17 +82,17 @@ pub(crate) enum PollAt { /// [AnySocket]: trait.AnySocket.html /// [SocketSet::get]: struct.SocketSet.html#method.get #[derive(Debug)] -pub enum Socket<'a, 'b: 'a> { +pub enum Socket<'a> { #[cfg(feature = "socket-raw")] - Raw(RawSocket<'a, 'b>), + Raw(RawSocket<'a>), #[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))] - Icmp(IcmpSocket<'a, 'b>), + Icmp(IcmpSocket<'a>), #[cfg(feature = "socket-udp")] - Udp(UdpSocket<'a, 'b>), + Udp(UdpSocket<'a>), #[cfg(feature = "socket-tcp")] Tcp(TcpSocket<'a>), #[doc(hidden)] - __Nonexhaustive(PhantomData<(&'a (), &'b ())>) + __Nonexhaustive(PhantomData<&'a ()>) } macro_rules! dispatch_socket { @@ -117,7 +117,7 @@ macro_rules! dispatch_socket { }; } -impl<'a, 'b> Socket<'a, 'b> { +impl<'a> Socket<'a> { /// Return the socket handle. #[inline] pub fn handle(&self) -> SocketHandle { @@ -137,22 +137,22 @@ impl<'a, 'b> Socket<'a, 'b> { } } -impl<'a, 'b> SocketSession for Socket<'a, 'b> { +impl<'a> SocketSession for Socket<'a> { fn finish(&mut self) { dispatch_socket!(mut self, |socket| socket.finish()) } } /// A conversion trait for network sockets. -pub trait AnySocket<'a, 'b>: SocketSession + Sized { - fn downcast<'c>(socket_ref: SocketRef<'c, Socket<'a, 'b>>) -> +pub trait AnySocket<'a>: SocketSession + Sized { + fn downcast<'c>(socket_ref: SocketRef<'c, Socket<'a>>) -> Option>; } macro_rules! from_socket { ($socket:ty, $variant:ident) => { - impl<'a, 'b> AnySocket<'a, 'b> for $socket { - fn downcast<'c>(ref_: SocketRef<'c, Socket<'a, 'b>>) -> + impl<'a> AnySocket<'a> for $socket { + fn downcast<'c>(ref_: SocketRef<'c, Socket<'a>>) -> Option> { match SocketRef::into_inner(ref_) { &mut Socket::$variant(ref mut socket) => Some(SocketRef::new(socket)), @@ -164,10 +164,10 @@ macro_rules! from_socket { } #[cfg(feature = "socket-raw")] -from_socket!(RawSocket<'a, 'b>, Raw); +from_socket!(RawSocket<'a>, Raw); #[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))] -from_socket!(IcmpSocket<'a, 'b>, Icmp); +from_socket!(IcmpSocket<'a>, Icmp); #[cfg(feature = "socket-udp")] -from_socket!(UdpSocket<'a, 'b>, Udp); +from_socket!(UdpSocket<'a>, Udp); #[cfg(feature = "socket-tcp")] from_socket!(TcpSocket<'a>, Tcp); diff --git a/src/socket/raw.rs b/src/socket/raw.rs index 61b771d..2b99792 100644 --- a/src/socket/raw.rs +++ b/src/socket/raw.rs @@ -19,31 +19,31 @@ use crate::wire::{Ipv6Repr, Ipv6Packet}; pub type RawPacketMetadata = PacketMetadata<()>; /// A UDP packet ring buffer. -pub type RawSocketBuffer<'a, 'b> = PacketBuffer<'a, 'b, ()>; +pub type RawSocketBuffer<'a> = PacketBuffer<'a, ()>; /// A raw IP socket. /// /// A raw socket is bound to a specific IP protocol, and owns /// transmit and receive packet buffers. #[derive(Debug)] -pub struct RawSocket<'a, 'b: 'a> { +pub struct RawSocket<'a> { pub(crate) meta: SocketMeta, ip_version: IpVersion, ip_protocol: IpProtocol, - rx_buffer: RawSocketBuffer<'a, 'b>, - tx_buffer: RawSocketBuffer<'a, 'b>, + rx_buffer: RawSocketBuffer<'a>, + tx_buffer: RawSocketBuffer<'a>, #[cfg(feature = "async")] rx_waker: WakerRegistration, #[cfg(feature = "async")] tx_waker: WakerRegistration, } -impl<'a, 'b> RawSocket<'a, 'b> { +impl<'a> RawSocket<'a> { /// Create a raw IP socket bound to the given IP version and datagram protocol, /// with the given buffers. pub fn new(ip_version: IpVersion, ip_protocol: IpProtocol, - rx_buffer: RawSocketBuffer<'a, 'b>, - tx_buffer: RawSocketBuffer<'a, 'b>) -> RawSocket<'a, 'b> { + rx_buffer: RawSocketBuffer<'a>, + tx_buffer: RawSocketBuffer<'a>) -> RawSocket<'a> { RawSocket { meta: SocketMeta::default(), ip_version, @@ -297,8 +297,8 @@ impl<'a, 'b> RawSocket<'a, 'b> { } } -impl<'a, 'b> Into> for RawSocket<'a, 'b> { - fn into(self) -> Socket<'a, 'b> { +impl<'a> Into> for RawSocket<'a> { + fn into(self) -> Socket<'a> { Socket::Raw(self) } } @@ -312,7 +312,7 @@ mod test { use crate::wire::{Ipv6Address, Ipv6Repr}; use super::*; - fn buffer(packets: usize) -> RawSocketBuffer<'static, 'static> { + fn buffer(packets: usize) -> RawSocketBuffer<'static> { RawSocketBuffer::new(vec![RawPacketMetadata::EMPTY; packets], vec![0; 48 * packets]) } @@ -320,9 +320,9 @@ mod test { mod ipv4_locals { use super::*; - pub fn socket(rx_buffer: RawSocketBuffer<'static, 'static>, - tx_buffer: RawSocketBuffer<'static, 'static>) - -> RawSocket<'static, 'static> { + pub fn socket(rx_buffer: RawSocketBuffer<'static>, + tx_buffer: RawSocketBuffer<'static>) + -> RawSocket<'static> { RawSocket::new(IpVersion::Ipv4, IpProtocol::Unknown(IP_PROTO), rx_buffer, tx_buffer) } @@ -352,9 +352,9 @@ mod test { mod ipv6_locals { use super::*; - pub fn socket(rx_buffer: RawSocketBuffer<'static, 'static>, - tx_buffer: RawSocketBuffer<'static, 'static>) - -> RawSocket<'static, 'static> { + pub fn socket(rx_buffer: RawSocketBuffer<'static>, + tx_buffer: RawSocketBuffer<'static>) + -> RawSocket<'static> { RawSocket::new(IpVersion::Ipv6, IpProtocol::Unknown(IP_PROTO), rx_buffer, tx_buffer) } diff --git a/src/socket/ref_.rs b/src/socket/ref_.rs index 7d57ccf..9e030ff 100644 --- a/src/socket/ref_.rs +++ b/src/socket/ref_.rs @@ -20,11 +20,11 @@ pub trait Session { } #[cfg(feature = "socket-raw")] -impl<'a, 'b> Session for RawSocket<'a, 'b> {} +impl<'a> Session for RawSocket<'a> {} #[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))] -impl<'a, 'b> Session for IcmpSocket<'a, 'b> {} +impl<'a> Session for IcmpSocket<'a> {} #[cfg(feature = "socket-udp")] -impl<'a, 'b> Session for UdpSocket<'a, 'b> {} +impl<'a> Session for UdpSocket<'a> {} #[cfg(feature = "socket-tcp")] impl<'a> Session for TcpSocket<'a> {} diff --git a/src/socket/set.rs b/src/socket/set.rs index 8c6a56a..8f0bc01 100644 --- a/src/socket/set.rs +++ b/src/socket/set.rs @@ -10,8 +10,8 @@ use crate::socket::TcpState; /// The only reason this struct is public is to allow the socket set storage /// to be allocated externally. #[derive(Debug)] -pub struct Item<'a, 'b: 'a> { - socket: Socket<'a, 'b>, +pub struct Item<'a> { + socket: Socket<'a>, refs: usize } @@ -27,16 +27,16 @@ impl fmt::Display for Handle { /// An extensible set of sockets. /// -/// The lifetimes `'b` and `'c` are used when storing a `Socket<'b, 'c>`. +/// The lifetime `'b` is used when storing a `Socket<'b>`. #[derive(Debug)] -pub struct Set<'a, 'b: 'a, 'c: 'a + 'b> { - sockets: ManagedSlice<'a, Option>> +pub struct Set<'a, 'b: 'a> { + sockets: ManagedSlice<'a, Option>> } -impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> { +impl<'a, 'b: 'a> Set<'a, 'b> { /// Create a socket set using the provided storage. - pub fn new(sockets: SocketsT) -> Set<'a, 'b, 'c> - where SocketsT: Into>>> { + pub fn new(sockets: SocketsT) -> Set<'a, 'b> + where SocketsT: Into>>> { let sockets = sockets.into(); Set { sockets } } @@ -46,10 +46,10 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> { /// # Panics /// This function panics if the storage is fixed-size (not a `Vec`) and is full. pub fn add(&mut self, socket: T) -> Handle - where T: Into> + where T: Into> { - fn put<'b, 'c>(index: usize, slot: &mut Option>, - mut socket: Socket<'b, 'c>) -> Handle { + fn put<'b>(index: usize, slot: &mut Option>, + mut socket: Socket<'b>) -> Handle { net_trace!("[{}]: adding", index); let handle = Handle(index); socket.meta_mut().handle = handle; @@ -83,7 +83,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> { /// # Panics /// This function may panic if the handle does not belong to this socket set /// or the socket has the wrong type. - pub fn get>(&mut self, handle: Handle) -> SocketRef { + pub fn get>(&mut self, handle: Handle) -> SocketRef { match self.sockets[handle.0].as_mut() { Some(item) => { T::downcast(SocketRef::new(&mut item.socket)) @@ -97,7 +97,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> { /// /// # Panics /// This function may panic if the handle does not belong to this socket set. - pub fn remove(&mut self, handle: Handle) -> Socket<'b, 'c> { + pub fn remove(&mut self, handle: Handle) -> Socket<'b> { net_trace!("[{}]: removing", handle.0); match self.sockets[handle.0].take() { Some(item) => item.socket, @@ -166,12 +166,12 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> { } /// Iterate every socket in this set. - pub fn iter<'d>(&'d self) -> Iter<'d, 'b, 'c> { + pub fn iter<'d>(&'d self) -> Iter<'d, 'b> { Iter { lower: self.sockets.iter() } } /// Iterate every socket in this set, as SocketRef. - pub fn iter_mut<'d>(&'d mut self) -> IterMut<'d, 'b, 'c> { + pub fn iter_mut<'d>(&'d mut self) -> IterMut<'d, 'b> { IterMut { lower: self.sockets.iter_mut() } } } @@ -180,12 +180,12 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> { /// /// This struct is created by the [iter](struct.SocketSet.html#method.iter) /// on [socket sets](struct.SocketSet.html). -pub struct Iter<'a, 'b: 'a, 'c: 'a + 'b> { - lower: slice::Iter<'a, Option>> +pub struct Iter<'a, 'b: 'a> { + lower: slice::Iter<'a, Option>> } -impl<'a, 'b: 'a, 'c: 'a + 'b> Iterator for Iter<'a, 'b, 'c> { - type Item = &'a Socket<'b, 'c>; +impl<'a, 'b: 'a> Iterator for Iter<'a, 'b> { + type Item = &'a Socket<'b>; fn next(&mut self) -> Option { while let Some(item_opt) = self.lower.next() { @@ -201,12 +201,12 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Iterator for Iter<'a, 'b, 'c> { /// /// This struct is created by the [iter_mut](struct.SocketSet.html#method.iter_mut) /// on [socket sets](struct.SocketSet.html). -pub struct IterMut<'a, 'b: 'a, 'c: 'a + 'b> { - lower: slice::IterMut<'a, Option>>, +pub struct IterMut<'a, 'b: 'a> { + lower: slice::IterMut<'a, Option>>, } -impl<'a, 'b: 'a, 'c: 'a + 'b> Iterator for IterMut<'a, 'b, 'c> { - type Item = SocketRef<'a, Socket<'b, 'c>>; +impl<'a, 'b: 'a> Iterator for IterMut<'a, 'b> { + type Item = SocketRef<'a, Socket<'b>>; fn next(&mut self) -> Option { while let Some(item_opt) = self.lower.next() { diff --git a/src/socket/tcp.rs b/src/socket/tcp.rs index 38b3173..346cd4a 100644 --- a/src/socket/tcp.rs +++ b/src/socket/tcp.rs @@ -1943,8 +1943,8 @@ impl<'a> TcpSocket<'a> { } } -impl<'a, 'b> Into> for TcpSocket<'a> { - fn into(self) -> Socket<'a, 'b> { +impl<'a> Into> for TcpSocket<'a> { + fn into(self) -> Socket<'a> { Socket::Tcp(self) } } diff --git a/src/socket/udp.rs b/src/socket/udp.rs index caf8cb4..88f5be4 100644 --- a/src/socket/udp.rs +++ b/src/socket/udp.rs @@ -13,18 +13,18 @@ use crate::socket::WakerRegistration; pub type UdpPacketMetadata = PacketMetadata; /// A UDP packet ring buffer. -pub type UdpSocketBuffer<'a, 'b> = PacketBuffer<'a, 'b, IpEndpoint>; +pub type UdpSocketBuffer<'a> = PacketBuffer<'a, IpEndpoint>; /// A User Datagram Protocol socket. /// /// A UDP socket is bound to a specific endpoint, and owns transmit and receive /// packet buffers. #[derive(Debug)] -pub struct UdpSocket<'a, 'b: 'a> { +pub struct UdpSocket<'a> { pub(crate) meta: SocketMeta, endpoint: IpEndpoint, - rx_buffer: UdpSocketBuffer<'a, 'b>, - tx_buffer: UdpSocketBuffer<'a, 'b>, + rx_buffer: UdpSocketBuffer<'a>, + tx_buffer: UdpSocketBuffer<'a>, /// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets. hop_limit: Option, #[cfg(feature = "async")] @@ -33,10 +33,10 @@ pub struct UdpSocket<'a, 'b: 'a> { tx_waker: WakerRegistration, } -impl<'a, 'b> UdpSocket<'a, 'b> { +impl<'a> UdpSocket<'a> { /// Create an UDP socket with the given buffers. - pub fn new(rx_buffer: UdpSocketBuffer<'a, 'b>, - tx_buffer: UdpSocketBuffer<'a, 'b>) -> UdpSocket<'a, 'b> { + pub fn new(rx_buffer: UdpSocketBuffer<'a>, + tx_buffer: UdpSocketBuffer<'a>) -> UdpSocket<'a> { UdpSocket { meta: SocketMeta::default(), endpoint: IpEndpoint::default(), @@ -336,8 +336,8 @@ impl<'a, 'b> UdpSocket<'a, 'b> { } } -impl<'a, 'b> Into> for UdpSocket<'a, 'b> { - fn into(self) -> Socket<'a, 'b> { +impl<'a> Into> for UdpSocket<'a> { + fn into(self) -> Socket<'a> { Socket::Udp(self) } } @@ -352,13 +352,13 @@ mod test { use crate::wire::ip::test::{MOCK_IP_ADDR_1, MOCK_IP_ADDR_2, MOCK_IP_ADDR_3}; use super::*; - fn buffer(packets: usize) -> UdpSocketBuffer<'static, 'static> { + fn buffer(packets: usize) -> UdpSocketBuffer<'static> { UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY; packets], vec![0; 16 * packets]) } - fn socket(rx_buffer: UdpSocketBuffer<'static, 'static>, - tx_buffer: UdpSocketBuffer<'static, 'static>) - -> UdpSocket<'static, 'static> { + fn socket(rx_buffer: UdpSocketBuffer<'static>, + tx_buffer: UdpSocketBuffer<'static>) + -> UdpSocket<'static> { UdpSocket::new(rx_buffer, tx_buffer) } diff --git a/src/storage/packet_buffer.rs b/src/storage/packet_buffer.rs index ab91596..be9866b 100644 --- a/src/storage/packet_buffer.rs +++ b/src/storage/packet_buffer.rs @@ -35,19 +35,19 @@ impl PacketMetadata { /// An UDP packet ring buffer. #[derive(Debug)] -pub struct PacketBuffer<'a, 'b, H: 'a> { +pub struct PacketBuffer<'a, H: 'a> { metadata_ring: RingBuffer<'a, PacketMetadata>, - payload_ring: RingBuffer<'b, u8>, + payload_ring: RingBuffer<'a, u8>, } -impl<'a, 'b, H> PacketBuffer<'a, 'b, H> { +impl<'a, H> PacketBuffer<'a, H> { /// Create a new packet buffer with the provided metadata and payload storage. /// /// Metadata storage limits the maximum _number_ of packets in the buffer and payload /// storage limits the maximum _total size_ of packets. - pub fn new(metadata_storage: MS, payload_storage: PS) -> PacketBuffer<'a, 'b, H> + pub fn new(metadata_storage: MS, payload_storage: PS) -> PacketBuffer<'a, H> where MS: Into>>, - PS: Into>, + PS: Into>, { PacketBuffer { metadata_ring: RingBuffer::new(metadata_storage), @@ -184,7 +184,7 @@ impl<'a, 'b, H> PacketBuffer<'a, 'b, H> { mod test { use super::*; - fn buffer() -> PacketBuffer<'static, 'static, ()> { + fn buffer() -> PacketBuffer<'static, ()> { PacketBuffer::new(vec![PacketMetadata::EMPTY; 4], vec![0u8; 16]) }