Merge pull request #413 from smoltcp-rs/simplify-lifetimes-2

Simplify more lifetimes
This commit is contained in:
Dario Nieuwenhuis 2021-01-19 23:06:34 +01:00 committed by GitHub
commit e027224b3d
3 changed files with 41 additions and 52 deletions

View File

@ -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);

View File

@ -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<T>` 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<EthernetAddress>,
neighbor_cache: Option<NeighborCache<'b>>,
ip_addrs: ManagedSlice<'c, IpCidr>,
neighbor_cache: Option<NeighborCache<'a>>,
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<T>(mut self, ip_addrs: T) -> Self
where T: Into<ManagedSlice<'c, IpCidr>>
where T: Into<ManagedSlice<'a, IpCidr>>
{
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<T>(mut self, routes: T) -> InterfaceBuilder<'b, 'c, 'e, DeviceT>
where T: Into<Routes<'e>>
pub fn routes<T>(mut self, routes: T) -> InterfaceBuilder<'a, DeviceT>
where T: Into<Routes<'a>>
{
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<T>(mut self, ipv4_multicast_groups: T) -> Self
where T: Into<ManagedMap<'e, Ipv4Address, ()>>
where T: Into<ManagedMap<'a, Ipv4Address, ()>>
{
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<F: FnOnce(&mut ManagedSlice<'c, IpCidr>)>(&mut self, f: F) {
pub fn update_ip_addrs<F: FnOnce(&mut ManagedSlice<'a, IpCidr>)>(&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<'static, '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<Vec<u8>> {
fn recv_all(iface: &mut EthernetInterface<'_, Loopback>, timestamp: Instant) -> Vec<Vec<u8>> {
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()

View File

@ -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<Item<'b>>>
pub struct Set<'a> {
sockets: ManagedSlice<'a, Option<Item<'a>>>
}
impl<'a, 'b: 'a> Set<'a, 'b> {
impl<'a> Set<'a> {
/// Create a socket set using the provided storage.
pub fn new<SocketsT>(sockets: SocketsT) -> Set<'a, 'b>
where SocketsT: Into<ManagedSlice<'a, Option<Item<'b>>>> {
pub fn new<SocketsT>(sockets: SocketsT) -> Set<'a>
where SocketsT: Into<ManagedSlice<'a, Option<Item<'a>>>> {
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<T>(&mut self, socket: T) -> Handle
where T: Into<Socket<'b>>
where T: Into<Socket<'a>>
{
fn put<'b>(index: usize, slot: &mut Option<Item<'b>>,
mut socket: Socket<'b>) -> Handle {
fn put<'a>(index: usize, slot: &mut Option<Item<'a>>,
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<T: AnySocket<'b>>(&mut self, handle: Handle) -> SocketRef<T> {
pub fn get<T: AnySocket<'a>>(&mut self, handle: Handle) -> SocketRef<T> {
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() }
}
}