Skip to content

Commit cc75437

Browse files
authored
Merge pull request #413 from smoltcp-rs/simplify-lifetimes-2
Simplify more lifetimes
2 parents 8aed248 + 0c38674 commit cc75437

File tree

3 files changed

+41
-52
lines changed

3 files changed

+41
-52
lines changed

src/dhcp/clientv4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl Client {
9494
/// Instant::now()
9595
/// );
9696
/// ```
97-
pub fn new<'a, 'b>(sockets: &mut SocketSet<'a, 'b>, rx_buffer: RawSocketBuffer<'b>, tx_buffer: RawSocketBuffer<'b>, now: Instant) -> Self
97+
pub fn new<'a>(sockets: &mut SocketSet<'a>, rx_buffer: RawSocketBuffer<'a>, tx_buffer: RawSocketBuffer<'a>, now: Instant) -> Self
9898
{
9999
let raw_socket = RawSocket::new(IpVersion::Ipv4, IpProtocol::Udp, rx_buffer, tx_buffer);
100100
let raw_handle = sockets.add(raw_socket);

src/iface/ethernet.rs

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use core::cmp;
66
use managed::{ManagedSlice, ManagedMap};
7-
#[cfg(not(feature = "proto-igmp"))]
8-
use core::marker::PhantomData;
97

108
use crate::{Error, Result};
119
use crate::phy::{Device, DeviceCapabilities, RxToken, TxToken};
@@ -57,9 +55,9 @@ use crate::iface::Routes;
5755
/// The network interface logically owns a number of other data structures; to avoid
5856
/// a dependency on heap allocation, it instead owns a `BorrowMut<[T]>`, which can be
5957
/// a `&mut [T]`, or `Vec<T>` if a heap is available.
60-
pub struct Interface<'b, 'c, 'e, DeviceT: for<'d> Device<'d>> {
58+
pub struct Interface<'a, DeviceT: for<'d> Device<'d>> {
6159
device: DeviceT,
62-
inner: InterfaceInner<'b, 'c, 'e>,
60+
inner: InterfaceInner<'a>,
6361
}
6462

6563
/// The device independent part of an Ethernet network interface.
@@ -69,17 +67,15 @@ pub struct Interface<'b, 'c, 'e, DeviceT: for<'d> Device<'d>> {
6967
/// the `device` mutably until they're used, which makes it impossible to call other
7068
/// methods on the `Interface` in this time (since its `device` field is borrowed
7169
/// exclusively). However, it is still possible to call methods on its `inner` field.
72-
struct InterfaceInner<'b, 'c, 'e> {
73-
neighbor_cache: NeighborCache<'b>,
70+
struct InterfaceInner<'a> {
71+
neighbor_cache: NeighborCache<'a>,
7472
ethernet_addr: EthernetAddress,
75-
ip_addrs: ManagedSlice<'c, IpCidr>,
73+
ip_addrs: ManagedSlice<'a, IpCidr>,
7674
#[cfg(feature = "proto-ipv4")]
7775
any_ip: bool,
78-
routes: Routes<'e>,
76+
routes: Routes<'a>,
7977
#[cfg(feature = "proto-igmp")]
80-
ipv4_multicast_groups: ManagedMap<'e, Ipv4Address, ()>,
81-
#[cfg(not(feature = "proto-igmp"))]
82-
_ipv4_multicast_groups: PhantomData<&'e ()>,
78+
ipv4_multicast_groups: ManagedMap<'a, Ipv4Address, ()>,
8379
/// When to report for (all or) the next multicast group membership via IGMP
8480
#[cfg(feature = "proto-igmp")]
8581
igmp_report_state: IgmpReportState,
@@ -88,22 +84,20 @@ struct InterfaceInner<'b, 'c, 'e> {
8884

