Skip to content

Commit 1765ae0

Browse files
feat(kademlia)!: use GATs on RecordStore trait (#3239)
Previously, we applied a lifetime onto the entire `RecordStore` to workaround Rust not having GATs. With Rust 1.65.0 we now have GATs so we can remove this workaround. Related #3240. Without this change, we would have to specify HRTB in various places.
1 parent 65ec545 commit 1765ae0

File tree

10 files changed

+44
-31
lines changed

10 files changed

+44
-31
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
- Remove `BandwidthFuture`
5353
- Rename `BandwidthConnecLogging` to `InstrumentedStream`
5454
- Remove `SimpleProtocol` due to being unused. See [`libp2p::core::upgrade`](https://docs.rs/libp2p/0.50.0/libp2p/core/upgrade/index.html) for alternatives. See [PR 3191].
55+
56+
- Bump MSRV to 1.65.0.
57+
5558
- Update individual crates.
5659
- Update to [`libp2p-dcutr` `v0.9.0`](protocols/dcutr/CHANGELOG.md#090).
5760

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "libp2p"
33
edition = "2021"
4-
rust-version = "1.62.0"
4+
rust-version = "1.65.0"
55
description = "Peer-to-peer networking library"
66
version = "0.51.0"
77
authors = ["Parity Technologies <[email protected]>"]

misc/metrics/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- Add `connections_establishment_duration` metric. See [PR 3134].
66

7+
- Bump MSRV to 1.65.0.
8+
79
- Update to `libp2p-dcutr` `v0.9.0`.
810

911
- Update to `libp2p-ping` `v0.42.0`.

misc/metrics/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "libp2p-metrics"
33
edition = "2021"
4-
rust-version = "1.62.0"
4+
rust-version = "1.65.0"
55
description = "Metrics for libp2p"
66
version = "0.12.0"
77
authors = ["Max Inden <[email protected]>"]

protocols/kad/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
- Update to `libp2p-swarm` `v0.42.0`.
66

7+
- Remove lifetime from `RecordStore` and use GATs instead. See [PR 3239].
8+
9+
- Bump MSRV to 1.65.0.
10+
11+
[PR 3239]: https://github.com/libp2p/rust-libp2p/pull/3239
12+
713
# 0.42.0
814

915
- Update to `libp2p-core` `v0.38.0`.

protocols/kad/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "libp2p-kad"
33
edition = "2021"
4-
rust-version = "1.62.0"
4+
rust-version = "1.65.0"
55
description = "Kademlia protocol for libp2p"
66
version = "0.43.0"
77
authors = ["Parity Technologies <[email protected]>"]

protocols/kad/src/behaviour.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,7 @@ impl KademliaConfig {
401401

402402
impl<TStore> Kademlia<TStore>
403403
where
404-
for<'a> TStore: RecordStore<'a>,
405-
TStore: Send + 'static,
404+
TStore: RecordStore + Send + 'static,
406405
{
407406
/// Creates a new `Kademlia` network behaviour with a default configuration.
408407
pub fn new(id: PeerId, store: TStore) -> Self {
@@ -1987,8 +1986,7 @@ fn exp_decrease(ttl: Duration, exp: u32) -> Duration {
19871986

19881987
impl<TStore> NetworkBehaviour for Kademlia<TStore>
19891988
where
1990-
for<'a> TStore: RecordStore<'a>,
1991-
TStore: Send + 'static,
1989+
TStore: RecordStore + Send + 'static,
19921990
{
19931991
type ConnectionHandler = KademliaHandlerProto<QueryId>;
19941992
type OutEvent = KademliaEvent;

protocols/kad/src/jobs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl PutRecordJob {
193193
/// to be run.
194194
pub fn poll<T>(&mut self, cx: &mut Context<'_>, store: &mut T, now: Instant) -> Poll<Record>
195195
where
196-
for<'a> T: RecordStore<'a>,
196+
T: RecordStore,
197197
{
198198
if self.inner.check_ready(cx, now) {
199199
let publish = self.next_publish.map_or(false, |t_pub| now >= t_pub);
@@ -294,7 +294,7 @@ impl AddProviderJob {
294294
now: Instant,
295295
) -> Poll<ProviderRecord>
296296
where
297-
for<'a> T: RecordStore<'a>,
297+
T: RecordStore,
298298
{
299299
if self.inner.check_ready(cx, now) {
300300
let records = store

protocols/kad/src/record/store.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,36 +64,40 @@ pub enum Error {
6464
/// content. Just like a regular record, a provider record is distributed
6565
/// to the closest nodes to the key.
6666
///
67-
pub trait RecordStore<'a> {
68-
type RecordsIter: Iterator<Item = Cow<'a, Record>>;
69-
type ProvidedIter: Iterator<Item = Cow<'a, ProviderRecord>>;
67+
pub trait RecordStore {
68+
type RecordsIter<'a>: Iterator<Item = Cow<'a, Record>>
69+
where
70+
Self: 'a;
71+
type ProvidedIter<'a>: Iterator<Item = Cow<'a, ProviderRecord>>
72+
where
73+
Self: 'a;
7074

7175
/// Gets a record from the store, given its key.
72-
fn get(&'a self, k: &Key) -> Option<Cow<'_, Record>>;
76+
fn get(&self, k: &Key) -> Option<Cow<'_, Record>>;
7377

7478
/// Puts a record into the store.
75-
fn put(&'a mut self, r: Record) -> Result<()>;
79+
fn put(&mut self, r: Record) -> Result<()>;
7680

7781
/// Removes the record with the given key from the store.
78-
fn remove(&'a mut self, k: &Key);
82+
fn remove(&mut self, k: &Key);
7983

8084
/// Gets an iterator over all (value-) records currently stored.
81-
fn records(&'a self) -> Self::RecordsIter;
85+
fn records(&self) -> Self::RecordsIter<'_>;
8286

8387
/// Adds a provider record to the store.
8488
///
8589
/// A record store only needs to store a number of provider records
8690
/// for a key corresponding to the replication factor and should
8791
/// store those records whose providers are closest to the key.
88-
fn add_provider(&'a mut self, record: ProviderRecord) -> Result<()>;
92+
fn add_provider(&mut self, record: ProviderRecord) -> Result<()>;
8993

9094
/// Gets a copy of the stored provider records for the given key.
91-
fn providers(&'a self, key: &Key) -> Vec<ProviderRecord>;
95+
fn providers(&self, key: &Key) -> Vec<ProviderRecord>;
9296

9397
/// Gets an iterator over all stored provider records for which the
9498
/// node owning the store is itself the provider.
95-
fn provided(&'a self) -> Self::ProvidedIter;
99+
fn provided(&self) -> Self::ProvidedIter<'_>;
96100

97101
/// Removes a provider record from the store.
98-
fn remove_provider(&'a mut self, k: &Key, p: &PeerId);
102+
fn remove_provider(&mut self, k: &Key, p: &PeerId);
99103
}

protocols/kad/src/record/store/memory.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ impl MemoryStore {
9696
}
9797
}
9898

99-
impl<'a> RecordStore<'a> for MemoryStore {
100-
type RecordsIter =
99+
impl RecordStore for MemoryStore {
100+
type RecordsIter<'a> =
101101
iter::Map<hash_map::Values<'a, Key, Record>, fn(&'a Record) -> Cow<'a, Record>>;
102102

103-
type ProvidedIter = iter::Map<
103+
type ProvidedIter<'a> = iter::Map<
104104
hash_set::Iter<'a, ProviderRecord>,
105105
fn(&'a ProviderRecord) -> Cow<'a, ProviderRecord>,
106106
>;
107107

108-
fn get(&'a self, k: &Key) -> Option<Cow<'_, Record>> {
108+
fn get(&self, k: &Key) -> Option<Cow<'_, Record>> {
109109
self.records.get(k).map(Cow::Borrowed)
110110
}
111111

112-
fn put(&'a mut self, r: Record) -> Result<()> {
112+
fn put(&mut self, r: Record) -> Result<()> {
113113
if r.value.len() >= self.config.max_value_bytes {
114114
return Err(Error::ValueTooLarge);
115115
}
@@ -131,15 +131,15 @@ impl<'a> RecordStore<'a> for MemoryStore {
131131
Ok(())
132132
}
133133

134-
fn remove(&'a mut self, k: &Key) {
134+
fn remove(&mut self, k: &Key) {
135135
self.records.remove(k);
136136
}
137137

138-
fn records(&'a self) -> Self::RecordsIter {
138+
fn records(&self) -> Self::RecordsIter<'_> {
139139
self.records.values().map(Cow::Borrowed)
140140
}
141141

142-
fn add_provider(&'a mut self, record: ProviderRecord) -> Result<()> {
142+
fn add_provider(&mut self, record: ProviderRecord) -> Result<()> {
143143
let num_keys = self.providers.len();
144144

145145
// Obtain the entry
@@ -189,17 +189,17 @@ impl<'a> RecordStore<'a> for MemoryStore {
189189
Ok(())
190190
}
191191

192-
fn providers(&'a self, key: &Key) -> Vec<ProviderRecord> {
192+
fn providers(&self, key: &Key) -> Vec<ProviderRecord> {
193193
self.providers
194194
.get(key)
195195
.map_or_else(Vec::new, |ps| ps.clone().into_vec())
196196
}
197197

198-
fn provided(&'a self) -> Self::ProvidedIter {
198+
fn provided(&self) -> Self::ProvidedIter<'_> {
199199
self.provided.iter().map(Cow::Borrowed)
200200
}
201201

202-
fn remove_provider(&'a mut self, key: &Key, provider: &PeerId) {
202+
fn remove_provider(&mut self, key: &Key, provider: &PeerId) {
203203
if let hash_map::Entry::Occupied(mut e) = self.providers.entry(key.clone()) {
204204
let providers = e.get_mut();
205205
if let Some(i) = providers.iter().position(|p| &p.provider == provider) {

0 commit comments

Comments
 (0)