From 9b01d61c08138ab4476d32d2e266a3de8c52dc85 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 14 Dec 2022 12:40:28 +1100 Subject: [PATCH 1/4] Use GATs to avoid lifetimes on trait interface --- protocols/kad/src/behaviour.rs | 6 ++---- protocols/kad/src/jobs.rs | 4 ++-- protocols/kad/src/record/store.rs | 26 ++++++++++++++---------- protocols/kad/src/record/store/memory.rs | 22 ++++++++++---------- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 42081b22108..7589cc57578 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -401,8 +401,7 @@ impl KademliaConfig { impl Kademlia where - for<'a> TStore: RecordStore<'a>, - TStore: Send + 'static, + TStore: RecordStore + Send + 'static, { /// Creates a new `Kademlia` network behaviour with a default configuration. pub fn new(id: PeerId, store: TStore) -> Self { @@ -1987,8 +1986,7 @@ fn exp_decrease(ttl: Duration, exp: u32) -> Duration { impl NetworkBehaviour for Kademlia where - for<'a> TStore: RecordStore<'a>, - TStore: Send + 'static, + TStore: RecordStore + Send + 'static, { type ConnectionHandler = KademliaHandlerProto; type OutEvent = KademliaEvent; diff --git a/protocols/kad/src/jobs.rs b/protocols/kad/src/jobs.rs index 8855026e8d5..7fbb6e00d75 100644 --- a/protocols/kad/src/jobs.rs +++ b/protocols/kad/src/jobs.rs @@ -193,7 +193,7 @@ impl PutRecordJob { /// to be run. pub fn poll(&mut self, cx: &mut Context<'_>, store: &mut T, now: Instant) -> Poll where - for<'a> T: RecordStore<'a>, + T: RecordStore, { if self.inner.check_ready(cx, now) { let publish = self.next_publish.map_or(false, |t_pub| now >= t_pub); @@ -294,7 +294,7 @@ impl AddProviderJob { now: Instant, ) -> Poll where - for<'a> T: RecordStore<'a>, + T: RecordStore, { if self.inner.check_ready(cx, now) { let records = store diff --git a/protocols/kad/src/record/store.rs b/protocols/kad/src/record/store.rs index 9e75e5a3e8a..5c25bc8b2fa 100644 --- a/protocols/kad/src/record/store.rs +++ b/protocols/kad/src/record/store.rs @@ -64,36 +64,40 @@ pub enum Error { /// content. Just like a regular record, a provider record is distributed /// to the closest nodes to the key. /// -pub trait RecordStore<'a> { - type RecordsIter: Iterator>; - type ProvidedIter: Iterator>; +pub trait RecordStore { + type RecordsIter<'a>: Iterator> + where + Self: 'a; + type ProvidedIter<'a>: Iterator> + where + Self: 'a; /// Gets a record from the store, given its key. - fn get(&'a self, k: &Key) -> Option>; + fn get(&self, k: &Key) -> Option>; /// Puts a record into the store. - fn put(&'a mut self, r: Record) -> Result<()>; + fn put(&mut self, r: Record) -> Result<()>; /// Removes the record with the given key from the store. - fn remove(&'a mut self, k: &Key); + fn remove(&mut self, k: &Key); /// Gets an iterator over all (value-) records currently stored. - fn records(&'a self) -> Self::RecordsIter; + fn records(&self) -> Self::RecordsIter<'_>; /// Adds a provider record to the store. /// /// A record store only needs to store a number of provider records /// for a key corresponding to the replication factor and should /// store those records whose providers are closest to the key. - fn add_provider(&'a mut self, record: ProviderRecord) -> Result<()>; + fn add_provider(&mut self, record: ProviderRecord) -> Result<()>; /// Gets a copy of the stored provider records for the given key. - fn providers(&'a self, key: &Key) -> Vec; + fn providers(&self, key: &Key) -> Vec; /// Gets an iterator over all stored provider records for which the /// node owning the store is itself the provider. - fn provided(&'a self) -> Self::ProvidedIter; + fn provided(&self) -> Self::ProvidedIter<'_>; /// Removes a provider record from the store. - fn remove_provider(&'a mut self, k: &Key, p: &PeerId); + fn remove_provider(&mut self, k: &Key, p: &PeerId); } diff --git a/protocols/kad/src/record/store/memory.rs b/protocols/kad/src/record/store/memory.rs index 39d17d37c2b..87951637889 100644 --- a/protocols/kad/src/record/store/memory.rs +++ b/protocols/kad/src/record/store/memory.rs @@ -96,20 +96,20 @@ impl MemoryStore { } } -impl<'a> RecordStore<'a> for MemoryStore { - type RecordsIter = +impl RecordStore for MemoryStore { + type RecordsIter<'a> = iter::Map, fn(&'a Record) -> Cow<'a, Record>>; - type ProvidedIter = iter::Map< + type ProvidedIter<'a> = iter::Map< hash_set::Iter<'a, ProviderRecord>, fn(&'a ProviderRecord) -> Cow<'a, ProviderRecord>, >; - fn get(&'a self, k: &Key) -> Option> { + fn get(&self, k: &Key) -> Option> { self.records.get(k).map(Cow::Borrowed) } - fn put(&'a mut self, r: Record) -> Result<()> { + fn put(&mut self, r: Record) -> Result<()> { if r.value.len() >= self.config.max_value_bytes { return Err(Error::ValueTooLarge); } @@ -131,15 +131,15 @@ impl<'a> RecordStore<'a> for MemoryStore { Ok(()) } - fn remove(&'a mut self, k: &Key) { + fn remove(&mut self, k: &Key) { self.records.remove(k); } - fn records(&'a self) -> Self::RecordsIter { + fn records(&self) -> Self::RecordsIter<'_> { self.records.values().map(Cow::Borrowed) } - fn add_provider(&'a mut self, record: ProviderRecord) -> Result<()> { + fn add_provider(&mut self, record: ProviderRecord) -> Result<()> { let num_keys = self.providers.len(); // Obtain the entry @@ -189,17 +189,17 @@ impl<'a> RecordStore<'a> for MemoryStore { Ok(()) } - fn providers(&'a self, key: &Key) -> Vec { + fn providers(&self, key: &Key) -> Vec { self.providers .get(key) .map_or_else(Vec::new, |ps| ps.clone().into_vec()) } - fn provided(&'a self) -> Self::ProvidedIter { + fn provided(&self) -> Self::ProvidedIter<'_> { self.provided.iter().map(Cow::Borrowed) } - fn remove_provider(&'a mut self, key: &Key, provider: &PeerId) { + fn remove_provider(&mut self, key: &Key, provider: &PeerId) { if let hash_map::Entry::Occupied(mut e) = self.providers.entry(key.clone()) { let providers = e.get_mut(); if let Some(i) = providers.iter().position(|p| &p.provider == provider) { From 4d246dc6475a5a3f275e8aa5f5d649f9d559ee01 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 14 Dec 2022 12:41:50 +1100 Subject: [PATCH 2/4] Bump MSRV accordingly --- CHANGELOG.md | 2 ++ Cargo.toml | 2 +- protocols/kad/CHANGELOG.md | 2 ++ protocols/kad/Cargo.toml | 2 +- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df2d37c210..be066b37b46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,8 @@ - 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]. +- Bump MSRV to 1.65.0. + - Update individual crates. - Update to [`libp2p-dcutr` `v0.9.0`](protocols/dcutr/CHANGELOG.md#090). diff --git a/Cargo.toml b/Cargo.toml index eaed637a2d8..07f1d1fbc1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "libp2p" edition = "2021" -rust-version = "1.62.0" +rust-version = "1.65.0" description = "Peer-to-peer networking library" version = "0.51.0" authors = ["Parity Technologies "] diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index f126a519958..2edc45aeea6 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -2,6 +2,8 @@ - Update to `libp2p-swarm` `v0.42.0`. +- Bump MSRV to 1.65.0. + # 0.42.0 - Update to `libp2p-core` `v0.38.0`. diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index bf7a456df0e..8f8d8fad56e 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "libp2p-kad" edition = "2021" -rust-version = "1.62.0" +rust-version = "1.65.0" description = "Kademlia protocol for libp2p" version = "0.43.0" authors = ["Parity Technologies "] From 74a275866640624e241b85a22eeee381fb393880 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 14 Dec 2022 12:54:31 +1100 Subject: [PATCH 3/4] Bump MSRV of libp2p-metrics --- misc/metrics/CHANGELOG.md | 2 ++ misc/metrics/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/misc/metrics/CHANGELOG.md b/misc/metrics/CHANGELOG.md index 70cfd948e6a..bb6680530bd 100644 --- a/misc/metrics/CHANGELOG.md +++ b/misc/metrics/CHANGELOG.md @@ -2,6 +2,8 @@ - Add `connections_establishment_duration` metric. See [PR 3134]. +- Bump MSRV to 1.65.0. + - Update to `libp2p-dcutr` `v0.9.0`. - Update to `libp2p-ping` `v0.42.0`. diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index f1dc7526386..ba440704746 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "libp2p-metrics" edition = "2021" -rust-version = "1.62.0" +rust-version = "1.65.0" description = "Metrics for libp2p" version = "0.12.0" authors = ["Max Inden "] From e6e33c57f15ce45912323d31514068405bf7dea7 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 20 Dec 2022 13:45:46 +1100 Subject: [PATCH 4/4] Add changelog entry --- protocols/kad/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 2edc45aeea6..31912fc738d 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -2,8 +2,12 @@ - Update to `libp2p-swarm` `v0.42.0`. +- Remove lifetime from `RecordStore` and use GATs instead. See [PR 3239]. + - Bump MSRV to 1.65.0. +[PR 3239]: https://github.com/libp2p/rust-libp2p/pull/3239 + # 0.42.0 - Update to `libp2p-core` `v0.38.0`.