From ad7ec902e5fba77c3915624efa5bd211dcd8e2a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren?= Date: Tue, 21 May 2024 13:28:19 +0200 Subject: [PATCH] Avoid having to clone during drop Use `Option`s in `AddressSubscription`, because they can be `take`n. --- web-client/src/client/lib.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/web-client/src/client/lib.rs b/web-client/src/client/lib.rs index df1a22cec7..7879209ff2 100644 --- a/web-client/src/client/lib.rs +++ b/web-client/src/client/lib.rs @@ -532,8 +532,10 @@ impl Client { let consensus = self.inner.consensus_proxy(); struct AddressSubscription { - address: nimiq_keys::Address, - consensus: ConsensusProxy, + // Use Options to be able to take the address and consensus out of the + // struct during drop, avoiding another clone. + address: Option, + consensus: Option, } impl AddressSubscription { pub async fn subscribe( @@ -543,13 +545,16 @@ impl Client { consensus .subscribe_to_addresses(vec![address.clone()], 1, None) .await?; - Ok(Self { address, consensus }) + Ok(Self { + address: Some(address), + consensus: Some(consensus), + }) } } impl Drop for AddressSubscription { fn drop(&mut self) { - let address = self.address.clone(); - let consensus = self.consensus.clone(); + let address = self.address.take().unwrap(); + let consensus = self.consensus.take().unwrap(); // Unsubscribe from the address without caring about the result spawn_local(async move { let _ = consensus.unsubscribe_from_addresses(vec![address], 1).await;