From 43cf6caddff2c0b09901b6823d3f459bcc8570ba Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 19 Jan 2021 22:45:15 +0100 Subject: [PATCH 1/2] Simplify SocketSet lifetimes --- src/dhcp/clientv4.rs | 2 +- src/iface/ethernet.rs | 2 +- src/socket/set.rs | 26 +++++++++++++------------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/dhcp/clientv4.rs b/src/dhcp/clientv4.rs index 7e344afab..a2d33426e 100644 --- a/src/dhcp/clientv4.rs +++ b/src/dhcp/clientv4.rs @@ -94,7 +94,7 @@ impl Client { /// Instant::now() /// ); /// ``` - pub fn new<'a, 'b>(sockets: &mut SocketSet<'a, 'b>, rx_buffer: RawSocketBuffer<'b>, tx_buffer: RawSocketBuffer<'b>, now: Instant) -> Self + pub fn new<'a>(sockets: &mut SocketSet<'a>, rx_buffer: RawSocketBuffer<'a>, tx_buffer: RawSocketBuffer<'a>, 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 4aa2e7e84..c5d76454e 100644 --- a/src/iface/ethernet.rs +++ b/src/iface/ethernet.rs @@ -1731,7 +1731,7 @@ mod test { use super::{EthernetPacket, IpPacket}; fn create_loopback<'a, 'b, 'c>() -> (EthernetInterface<'static, 'b, 'c, Loopback>, - SocketSet<'static, 'a>) { + SocketSet<'a>) { // Create a basic device let device = Loopback::new(); let ip_addrs = [ diff --git a/src/socket/set.rs b/src/socket/set.rs index 883bed86d..826776348 100644 --- a/src/socket/set.rs +++ b/src/socket/set.rs @@ -27,16 +27,16 @@ impl fmt::Display for Handle { /// An extensible set of sockets. /// -/// The lifetime `'b` is used when storing a `Socket<'b>`. +/// The lifetime `'a` is used when storing a `Socket<'a>`. #[derive(Debug)] -pub struct Set<'a, 'b: 'a> { - sockets: ManagedSlice<'a, Option>> +pub struct Set<'a> { + sockets: ManagedSlice<'a, Option>> } -impl<'a, 'b: 'a> Set<'a, 'b> { +impl<'a> Set<'a> { /// Create a socket set using the provided storage. - pub fn new(sockets: SocketsT) -> Set<'a, 'b> - where SocketsT: Into>>> { + pub fn new(sockets: SocketsT) -> Set<'a> + where SocketsT: Into>>> { let sockets = sockets.into(); Set { sockets } } @@ -46,10 +46,10 @@ impl<'a, 'b: 'a> Set<'a, 'b> { /// # 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>(index: usize, slot: &mut Option>, - mut socket: Socket<'b>) -> Handle { + fn put<'a>(index: usize, slot: &mut Option>, + mut socket: Socket<'a>) -> Handle { net_trace!("[{}]: adding", index); let handle = Handle(index); socket.meta_mut().handle = handle; @@ -83,7 +83,7 @@ impl<'a, 'b: 'a> Set<'a, 'b> { /// # 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> Set<'a, 'b> { /// /// # Panics /// This function may panic if the handle does not belong to this socket set. - pub fn remove(&mut self, handle: Handle) -> Socket<'b> { + pub fn remove(&mut self, handle: Handle) -> Socket<'a> { net_trace!("[{}]: removing", handle.0); match self.sockets[handle.0].take() { Some(item) => item.socket, @@ -165,12 +165,12 @@ impl<'a, 'b: 'a> Set<'a, 'b> { } /// Iterate every socket in this set. - pub fn iter<'d>(&'d self) -> Iter<'d, 'b> { + pub fn iter<'d>(&'d self) -> Iter<'d, 'a> { Iter { lower: self.sockets.iter() } } /// Iterate every socket in this set, as SocketRef. - pub fn iter_mut<'d>(&'d mut self) -> IterMut<'d, 'b> { + pub fn iter_mut<'d>(&'d mut self) -> IterMut<'d, 'a> { IterMut { lower: self.sockets.iter_mut() } } } From 0c3867470378fe2a2261641f7270ce0c90f4d115 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 19 Jan 2021 22:50:32 +0100 Subject: [PATCH 2/2] Simplify EthernetInterface lifetimes. --- src/iface/ethernet.rs | 65 ++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/src/iface/ethernet.rs b/src/iface/ethernet.rs index c5d76454e..22b6e3e61 100644 --- a/src/iface/ethernet.rs +++ b/src/iface/ethernet.rs @@ -4,8 +4,6 @@ use core::cmp; use managed::{ManagedSlice, ManagedMap}; -#[cfg(not(feature = "proto-igmp"))] -use core::marker::PhantomData; use crate::{Error, Result}; use crate::phy::{Device, DeviceCapabilities, RxToken, TxToken}; @@ -57,9 +55,9 @@ use crate::iface::Routes; /// The network interface logically owns a number of other data structures; to avoid /// a dependency on heap allocation, it instead owns a `BorrowMut<[T]>`, which can be /// a `&mut [T]`, or `Vec` if a heap is available. -pub struct Interface<'b, 'c, 'e, DeviceT: for<'d> Device<'d>> { +pub struct Interface<'a, DeviceT: for<'d> Device<'d>> { device: DeviceT, - inner: InterfaceInner<'b, 'c, 'e>, + inner: InterfaceInner<'a>, } /// The device independent part of an Ethernet network interface. @@ -69,17 +67,15 @@ pub struct Interface<'b, 'c, 'e, DeviceT: for<'d> Device<'d>> { /// the `device` mutably until they're used, which makes it impossible to call other /// methods on the `Interface` in this time (since its `device` field is borrowed /// exclusively). However, it is still possible to call methods on its `inner` field. -struct InterfaceInner<'b, 'c, 'e> { - neighbor_cache: NeighborCache<'b>, +struct InterfaceInner<'a> { + neighbor_cache: NeighborCache<'a>, ethernet_addr: EthernetAddress, - ip_addrs: ManagedSlice<'c, IpCidr>, + ip_addrs: ManagedSlice<'a, IpCidr>, #[cfg(feature = "proto-ipv4")] any_ip: bool, - routes: Routes<'e>, + routes: Routes<'a>, #[cfg(feature = "proto-igmp")] - ipv4_multicast_groups: ManagedMap<'e, Ipv4Address, ()>, - #[cfg(not(feature = "proto-igmp"))] - _ipv4_multicast_groups: PhantomData<&'e ()>, + ipv4_multicast_groups: ManagedMap<'a, Ipv4Address, ()>, /// When to report for (all or) the next multicast group membership via IGMP #[cfg(feature = "proto-igmp")] igmp_report_state: IgmpReportState, @@ -88,22 +84,20 @@ struct InterfaceInner<'b, 'c, 'e> { /// A builder structure used for creating a Ethernet network /// interface. -pub struct InterfaceBuilder <'b, 'c, 'e, DeviceT: for<'d> Device<'d>> { +pub struct InterfaceBuilder <'a, DeviceT: for<'d> Device<'d>> { device: DeviceT, ethernet_addr: Option, - neighbor_cache: Option>, - ip_addrs: ManagedSlice<'c, IpCidr>, + neighbor_cache: Option>, + ip_addrs: ManagedSlice<'a, IpCidr>, #[cfg(feature = "proto-ipv4")] any_ip: bool, - routes: Routes<'e>, + routes: Routes<'a>, /// Does not share storage with `ipv6_multicast_groups` to avoid IPv6 size overhead. #[cfg(feature = "proto-igmp")] - ipv4_multicast_groups: ManagedMap<'e, Ipv4Address, ()>, - #[cfg(not(feature = "proto-igmp"))] - _ipv4_multicast_groups: PhantomData<&'e ()>, + ipv4_multicast_groups: ManagedMap<'a, Ipv4Address, ()>, } -impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT> +impl<'a, DeviceT> InterfaceBuilder<'a, DeviceT> where DeviceT: for<'d> Device<'d> { /// Create a builder used for creating a network interface using the /// given device and address. @@ -141,8 +135,6 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT> routes: Routes::new(ManagedMap::Borrowed(&mut [])), #[cfg(feature = "proto-igmp")] ipv4_multicast_groups: ManagedMap::Borrowed(&mut []), - #[cfg(not(feature = "proto-igmp"))] - _ipv4_multicast_groups: PhantomData, } } @@ -167,7 +159,7 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT> /// /// [ip_addrs]: struct.EthernetInterface.html#method.ip_addrs pub fn ip_addrs(mut self, ip_addrs: T) -> Self - where T: Into> + where T: Into> { let ip_addrs = ip_addrs.into(); InterfaceInner::check_ip_addrs(&ip_addrs); @@ -198,8 +190,8 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT> /// [routes]. /// /// [routes]: struct.EthernetInterface.html#method.routes - pub fn routes(mut self, routes: T) -> InterfaceBuilder<'b, 'c, 'e, DeviceT> - where T: Into> + pub fn routes(mut self, routes: T) -> InterfaceBuilder<'a, DeviceT> + where T: Into> { self.routes = routes.into(); self @@ -217,14 +209,14 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT> /// [`join_multicast_group()`]: struct.EthernetInterface.html#method.join_multicast_group #[cfg(feature = "proto-igmp")] pub fn ipv4_multicast_groups(mut self, ipv4_multicast_groups: T) -> Self - where T: Into> + where T: Into> { self.ipv4_multicast_groups = ipv4_multicast_groups.into(); self } /// Set the Neighbor Cache the interface will use. - pub fn neighbor_cache(mut self, neighbor_cache: NeighborCache<'b>) -> Self { + pub fn neighbor_cache(mut self, neighbor_cache: NeighborCache<'a>) -> Self { self.neighbor_cache = Some(neighbor_cache); self } @@ -240,7 +232,7 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT> /// /// [ethernet_addr]: #method.ethernet_addr /// [neighbor_cache]: #method.neighbor_cache - pub fn finalize(self) -> Interface<'b, 'c, 'e, DeviceT> { + pub fn finalize(self) -> Interface<'a, DeviceT> { match (self.ethernet_addr, self.neighbor_cache) { (Some(ethernet_addr), Some(neighbor_cache)) => { let device_capabilities = self.device.capabilities(); @@ -255,8 +247,6 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT> routes: self.routes, #[cfg(feature = "proto-igmp")] ipv4_multicast_groups: self.ipv4_multicast_groups, - #[cfg(not(feature = "proto-igmp"))] - _ipv4_multicast_groups: PhantomData, #[cfg(feature = "proto-igmp")] igmp_report_state: IgmpReportState::Inactive, } @@ -385,7 +375,7 @@ enum IgmpReportState { }, } -impl<'b, 'c, 'e, DeviceT> Interface<'b, 'c, 'e, DeviceT> +impl<'a, DeviceT> Interface<'a, DeviceT> where DeviceT: for<'d> Device<'d> { /// Get the Ethernet address of the interface. pub fn ethernet_addr(&self) -> EthernetAddress { @@ -495,7 +485,7 @@ impl<'b, 'c, 'e, DeviceT> Interface<'b, 'c, 'e, DeviceT> /// /// # Panics /// This function panics if any of the addresses are not unicast. - pub fn update_ip_addrs)>(&mut self, f: F) { + pub fn update_ip_addrs)>(&mut self, f: F) { f(&mut self.inner.ip_addrs); InterfaceInner::check_ip_addrs(&self.inner.ip_addrs) } @@ -511,11 +501,11 @@ impl<'b, 'c, 'e, DeviceT> Interface<'b, 'c, 'e, DeviceT> self.inner.ipv4_address() } - pub fn routes(&self) -> &Routes<'e> { + pub fn routes(&self) -> &Routes<'a> { &self.inner.routes } - pub fn routes_mut(&mut self) -> &mut Routes<'e> { + pub fn routes_mut(&mut self) -> &mut Routes<'a> { &mut self.inner.routes } @@ -752,7 +742,7 @@ impl<'b, 'c, 'e, DeviceT> Interface<'b, 'c, 'e, DeviceT> } } -impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> { +impl<'a> InterfaceInner<'a> { fn check_ethernet_addr(addr: &EthernetAddress) { if addr.is_multicast() { panic!("Ethernet address {} is not unicast", addr) @@ -1730,8 +1720,7 @@ mod test { use super::{EthernetPacket, IpPacket}; - fn create_loopback<'a, 'b, 'c>() -> (EthernetInterface<'static, 'b, 'c, Loopback>, - SocketSet<'a>) { + fn create_loopback<'a>() -> (EthernetInterface<'a, Loopback>, SocketSet<'a>) { // Create a basic device let device = Loopback::new(); let ip_addrs = [ @@ -1757,7 +1746,7 @@ mod test { } #[cfg(feature = "proto-igmp")] - fn recv_all<'b>(iface: &mut EthernetInterface<'static, 'b, 'static, Loopback>, timestamp: Instant) -> Vec> { + fn recv_all(iface: &mut EthernetInterface<'_, Loopback>, timestamp: Instant) -> Vec> { let mut pkts = Vec::new(); while let Some((rx, _tx)) = iface.device.receive() { rx.consume(timestamp, |pkt| { @@ -2533,7 +2522,7 @@ mod test { #[test] #[cfg(feature = "proto-igmp")] fn test_handle_igmp() { - fn recv_igmp<'b>(mut iface: &mut EthernetInterface<'static, 'b, 'static, Loopback>, timestamp: Instant) -> Vec<(Ipv4Repr, IgmpRepr)> { + fn recv_igmp(mut iface: &mut EthernetInterface<'_, Loopback>, timestamp: Instant) -> Vec<(Ipv4Repr, IgmpRepr)> { let checksum_caps = &iface.device.capabilities().checksum; recv_all(&mut iface, timestamp) .iter()