8985
/// A builder structure used for creating a Ethernet network
9086
/// interface.
91-
pub struct InterfaceBuilder <'b, 'c, 'e, DeviceT: for<'d> Device<'d>> {
87+
pub struct InterfaceBuilder <'a, DeviceT: for<'d> Device<'d>> {
9288
device: DeviceT,
9389
ethernet_addr: Option<EthernetAddress>,
94-
neighbor_cache: Option<NeighborCache<'b>>,
95-
ip_addrs: ManagedSlice<'c, IpCidr>,
90+
neighbor_cache: Option<NeighborCache<'a>>,
91+
ip_addrs: ManagedSlice<'a, IpCidr>,
9692
#[cfg(feature = "proto-ipv4")]
9793
any_ip: bool,
98-
routes: Routes<'e>,
94+
routes: Routes<'a>,
9995
/// Does not share storage with `ipv6_multicast_groups` to avoid IPv6 size overhead.
10096
#[cfg(feature = "proto-igmp")]
101-
ipv4_multicast_groups: ManagedMap<'e, Ipv4Address, ()>,
102-
#[cfg(not(feature = "proto-igmp"))]
103-
_ipv4_multicast_groups: PhantomData<&'e ()>,
97+
ipv4_multicast_groups: ManagedMap<'a, Ipv4Address, ()>,
10498
}
10599

106-
impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT>
100+
impl<'a, DeviceT> InterfaceBuilder<'a, DeviceT>
107101
where DeviceT: for<'d> Device<'d> {
108102
/// Create a builder used for creating a network interface using the
109103
/// given device and address.
@@ -141,8 +135,6 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT>
141135
routes: Routes::new(ManagedMap::Borrowed(&mut [])),
142136
#[cfg(feature = "proto-igmp")]
143137
ipv4_multicast_groups: ManagedMap::Borrowed(&mut []),
144-
#[cfg(not(feature = "proto-igmp"))]
145-
_ipv4_multicast_groups: PhantomData,
146138
}
147139
}
148140

