Skip to content

Commit

Permalink
feat(iroh): implement Discovery for Arc'ed Discovery types
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
flub committed Jan 8, 2025
1 parent f08d560 commit bf23bbe
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions iroh/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
doc = "[`LocalSwarmDiscovery`]: local_swarm_discovery::LocalSwarmDiscovery"
)]

use std::sync::Arc;
use std::{collections::BTreeSet, net::SocketAddr, time::Duration};

use anyhow::{anyhow, ensure, Result};
Expand Down Expand Up @@ -194,6 +195,8 @@ pub trait Discovery: std::fmt::Debug + Send + Sync {
}
}

impl<T: Discovery> Discovery for Arc<T> {}

/// The results returned from [`Discovery::resolve`].
#[derive(Debug, Clone)]
pub struct DiscoveryItem {
Expand Down Expand Up @@ -445,6 +448,7 @@ mod tests {

use iroh_base::SecretKey;
use rand::Rng;
use testresult::TestResult;
use tokio_util::task::AbortOnDropHandle;

use super::*;
Expand Down Expand Up @@ -725,6 +729,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.
Expand Down

0 comments on commit bf23bbe

Please sign in to comment.