Skip to content

Commit 44a532d

Browse files
committed
GAT-based Device trait.
The current `'a` lifetime in the `Device` trait is essentially a workaround for lack of GATs. I'm just experimenting how this would look like, it'll have to wait until GATs are stable to go in. The main benefit is structs implementing `Device` can now borrow stuff. This wasn't possible before because the `for<'d> T: Device<'d>` bounds would essentially imply `T: 'static`.
1 parent 0dfe66b commit 44a532d

File tree

16 files changed

+109
-89
lines changed

16 files changed

+109
-89
lines changed

.github/workflows/clippy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
on:
22
push:
3-
branches: [ staging, trying ]
3+
branches: [staging, trying]
44
pull_request_target:
55

66
name: Clippy check
@@ -17,7 +17,7 @@ jobs:
1717
- uses: actions-rs/toolchain@v1
1818
with:
1919
profile: minimal
20-
toolchain: 1.53.0
20+
toolchain: nightly
2121
override: true
2222
components: clippy
2323
- uses: actions-rs/clippy-check@v1

.github/workflows/rustfmt.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
on:
22
push:
3-
branches: [ staging, trying ]
3+
branches: [staging, trying]
44
pull_request:
55

66
name: Rustfmt check
@@ -11,7 +11,7 @@ jobs:
1111
- uses: actions/checkout@v2
1212
- uses: actions-rs/toolchain@v1
1313
with:
14-
toolchain: stable
14+
toolchain: nightly
1515
profile: minimal
1616
components: rustfmt
1717
- name: Check fmt

.github/workflows/test.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
on:
22
push:
3-
branches: [ staging, trying ]
3+
branches: [staging, trying]
44
pull_request:
55

66
name: Test
@@ -20,8 +20,8 @@ jobs:
2020
# Test on stable, MSRV 1.46, and nightly.
2121
# Failure is permitted on nightly.
2222
rust:
23-
- stable
24-
- 1.56.0
23+
#- stable
24+
#- 1.56.0
2525
- nightly
2626

2727
features:
@@ -67,8 +67,8 @@ jobs:
6767
# Test on stable, MSRV 1.46, and nightly.
6868
# Failure is permitted on nightly.
6969
rust:
70-
- stable
71-
- 1.56.0
70+
#- stable
71+
#- 1.56.0
7272
- nightly
7373

7474
features:

examples/dhcp_client.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ fn main() {
9494
}
9595
}
9696

97-
fn set_ipv4_addr<DeviceT>(iface: &mut Interface<'_, DeviceT>, cidr: Ipv4Cidr)
98-
where
99-
DeviceT: for<'d> Device<'d>,
100-
{
97+
fn set_ipv4_addr<DeviceT: Device>(iface: &mut Interface<'_, DeviceT>, cidr: Ipv4Cidr) {
10198
iface.update_ip_addrs(|addrs| {
10299
let dest = addrs.iter_mut().next().unwrap();
103100
*dest = IpCidr::Ipv4(cidr);

examples/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub fn parse_middleware_options<D>(
159159
loopback: bool,
160160
) -> FaultInjector<Tracer<PcapWriter<D, Box<dyn io::Write>>>>
161161
where
162-
D: for<'a> Device<'a>,
162+
D: Device,
163163
{
164164
let drop_chance = matches
165165
.opt_str("drop-chance")

fuzz/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub fn parse_middleware_options<D>(
9393
loopback: bool,
9494
) -> FaultInjector<Tracer<PcapWriter<D, Box<dyn Write>>>>
9595
where
96-
D: for<'a> Device<'a>,
96+
D: Device,
9797
{
9898
let drop_chance = matches
9999
.opt_str("drop-chance")

src/iface/interface.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{Error, Result};
1919
/// The network interface logically owns a number of other data structures; to avoid
2020
/// a dependency on heap allocation, it instead owns a `BorrowMut<[T]>`, which can be
2121
/// a `&mut [T]`, or `Vec<T>` if a heap is available.
22-
pub struct Interface<'a, DeviceT: for<'d> Device<'d>> {
22+
pub struct Interface<'a, DeviceT: Device> {
2323
device: DeviceT,
2424
sockets: SocketSet<'a>,
2525
inner: InterfaceInner<'a>,
@@ -53,7 +53,7 @@ struct InterfaceInner<'a> {
5353
}
5454

5555
/// A builder structure used for creating a network interface.
56-
pub struct InterfaceBuilder<'a, DeviceT: for<'d> Device<'d>> {
56+
pub struct InterfaceBuilder<'a, DeviceT: Device> {
5757
device: DeviceT,
5858
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
5959
hardware_addr: Option<HardwareAddress>,
@@ -73,10 +73,7 @@ pub struct InterfaceBuilder<'a, DeviceT: for<'d> Device<'d>> {
7373
ipv4_multicast_groups: ManagedMap<'a, Ipv4Address, ()>,
7474
}
7575

76-
impl<'a, DeviceT> InterfaceBuilder<'a, DeviceT>
77-
where
78-
DeviceT: for<'d> Device<'d>,
79-
{
76+
impl<'a, DeviceT: Device> InterfaceBuilder<'a, DeviceT> {
8077
/// Create a builder used for creating a network interface using the
8178
/// given device and address.
8279
#[cfg_attr(
@@ -465,10 +462,7 @@ enum IgmpReportState {
465462
},
466463
}
467464

468-
impl<'a, DeviceT> Interface<'a, DeviceT>
469-
where
470-
DeviceT: for<'d> Device<'d>,
471-
{
465+
impl<'a, DeviceT: Device> Interface<'a, DeviceT> {
472466
/// Add a socket to the interface, and return its handle.
473467
///
474468
/// # Panics

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
),
88
deny(unused)
99
)]
10+
#![feature(generic_associated_types)]
1011

1112
//! The _smoltcp_ library is built in a layered structure, with the layers corresponding
1213
//! to the levels of API abstraction. Only the highest layers would be used by a typical

src/phy/fault_injector.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ impl State {
9494
/// adverse network conditions (such as random packet loss or corruption), or software
9595
/// or hardware limitations (such as a limited number or size of usable network buffers).
9696
#[derive(Debug)]
97-
pub struct FaultInjector<D: for<'a> Device<'a>> {
97+
pub struct FaultInjector<D: Device> {
9898
inner: D,
9999
state: RefCell<State>,
100100
config: Config,
101101
}
102102

103-
impl<D: for<'a> Device<'a>> FaultInjector<D> {
103+
impl<D: Device> FaultInjector<D> {
104104
/// Create a fault injector device, using the given random number generator seed.
105105
pub fn new(inner: D, seed: u32) -> FaultInjector<D> {
106106
let state = State {
@@ -195,12 +195,15 @@ impl<D: for<'a> Device<'a>> FaultInjector<D> {
195195
}
196196
}
197197

198-
impl<'a, D> Device<'a> for FaultInjector<D>
199-
where
200-
D: for<'b> Device<'b>,
201-
{
202-
type RxToken = RxToken<'a, <D as Device<'a>>::RxToken>;
203-
type TxToken = TxToken<'a, <D as Device<'a>>::TxToken>;
198+
impl<D: Device> Device for FaultInjector<D> {
199+
type RxToken<'a>
200+
where
201+
Self: 'a,
202+
= RxToken<'a, D::RxToken<'a>>;
203+
type TxToken<'a>
204+
where
205+
Self: 'a,
206+
= TxToken<'a, D::TxToken<'a>>;
204207

205208
fn capabilities(&self) -> DeviceCapabilities {
206209
let mut caps = self.inner.capabilities();
@@ -210,7 +213,7 @@ where
210213
caps
211214
}
212215

213-
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
216+
fn receive(&mut self) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
214217
let &mut Self {
215218
ref mut inner,
216219
ref state,
@@ -233,7 +236,7 @@ where
233236
})
234237
}
235238

236-
fn transmit(&'a mut self) -> Option<Self::TxToken> {
239+
fn transmit(&mut self) -> Option<Self::TxToken<'_>> {
237240
let &mut Self {
238241
ref mut inner,
239242
ref state,

src/phy/fuzz_injector.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ pub trait Fuzzer {
1919
#[allow(unused)]
2020
#[derive(Debug)]
2121
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
22-
pub struct FuzzInjector<D: for<'a> Device<'a>, FTx: Fuzzer, FRx: Fuzzer> {
22+
pub struct FuzzInjector<D: Device, FTx: Fuzzer, FRx: Fuzzer> {
2323
inner: D,
2424
fuzz_tx: FTx,
2525
fuzz_rx: FRx,
2626
}
2727

2828
#[allow(unused)]
29-
impl<D: for<'a> Device<'a>, FTx: Fuzzer, FRx: Fuzzer> FuzzInjector<D, FTx, FRx> {
29+
impl<D: Device, FTx: Fuzzer, FRx: Fuzzer> FuzzInjector<D, FTx, FRx> {
3030
/// Create a fuzz injector device.
3131
pub fn new(inner: D, fuzz_tx: FTx, fuzz_rx: FRx) -> FuzzInjector<D, FTx, FRx> {
3232
FuzzInjector {
@@ -42,14 +42,19 @@ impl<D: for<'a> Device<'a>, FTx: Fuzzer, FRx: Fuzzer> FuzzInjector<D, FTx, FRx>
4242
}
4343
}
4444

45-
impl<'a, D, FTx, FRx> Device<'a> for FuzzInjector<D, FTx, FRx>
45+
impl<D: Device, FTx, FRx> Device for FuzzInjector<D, FTx, FRx>
4646
where
47-
D: for<'b> Device<'b>,
48-
FTx: Fuzzer + 'a,
49-
FRx: Fuzzer + 'a,
47+
FTx: Fuzzer,
48+
FRx: Fuzzer,
5049
{
51-
type RxToken = RxToken<'a, <D as Device<'a>>::RxToken, FRx>;
52-
type TxToken = TxToken<'a, <D as Device<'a>>::TxToken, FTx>;
50+
type RxToken<'a>
51+
where
52+
Self: 'a,
53+
= RxToken<'a, D::RxToken<'a>, FRx>;
54+
type TxToken<'a>
55+
where
56+
Self: 'a,
57+
= TxToken<'a, D::TxToken<'a>, FTx>;
5358

5459
fn capabilities(&self) -> DeviceCapabilities {
5560
let mut caps = self.inner.capabilities();
@@ -59,7 +64,7 @@ where
5964
caps
6065
}
6166

62-
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
67+
fn receive(&mut self) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
6368
let &mut Self {
6469
ref mut inner,
6570
ref fuzz_rx,
@@ -78,7 +83,7 @@ where
7883
})
7984
}
8085

81-
fn transmit(&'a mut self) -> Option<Self::TxToken> {
86+
fn transmit(&mut self) -> Option<Self::TxToken<'_>> {
8287
let &mut Self {
8388
ref mut inner,
8489
fuzz_rx: _,

0 commit comments

Comments
 (0)