Skip to content

Commit

Permalink
Use a named type for Endpoint::node_addr return type
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Dec 13, 2024
1 parent 2529cde commit 111a428
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
51 changes: 33 additions & 18 deletions iroh/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::{
dns::{default_resolver, DnsResolver},
magicsock::{self, Handle, QuicMappedAddr},
tls,
watchable::{DirectWatcher, Watcher},
watchable::{DirectWatcher, MapWatcher, OrWatcher, Watcher},
};

mod rtt_actor;
Expand Down Expand Up @@ -74,6 +74,22 @@ const DISCOVERY_WAIT_PERIOD: Duration = Duration::from_millis(500);

type DiscoveryBuilder = Box<dyn FnOnce(&SecretKey) -> Option<Box<dyn Discovery>> + Send + Sync>;

type NodeAddrMapper = Box<
dyn Fn((Option<BTreeSet<DirectAddr>>, Option<RelayUrl>)) -> Option<NodeAddr>
+ Send
+ Sync
+ 'static,
>;

/// TODO(matheus23): DOCS (don't even ask)
///
/// Implements [`Watcher`]`<Option<`[`NodeAddr`]`>>`.
pub type NodeAddrWatcher = MapWatcher<
OrWatcher<DirectWatcher<Option<BTreeSet<DirectAddr>>>, DirectWatcher<Option<RelayUrl>>>,
Option<NodeAddr>,
NodeAddrMapper,
>;

/// Builder for [`Endpoint`].
///
/// By default the endpoint will generate a new random [`SecretKey`], which will result in a
Expand Down Expand Up @@ -759,26 +775,25 @@ impl Endpoint {
/// The returned [`NodeAddr`] will have the current [`RelayUrl`] and direct addresses
/// as they would be returned by [`Endpoint::home_relay`] and
/// [`Endpoint::direct_addresses`].
pub fn node_addr(&self) -> Result<impl Watcher<Value = Option<NodeAddr>>> {
pub fn node_addr(&self) -> Result<NodeAddrWatcher> {
let watch_addrs = self.direct_addresses();
let watch_relay = self.home_relay();
let node_id = self.node_id();
let watcher =
watch_addrs
.or(watch_relay)
.map(move |(addrs, relay)| match (addrs, relay) {
(Some(addrs), relay) => Some(NodeAddr::from_parts(
node_id,
relay,
addrs.into_iter().map(|x| x.addr),
)),
(None, Some(relay)) => Some(NodeAddr::from_parts(
node_id,
Some(relay),
std::iter::empty(),
)),
(None, None) => None,
})?;
let mapper: NodeAddrMapper = Box::new(move |(addrs, relay)| match (addrs, relay) {
(Some(addrs), relay) => Some(NodeAddr::from_parts(
node_id,
relay,
addrs.into_iter().map(|x| x.addr),
)),
(None, Some(relay)) => Some(NodeAddr::from_parts(
node_id,
Some(relay),
std::iter::empty(),
)),
(None, None) => None,
});

let watcher = watch_addrs.or(watch_relay).map(mapper)?;
Ok(watcher)
}

Expand Down
22 changes: 16 additions & 6 deletions iroh/src/watchable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ pub trait Watcher: Clone {
}

/// Maps this watcher with a function that transforms the observed values.
fn map<T: Clone + Eq, F: Clone + Fn(Self::Value) -> T>(
fn map<T: Clone + Eq, F: Fn(Self::Value) -> T>(
self,
map: F,
) -> Result<MapWatcher<Self, T, F>, Disconnected> {
Ok(MapWatcher {
current: (map)(self.get()?),
map,
map: Arc::new(map),
watcher: self,
})
}
Expand Down Expand Up @@ -292,14 +292,24 @@ impl<S: Watcher, T: Watcher> Watcher for OrWatcher<S, T> {
}

/// Maps a [`Watcher`] and allows filtering updates.
#[derive(Clone, Debug)]
pub struct MapWatcher<W: Watcher, T: Clone + Eq, F: Clone + Fn(W::Value) -> T> {
map: F,
#[derive(Debug)]
pub struct MapWatcher<W: Watcher, T: Clone + Eq, F: Fn(W::Value) -> T> {
map: Arc<F>,
watcher: W,
current: T,
}

impl<W: Watcher, T: Clone + Eq, F: Clone + Fn(W::Value) -> T> Watcher for MapWatcher<W, T, F> {
impl<W: Watcher + Clone, T: Clone + Eq, F: Fn(W::Value) -> T> Clone for MapWatcher<W, T, F> {
fn clone(&self) -> Self {
Self {
map: self.map.clone(),
watcher: self.watcher.clone(),
current: self.current.clone(),
}
}
}

impl<W: Watcher, T: Clone + Eq, F: Fn(W::Value) -> T> Watcher for MapWatcher<W, T, F> {
type Value = T;

fn get(&self) -> Result<Self::Value, Disconnected> {
Expand Down

0 comments on commit 111a428

Please sign in to comment.