Skip to content

Commit f403862

Browse files
bors[bot]Dirbaio
andauthored
Merge #572
572: GAT-based Device trait. r=Dirbaio a=Dirbaio 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`. Co-authored-by: Dario Nieuwenhuis <[email protected]>
2 parents 202e9b4 + 13cc7f8 commit f403862

24 files changed

+119
-111
lines changed

.github/workflows/test.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ jobs:
2020
# Test on stable, MSRV, and nightly.
2121
# Failure is permitted on nightly.
2222
rust:
23-
- stable
24-
- 1.61.0
23+
#- stable # TODO: enable again when "stable" is 1.66 or higher.
24+
- 1.65.0
2525
- nightly
2626

2727
features:
@@ -64,8 +64,8 @@ jobs:
6464
# Test on stable, MSRV, and nightly.
6565
# Failure is permitted on nightly.
6666
rust:
67-
- stable
68-
- 1.61.0
67+
#- stable # TODO: enable again when "stable" is 1.66 or higher.
68+
- 1.65.0
6969
- nightly
7070

7171
features:

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Remove IpAddress::Unspecified
1212
- When sending packets with a raw socket, the source IP address is sent unmodified (it was previously replaced with the interface's address if it was unspecified).
1313
- Fix enable `defmt/alloc` if `alloc` or `std` is enabled.
14-
- Minimum Supported Rust Version (MSRV) **bumped** from 1.56 to 1.60
14+
- Minimum Supported Rust Version (MSRV) **bumped** from 1.56 to 1.65
1515

1616
## [0.8.1] - 2022-05-12
1717

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "smoltcp"
33
version = "0.8.1"
44
edition = "2018"
5-
rust-version = "1.61"
5+
rust-version = "1.65"
66
authors = ["whitequark <[email protected]>"]
77
description = "A TCP/IP stack designed for bare-metal, real-time systems without a heap."
88
documentation = "https://docs.rs/smoltcp/"

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include complicated compile-time computations, such as macro or type tricks, eve
1111
at cost of performance degradation.
1212

1313
_smoltcp_ does not need heap allocation *at all*, is [extensively documented][docs],
14-
and compiles on stable Rust 1.61 and later.
14+
and compiles on stable Rust 1.65 and later.
1515

1616
_smoltcp_ achieves [~Gbps of throughput](#examplesbenchmarkrs) when tested against
1717
the Linux TCP stack in loopback mode.

examples/utils.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ pub fn parse_options(options: &Options, free: Vec<&str>) -> Matches {
8484
free.join(" ")
8585
);
8686
print!("{}", options.usage(&brief));
87-
process::exit(if matches.free.len() != free.len() {
88-
1
89-
} else {
90-
0
91-
})
87+
process::exit((matches.free.len() != free.len()) as _);
9288
}
9389
matches
9490
}
@@ -159,7 +155,7 @@ pub fn parse_middleware_options<D>(
159155
loopback: bool,
160156
) -> FaultInjector<Tracer<PcapWriter<D, Box<dyn io::Write>>>>
161157
where
162-
D: for<'a> Device<'a>,
158+
D: Device,
163159
{
164160
let drop_chance = matches
165161
.opt_str("drop-chance")

fuzz/utils.rs

+1-1
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/fragmentation.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl<'a, K: Eq + Ord + Clone + Copy> PacketAssemblerSet<'a, K> {
364364
/// - Returns [`Error::PacketAssemblerSetKeyNotFound`] when the key was not found in the set.
365365
pub(crate) fn get_packet_assembler_mut(&mut self, key: &K) -> Result<&mut PacketAssembler<'a>> {
366366
if let Some(i) = self.index_buffer.get(key) {
367-
Ok(&mut self.packet_buffer[*i as usize])
367+
Ok(&mut self.packet_buffer[*i])
368368
} else {
369369
Err(Error::PacketAssemblerSetKeyNotFound)
370370
}
@@ -379,7 +379,7 @@ impl<'a, K: Eq + Ord + Clone + Copy> PacketAssemblerSet<'a, K> {
379379
/// - Returns [`Error::PacketAssemblerIncomplete`] when the fragments assembler was empty or not fully assembled.
380380
pub(crate) fn get_assembled_packet(&mut self, key: &K) -> Result<&[u8]> {
381381
if let Some(i) = self.index_buffer.get(key) {
382-
let p = self.packet_buffer[*i as usize].assemble()?;
382+
let p = self.packet_buffer[*i].assemble()?;
383383
self.index_buffer.remove(key);
384384
Ok(p)
385385
} else {
@@ -392,10 +392,7 @@ impl<'a, K: Eq + Ord + Clone + Copy> PacketAssemblerSet<'a, K> {
392392
loop {
393393
let mut key = None;
394394
for (k, i) in self.index_buffer.iter() {
395-
if matches!(
396-
self.packet_buffer[*i as usize].assembler,
397-
AssemblerState::NotInit
398-
) {
395+
if matches!(self.packet_buffer[*i].assembler, AssemblerState::NotInit) {
399396
key = Some(*k);
400397
break;
401398
}
@@ -416,7 +413,7 @@ impl<'a, K: Eq + Ord + Clone + Copy> PacketAssemblerSet<'a, K> {
416413
F: Fn(&mut PacketAssembler<'_>) -> Result<bool>,
417414
{
418415
for (_, i) in &mut self.index_buffer.iter() {
419-
let frag = &mut self.packet_buffer[*i as usize];
416+
let frag = &mut self.packet_buffer[*i];
420417
if f(frag)? {
421418
frag.mark_discarded();
422419
}

src/iface/interface/ipv4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl<'a> InterfaceInner<'a> {
507507
}
508508

509509
tx_buffer[repr.buffer_len()..][..payload_len].copy_from_slice(
510-
&buffer[*frag_offset as usize + repr.buffer_len() as usize..][..payload_len],
510+
&buffer[*frag_offset as usize + repr.buffer_len()..][..payload_len],
511511
);
512512

513513
// Update the frag offset for the next fragment.

src/iface/interface/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ let iface = builder.finalize(&mut device);
563563
/// [neighbor_cache]: #method.neighbor_cache
564564
pub fn finalize<D>(self, device: &mut D) -> Interface<'a>
565565
where
566-
D: for<'d> Device<'d> + ?Sized,
566+
D: Device + ?Sized,
567567
{
568568
let caps = device.capabilities();
569569

@@ -905,7 +905,7 @@ impl<'a> Interface<'a> {
905905
timestamp: Instant,
906906
) -> Result<bool>
907907
where
908-
D: for<'d> Device<'d> + ?Sized,
908+
D: Device + ?Sized,
909909
{
910910
self.inner.now = timestamp;
911911

@@ -947,7 +947,7 @@ impl<'a> Interface<'a> {
947947
timestamp: Instant,
948948
) -> Result<bool>
949949
where
950-
D: for<'d> Device<'d> + ?Sized,
950+
D: Device + ?Sized,
951951
{
952952
self.inner.now = timestamp;
953953

@@ -1047,7 +1047,7 @@ impl<'a> Interface<'a> {
10471047
sockets: &mut SocketSet<'_>,
10481048
) -> Result<bool>
10491049
where
1050-
D: for<'d> Device<'d> + ?Sized,
1050+
D: Device + ?Sized,
10511051
{
10521052
self.inner.now = timestamp;
10531053

@@ -1152,7 +1152,7 @@ impl<'a> Interface<'a> {
11521152

11531153
fn socket_ingress<D>(&mut self, device: &mut D, sockets: &mut SocketSet<'_>) -> bool
11541154
where
1155-
D: for<'d> Device<'d> + ?Sized,
1155+
D: Device + ?Sized,
11561156
{
11571157
let mut processed_any = false;
11581158
let Self {
@@ -1208,7 +1208,7 @@ impl<'a> Interface<'a> {
12081208

12091209
fn socket_egress<D>(&mut self, device: &mut D, sockets: &mut SocketSet<'_>) -> bool
12101210
where
1211-
D: for<'d> Device<'d> + ?Sized,
1211+
D: Device + ?Sized,
12121212
{
12131213
let Self {
12141214
inner,
@@ -1318,7 +1318,7 @@ impl<'a> Interface<'a> {
13181318
#[cfg(feature = "proto-igmp")]
13191319
fn igmp_egress<D>(&mut self, device: &mut D) -> Result<bool>
13201320
where
1321-
D: for<'d> Device<'d> + ?Sized,
1321+
D: Device + ?Sized,
13221322
{
13231323
match self.inner.igmp_report_state {
13241324
IgmpReportState::ToSpecificQuery {
@@ -1384,7 +1384,7 @@ impl<'a> Interface<'a> {
13841384
#[cfg(feature = "proto-ipv4-fragmentation")]
13851385
fn ipv4_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.ipv4_out_packet.finished() {
@@ -1422,7 +1422,7 @@ impl<'a> Interface<'a> {
14221422
#[cfg(feature = "proto-sixlowpan-fragmentation")]
14231423
fn sixlowpan_egress<D>(&mut self, device: &mut D) -> Result<bool>
14241424
where
1425-
D: for<'d> Device<'d> + ?Sized,
1425+
D: Device + ?Sized,
14261426
{
14271427
// Reset the buffer when we transmitted everything.
14281428
if self.out_packets.sixlowpan_out_packet.finished() {

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
//!
6666
//! # Minimum Supported Rust Version (MSRV)
6767
//!
68-
//! This crate is guaranteed to compile on stable Rust 1.61 and up with any valid set of features.
68+
//! This crate is guaranteed to compile on stable Rust 1.65 and up with any valid set of features.
6969
//! It *might* compile on older versions but that may change in any new patch release.
7070
//!
7171
//! The exception is when using the `defmt` feature, in which case `defmt`'s MSRV applies, which

src/phy/fault_injector.rs

+11-10
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

+13-10
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

+5-5
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
})

0 commit comments

Comments
 (0)