From 4d28dfaaf4824e57c9dffc1045068c4e0321dd05 Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Wed, 8 Jan 2025 18:27:55 +0100 Subject: [PATCH 1/3] feat(iroh): implement Discovery for Arc'ed Discovery types Nothing in the trait stops it from working for discovery structs which are Arc'ed. Not all discovery types are in control of the users so this is helpful. I stumbled upon this because StaticProvider is not Clone. --- iroh/src/discovery.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/iroh/src/discovery.rs b/iroh/src/discovery.rs index c23789b5782..d755c145dae 100644 --- a/iroh/src/discovery.rs +++ b/iroh/src/discovery.rs @@ -111,7 +111,7 @@ doc = "[`LocalSwarmDiscovery`]: local_swarm_discovery::LocalSwarmDiscovery" )] -use std::{collections::BTreeSet, net::SocketAddr, time::Duration}; +use std::{collections::BTreeSet, net::SocketAddr, sync::Arc, time::Duration}; use anyhow::{anyhow, ensure, Result}; use futures_lite::stream::{Boxed as BoxStream, StreamExt}; @@ -194,6 +194,8 @@ pub trait Discovery: std::fmt::Debug + Send + Sync { } } +impl Discovery for Arc {} + /// The results returned from [`Discovery::resolve`]. #[derive(Debug, Clone)] pub struct DiscoveryItem { @@ -445,6 +447,7 @@ mod tests { use iroh_base::SecretKey; use rand::Rng; + use testresult::TestResult; use tokio_util::task::AbortOnDropHandle; use super::*; @@ -725,6 +728,21 @@ mod tests { .expect("time drift") .as_micros() as u64 } + + #[tokio::test] + async fn test_arc_discovery() -> TestResult { + let discovery = Arc::new(EmptyDiscovery); + + let _ep = Endpoint::builder() + .add_discovery({ + let discovery = discovery.clone(); + move |_| Some(discovery) + }) + .bind() + .await?; + + Ok(()) + } } /// This module contains end-to-end tests for DNS node discovery. From c2b19ca3b8845cfc8253bb0c40272e20c5ab40dd Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Fri, 10 Jan 2025 11:10:50 +0100 Subject: [PATCH 2/3] remove kind of useless test --- iroh/src/discovery.rs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/iroh/src/discovery.rs b/iroh/src/discovery.rs index d755c145dae..b23f8c93c2e 100644 --- a/iroh/src/discovery.rs +++ b/iroh/src/discovery.rs @@ -728,21 +728,6 @@ mod tests { .expect("time drift") .as_micros() as u64 } - - #[tokio::test] - async fn test_arc_discovery() -> TestResult { - let discovery = Arc::new(EmptyDiscovery); - - let _ep = Endpoint::builder() - .add_discovery({ - let discovery = discovery.clone(); - move |_| Some(discovery) - }) - .bind() - .await?; - - Ok(()) - } } /// This module contains end-to-end tests for DNS node discovery. From c82735eacfbcb2fe032a8e991d70a9c4098ee8eb Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Fri, 10 Jan 2025 11:11:23 +0100 Subject: [PATCH 3/3] and import --- iroh/src/discovery.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/iroh/src/discovery.rs b/iroh/src/discovery.rs index b23f8c93c2e..beb5b947d51 100644 --- a/iroh/src/discovery.rs +++ b/iroh/src/discovery.rs @@ -447,7 +447,6 @@ mod tests { use iroh_base::SecretKey; use rand::Rng; - use testresult::TestResult; use tokio_util::task::AbortOnDropHandle; use super::*;