Skip to content

Commit e0a00fa

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 02535a7 commit e0a00fa

11 files changed

+91
-75
lines changed

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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ let iface = builder.finalize(&mut device);
528528
/// [neighbor_cache]: #method.neighbor_cache
529529
pub fn finalize<D>(self, device: &mut D) -> Interface<'a>
530530
where
531-
D: for<'d> Device<'d> + ?Sized,
531+
D: Device + ?Sized,
532532
{
533533
let caps = device.capabilities();
534534

@@ -868,7 +868,7 @@ impl<'a> Interface<'a> {
868868
timestamp: Instant,
869869
) -> Result<bool>
870870
where
871-
D: for<'d> Device<'d> + ?Sized,
871+
D: Device + ?Sized,
872872
{
873873
self.inner.now = timestamp;
874874

@@ -910,7 +910,7 @@ impl<'a> Interface<'a> {
910910
timestamp: Instant,
911911
) -> Result<bool>
912912
where
913-
D: for<'d> Device<'d> + ?Sized,
913+
D: Device + ?Sized,
914914
{
915915
self.inner.now = timestamp;
916916

@@ -1010,7 +1010,7 @@ impl<'a> Interface<'a> {
10101010
sockets: &mut SocketSet<'_>,
10111011
) -> Result<bool>
10121012
where
1013-
D: for<'d> Device<'d> + ?Sized,
1013+
D: Device + ?Sized,
10141014
{
10151015
self.inner.now = timestamp;
10161016

@@ -1117,7 +1117,7 @@ impl<'a> Interface<'a> {
11171117

11181118
fn socket_ingress<D>(&mut self, device: &mut D, sockets: &mut SocketSet<'_>) -> bool
11191119
where
1120-
D: for<'d> Device<'d> + ?Sized,
1120+
D: Device + ?Sized,
11211121
{
11221122
let mut processed_any = false;
11231123
let Self {
@@ -1173,7 +1173,7 @@ impl<'a> Interface<'a> {
11731173

11741174
fn socket_egress<D>(&mut self, device: &mut D, sockets: &mut SocketSet<'_>) -> bool
11751175
where
1176-
D: for<'d> Device<'d> + ?Sized,
1176+
D: Device + ?Sized,
11771177
{
11781178
let Self {
11791179
inner,
@@ -1283,7 +1283,7 @@ impl<'a> Interface<'a> {
12831283
#[cfg(feature = "proto-igmp")]
12841284
fn igmp_egress<D>(&mut self, device: &mut D) -> Result<bool>
12851285
where
1286-
D: for<'d> Device<'d> + ?Sized,
1286+
D: Device + ?Sized,
12871287
{
12881288
match self.inner.igmp_report_state {
12891289
IgmpReportState::ToSpecificQuery {
@@ -1344,7 +1344,7 @@ impl<'a> Interface<'a> {
13441344
#[cfg(feature = "proto-ipv4-fragmentation")]
13451345
fn ipv4_egress<D>(&mut self, device: &mut D) -> Result<bool>
13461346
where
1347-
D: for<'d> Device<'d> + ?Sized,
1347+
D: Device + ?Sized,
13481348
{
13491349
// Reset the buffer when we transmitted everything.
13501350
if self.out_packets.ipv4_out_packet.finished() {
@@ -1384,7 +1384,7 @@ impl<'a> Interface<'a> {
13841384
#[cfg(feature = "proto-sixlowpan-fragmentation")]
13851385
fn sixlowpan_egress<D>(&mut self, device: &mut D) -> Result<bool>
13861386
where
1387-
D: for<'d> Device<'d> + ?Sized,
1387+
D: Device + ?Sized,
13881388
{
13891389
// Reset the buffer when we transmitted everything.
13901390
if self.out_packets.sixlowpan_out_packet.finished() {

src/phy/fault_injector.rs

Lines changed: 11 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,13 @@ 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> = RxToken<'a, D::RxToken<'a>>
200+
where
201+
Self: 'a;
202+
type TxToken<'a> = TxToken<'a, D::TxToken<'a>>
203+
where
204+
Self: 'a;
204205

205206
fn capabilities(&self) -> DeviceCapabilities {
206207
let mut caps = self.inner.capabilities();
@@ -210,7 +211,7 @@ where
210211
caps
211212
}
212213

213-
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
214+
fn receive(&mut self) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
214215
let &mut Self {
215216
ref mut inner,
216217
ref state,
@@ -233,7 +234,7 @@ where
233234
})
234235
}
235236

236-
fn transmit(&'a mut self) -> Option<Self::TxToken> {
237+
fn transmit(&mut self) -> Option<Self::TxToken<'_>> {
237238
let &mut Self {
238239
ref mut inner,
239240
ref state,

src/phy/fuzz_injector.rs

Lines changed: 13 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,17 @@ 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> = RxToken<'a, D::RxToken<'a>, FRx>
51+
where
52+
Self: 'a;
53+
type TxToken<'a> = TxToken<'a, D::TxToken<'a>, FTx>
54+
where
55+
Self: 'a;
5356

5457
fn capabilities(&self) -> DeviceCapabilities {
5558
let mut caps = self.inner.capabilities();
@@ -59,7 +62,7 @@ where
5962
caps
6063
}
6164

62-
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
65+
fn receive(&mut self) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
6366
let &mut Self {
6467
ref mut inner,
6568
ref fuzz_rx,
@@ -78,7 +81,7 @@ where
7881
})
7982
}
8083

81-
fn transmit(&'a mut self) -> Option<Self::TxToken> {
84+
fn transmit(&mut self) -> Option<Self::TxToken<'_>> {
8285
let &mut Self {
8386
ref mut inner,
8487
fuzz_rx: _,

src/phy/loopback.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ impl Loopback {
2929
}
3030
}
3131

32-
impl<'a> Device<'a> for Loopback {
33-
type RxToken = RxToken;
34-
type TxToken = TxToken<'a>;
32+
impl Device for Loopback {
33+
type RxToken<'a> = RxToken;
34+
type TxToken<'a> = TxToken<'a>;
3535

3636
fn capabilities(&self) -> DeviceCapabilities {
3737
DeviceCapabilities {
@@ -41,7 +41,7 @@ impl<'a> Device<'a> for Loopback {
4141
}
4242
}
4343

44-
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
44+
fn receive(&mut self) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
4545
self.queue.pop_front().map(move |buffer| {
4646
let rx = RxToken { buffer };
4747
let tx = TxToken {
@@ -51,7 +51,7 @@ impl<'a> Device<'a> for Loopback {
5151
})
5252
}
5353

54-
fn transmit(&'a mut self) -> Option<Self::TxToken> {
54+
fn transmit(&mut self) -> Option<Self::TxToken<'_>> {
5555
Some(TxToken {
5656
queue: &mut self.queue,
5757
})

src/phy/mod.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ impl<'a> StmPhy {
3838
}
3939
}
4040
41-
impl<'a> phy::Device<'a> for StmPhy {
42-
type RxToken = StmPhyRxToken<'a>;
43-
type TxToken = StmPhyTxToken<'a>;
41+
impl phy::Device for StmPhy {
42+
type RxToken<'a> = StmPhyRxToken<'a> where Self: 'a;
43+
type TxToken<'a> = StmPhyTxToken<'a> where Self: 'a;
4444
45-
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
45+
fn receive(&mut self) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
4646
Some((StmPhyRxToken(&mut self.rx_buffer[..]),
4747
StmPhyTxToken(&mut self.tx_buffer[..])))
4848
}
4949
50-
fn transmit(&'a mut self) -> Option<Self::TxToken> {
50+
fn transmit(&mut self) -> Option<Self::TxToken<'_>> {
5151
Some(StmPhyTxToken(&mut self.tx_buffer[..]))
5252
}
5353
@@ -308,20 +308,24 @@ impl Default for Medium {
308308
/// The interface is based on _tokens_, which are types that allow to receive/transmit a
309309
/// single packet. The `receive` and `transmit` functions only construct such tokens, the
310310
/// real sending/receiving operation are performed when the tokens are consumed.
311-
pub trait Device<'a> {
312-
type RxToken: RxToken + 'a;
313-
type TxToken: TxToken + 'a;
311+
pub trait Device {
312+
type RxToken<'a>: RxToken
313+
where
314+
Self: 'a;
315+
type TxToken<'a>: TxToken
316+
where
317+
Self: 'a;
314318

315319
/// Construct a token pair consisting of one receive token and one transmit token.
316320
///
317321
/// The additional transmit token makes it possible to generate a reply packet based
318322
/// on the contents of the received packet. For example, this makes it possible to
319323
/// handle arbitrarily large ICMP echo ("ping") requests, where the all received bytes
320324
/// need to be sent back, without heap allocation.
321-
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)>;
325+
fn receive(&mut self) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)>;
322326

323327
/// Construct a transmit token.
324-
fn transmit(&'a mut self) -> Option<Self::TxToken>;
328+
fn transmit(&mut self) -> Option<Self::TxToken<'_>>;
325329

326330
/// Get a description of device capabilities.
327331
fn capabilities(&self) -> DeviceCapabilities;

src/phy/pcap_writer.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ impl<T: Write> PcapSink for T {
118118
#[derive(Debug)]
119119
pub struct PcapWriter<D, S>
120120
where
121-
D: for<'a> Device<'a>,
121+
D: Device,
122122
S: PcapSink,
123123
{
124124
lower: D,
125125
sink: RefCell<S>,
126126
mode: PcapMode,
127127
}
128128

129-
impl<D: for<'a> Device<'a>, S: PcapSink> PcapWriter<D, S> {
129+
impl<D: Device, S: PcapSink> PcapWriter<D, S> {
130130
/// Creates a packet capture writer.
131131
pub fn new(lower: D, mut sink: S, mode: PcapMode) -> PcapWriter<D, S> {
132132
let medium = lower.capabilities().medium;
@@ -162,19 +162,22 @@ impl<D: for<'a> Device<'a>, S: PcapSink> PcapWriter<D, S> {
162162
}
163163
}
164164

165-
impl<'a, D, S> Device<'a> for PcapWriter<D, S>
165+
impl<D: Device, S> Device for PcapWriter<D, S>
166166
where
167-
D: for<'b> Device<'b>,
168-
S: PcapSink + 'a,
167+
S: PcapSink,
169168
{
170-
type RxToken = RxToken<'a, <D as Device<'a>>::RxToken, S>;
171-
type TxToken = TxToken<'a, <D as Device<'a>>::TxToken, S>;
169+
type RxToken<'a> = RxToken<'a, D::RxToken<'a>, S>
170+
where
171+
Self: 'a;
172+
type TxToken<'a> = TxToken<'a, D::TxToken<'a>, S>
173+
where
174+
Self: 'a;
172175

173176
fn capabilities(&self) -> DeviceCapabilities {
174177
self.lower.capabilities()
175178
}
176179

177-
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
180+
fn receive(&mut self) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
178181
let sink = &self.sink;
179182
let mode = self.mode;
180183
self.lower.receive().map(move |(rx_token, tx_token)| {
@@ -192,7 +195,7 @@ where
192195
})
193196
}
194197

195-
fn transmit(&'a mut self) -> Option<Self::TxToken> {
198+
fn transmit(&mut self) -> Option<Self::TxToken<'_>> {
196199
let sink = &self.sink;
197200
let mode = self.mode;
198201
self.lower

src/phy/raw_socket.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ impl RawSocket {
5454
}
5555
}
5656

57-
impl<'a> Device<'a> for RawSocket {
58-
type RxToken = RxToken;
59-
type TxToken = TxToken;
57+
impl Device for RawSocket {
58+
type RxToken<'a> = RxToken
59+
where
60+
Self: 'a;
61+
type TxToken<'a> = TxToken
62+
where
63+
Self: 'a;
6064

6165
fn capabilities(&self) -> DeviceCapabilities {
6266
DeviceCapabilities {
@@ -66,7 +70,7 @@ impl<'a> Device<'a> for RawSocket {
6670
}
6771
}
6872

69-
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
73+
fn receive(&mut self) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
7074
let mut lower = self.lower.borrow_mut();
7175
let mut buffer = vec![0; self.mtu];
7276
match lower.recv(&mut buffer[..]) {
@@ -83,7 +87,7 @@ impl<'a> Device<'a> for RawSocket {
8387
}
8488
}
8589

86-
fn transmit(&'a mut self) -> Option<Self::TxToken> {
90+
fn transmit(&mut self) -> Option<Self::TxToken<'_>> {
8791
Some(TxToken {
8892
lower: self.lower.clone(),
8993
})

0 commit comments

Comments
 (0)