@@ -167,7 +159,7 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT>
167159
///
168160
/// [ip_addrs]: struct.EthernetInterface.html#method.ip_addrs
169161
pub fn ip_addrs<T>(mut self, ip_addrs: T) -> Self
170-
where T: Into<ManagedSlice<'c, IpCidr>>
162+
where T: Into<ManagedSlice<'a, IpCidr>>
171163
{
172164
let ip_addrs = ip_addrs.into();
173165
InterfaceInner::check_ip_addrs(&ip_addrs);
@@ -198,8 +190,8 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT>
198190
/// [routes].
199191
///
200192
/// [routes]: struct.EthernetInterface.html#method.routes
201-
pub fn routes<T>(mut self, routes: T) -> InterfaceBuilder<'b, 'c, 'e, DeviceT>
202-
where T: Into<Routes<'e>>
193+
pub fn routes<T>(mut self, routes: T) -> InterfaceBuilder<'a, DeviceT>
194+
where T: Into<Routes<'a>>
203195
{
204196
self.routes = routes.into();
205197
self
@@ -217,14 +209,14 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT>
217209
/// [`join_multicast_group()`]: struct.EthernetInterface.html#method.join_multicast_group
218210
#[cfg(feature = "proto-igmp")]
219211
pub fn ipv4_multicast_groups<T>(mut self, ipv4_multicast_groups: T) -> Self
220-
where T: Into<ManagedMap<'e, Ipv4Address, ()>>
212+
where T: Into<ManagedMap<'a, Ipv4Address, ()>>
221213
{
222214
self.ipv4_multicast_groups = ipv4_multicast_groups.into();
223215
self
224216
}
225217

226218
/// Set the Neighbor Cache the interface will use.
227-
pub fn neighbor_cache(mut self, neighbor_cache: NeighborCache<'b>) -> Self {
219+
pub fn neighbor_cache(mut self, neighbor_cache: NeighborCache<'a>) -> Self {
228220
self.neighbor_cache = Some(neighbor_cache);
229221
self
230222
}
@@ -240,7 +232,7 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT>
240232
///
241233
/// [ethernet_addr]: #method.ethernet_addr
242234
/// [neighbor_cache]: #method.neighbor_cache
243-
pub fn finalize(self) -> Interface<'b, 'c, 'e, DeviceT> {
235+
pub fn finalize(self) -> Interface<'a, DeviceT> {
244236
match (self.ethernet_addr, self.neighbor_cache) {
245237
(Some(ethernet_addr), Some(neighbor_cache)) => {
246238
let device_capabilities = self.device.capabilities();
@@ -255,8 +247,6 @@ impl<'b, 'c, 'e, DeviceT> InterfaceBuilder<'b, 'c, 'e, DeviceT>
255247
routes: self.routes,
256248
#[cfg(feature = "proto-igmp")]
257249
ipv4_multicast_groups: self.ipv4_multicast_groups,
258-
#[cfg(not(feature = "proto-igmp"))]
259-
_ipv4_multicast_groups: PhantomData,
260250
#[cfg(feature = "proto-igmp")]
261251
igmp_report_state: IgmpReportState::Inactive,
262252
}
@@ -385,7 +375,7 @@ enum IgmpReportState {
385375
},
386376
}
387377

388-
impl<'b, 'c, 'e, DeviceT> Interface<'b, 'c, 'e, DeviceT>
378+
impl<'a, DeviceT> Interface<'a, DeviceT>
389379
where DeviceT: for<'d> Device<'d> {
390380
/// Get the Ethernet address of the interface.
391381
pub fn ethernet_addr(&self) -> EthernetAddress {
@@ -495,7 +485,7 @@ impl<'b, 'c, 'e, DeviceT> Interface<'b, 'c, 'e, DeviceT>
495485
///
496486
/// # Panics
497487
/// This function panics if any of the addresses are not unicast.
498-
pub fn update_ip_addrs<F: FnOnce(&mut ManagedSlice<'c, IpCidr>)>(&mut self, f: F) {
488+
pub fn update_ip_addrs<F: FnOnce(&mut ManagedSlice<'a, IpCidr>)>(&mut self, f: F) {
499489
f(&mut self.inner.ip_addrs);
500490
InterfaceInner::check_ip_addrs(&self.inner.ip_addrs)
501491
}
@@ -511,11 +501,11 @@ impl<'b, 'c, 'e, DeviceT> Interface<'b, 'c, 'e, DeviceT>
511501
self.inner.ipv4_address()
512502
}
513503

514-
pub fn routes(&self) -> &Routes<'e> {
504+
pub fn routes(&self) -> &Routes<'a> {
515505
&self.inner.routes
516506
}
517507

518-
pub fn routes_mut(&mut self) -> &mut Routes<'e> {
508+
pub fn routes_mut(&mut self) -> &mut Routes<'a> {
519509
&mut self.inner.routes
520510
}
521511

@@ -752,7 +742,7 @@ impl<'b, 'c, 'e, DeviceT> Interface<'b, 'c, 'e, DeviceT>
752742
}
753743
}
754744

755-
impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
745+
impl<'a> InterfaceInner<'a> {
756746
fn check_ethernet_addr(addr: &EthernetAddress) {
757747
if addr.is_multicast() {
758748
panic!("Ethernet address {} is not unicast", addr)
@@ -1730,8 +1720,7 @@ mod test {
17301720

17311721
use super::{EthernetPacket, IpPacket};
17321722

1733-
fn create_loopback<'a, 'b, 'c>() -> (EthernetInterface<'static, 'b, 'c, Loopback>,
1734-
SocketSet<'static, 'a>) {
1723+
fn create_loopback<'a>() -> (EthernetInterface<'a, Loopback>, SocketSet<'a>) {
17351724
// Create a basic device
17361725
let device = Loopback::new();
17371726
let ip_addrs = [
@@ -1757,7 +1746,7 @@ mod test {
17571746
}
17581747

17591748
#[cfg(feature = "proto-igmp")]
1760-
fn recv_all<'b>(iface: &mut EthernetInterface<'static, 'b, 'static, Loopback>, timestamp: Instant) -> Vec<Vec<u8>> {
1749+
fn recv_all(iface: &mut EthernetInterface<'_, Loopback>, timestamp: Instant) -> Vec<Vec<u8>> {
17611750
let mut pkts = Vec::new();
17621751
while let Some((rx, _tx)) = iface.device.receive() {
17631752
rx.consume(timestamp, |pkt| {
@@ -2533,7 +2522,7 @@ mod test {
25332522
#[test]
25342523
#[cfg(feature = "proto-igmp")]
25352524
fn test_handle_igmp() {
2536-
fn recv_igmp<'b>(mut iface: &mut EthernetInterface<'static, 'b, 'static, Loopback>, timestamp: Instant) -> Vec<(Ipv4Repr, IgmpRepr)> {
2525+
fn recv_igmp(mut iface: &mut EthernetInterface<'_, Loopback>, timestamp: Instant) -> Vec<(Ipv4Repr, IgmpRepr)> {
25372526
let checksum_caps = &iface.device.capabilities().checksum;
25382527
recv_all(&mut iface, timestamp)
25392528
.iter()

src/socket/set.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ impl fmt::Display for Handle {
2727

2828
/// An extensible set of sockets.
2929
///
30-
/// The lifetime `'b` is used when storing a `Socket<'b>`.
30+
/// The lifetime `'a` is used when storing a `Socket<'a>`.
3131
#[derive(Debug)]
32-
pub struct Set<'a, 'b: 'a> {
33-
sockets: ManagedSlice<'a, Option<Item<'b>>>
32+
pub struct Set<'a> {
33+
sockets: ManagedSlice<'a, Option<Item<'a>>>
3434
}
3535

36-
impl<'a, 'b: 'a> Set<'a, 'b> {
36+
impl<'a> Set<'a> {
3737
/// Create a socket set using the provided storage.
38-
pub fn new<SocketsT>(sockets: SocketsT) -> Set<'a, 'b>
39-
where SocketsT: Into<ManagedSlice<'a, Option<Item<'b>>>> {
38+
pub fn new<SocketsT>(sockets: SocketsT) -> Set<'a>
39+
where SocketsT: Into<ManagedSlice<'a, Option<Item<'a>>>> {
4040
let sockets = sockets.into();
4141
Set { sockets }
4242
}
@@ -46,10 +46,10 @@ impl<'a, 'b: 'a> Set<'a, 'b> {
4646
/// # Panics
4747
/// This function panics if the storage is fixed-size (not a `Vec`) and is full.
4848
pub fn add<T>(&mut self, socket: T) -> Handle
49-
where T: Into<Socket<'b>>
49+
where T: Into<Socket<'a>>
5050
{
51-
fn put<'b>(index: usize, slot: &mut Option<Item<'b>>,
52-
mut socket: Socket<'b>) -> Handle {
51+
fn put<'a>(index: usize, slot: &mut Option<Item<'a>>,
52+
mut socket: Socket<'a>) -> Handle {
5353
net_trace!("[{}]: adding", index);
5454
let handle = Handle(index);
5555
socket.meta_mut().handle = handle;
@@ -83,7 +83,7 @@ impl<'a, 'b: 'a> Set<'a, 'b> {
8383
/// # Panics
8484
/// This function may panic if the handle does not belong to this socket set
8585
/// or the socket has the wrong type.
86-
pub fn get<T: AnySocket<'b>>(&mut self, handle: Handle) -> SocketRef<T> {
86+
pub fn get<T: AnySocket<'a>>(&mut self, handle: Handle) -> SocketRef<T> {
8787
match self.sockets[handle.0].as_mut() {
8888
Some(item) => {
8989
T::downcast(SocketRef::new(&mut item.socket))
@@ -97,7 +97,7 @@ impl<'a, 'b: 'a> Set<'a, 'b> {
9797
///
9898
/// # Panics
9999
/// This function may panic if the handle does not belong to this socket set.
100-
pub fn remove(&mut self, handle: Handle) -> Socket<'b> {
100+
pub fn remove(&mut self, handle: Handle) -> Socket<'a> {
101101
net_trace!("[{}]: removing", handle.0);
102102
match self.sockets[handle.0].take() {
103103
Some(item) => item.socket,
@@ -165,12 +165,12 @@ impl<'a, 'b: 'a> Set<'a, 'b> {
165165
}
166166

167167
/// Iterate every socket in this set.
168-
pub fn iter<'d>(&'d self) -> Iter<'d, 'b> {
168+
pub fn iter<'d>(&'d self) -> Iter<'d, 'a> {
169169
Iter { lower: self.sockets.iter() }
170170
}
171171

172172
/// Iterate every socket in this set, as SocketRef.
173-
pub fn iter_mut<'d>(&'d mut self) -> IterMut<'d, 'b> {
173+
pub fn iter_mut<'d>(&'d mut self) -> IterMut<'d, 'a> {
174174
IterMut { lower: self.sockets.iter_mut() }
175175
}
176176
}

0 commit comments

Comments
 (0)