Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify more lifetimes #413

Merged
merged 2 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dhcp/clientv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
65 changes: 27 additions & 38 deletions src/iface/ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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();
Expand All @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = [
Expand All @@ -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| {
Expand Down Expand Up @@ -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()
Expand Down
26 changes: 13 additions & 13 deletions src/socket/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand All @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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,
Expand Down Expand Up @@ -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() }
}
}
Expand Down