Skip to content

Commit 25c2d4d

Browse files
authored
refactor: Prefer EndpointAddr::from_parts over EndpointAddr { ... } (#3662)
## Description This would make the diff smaller if we decide to go with generic endpoint ids. See #3653 . And I also think it is nicer in any case. ## Breaking Changes None ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist <!-- Remove any that are not relevant. --> - [ ] Self-review. - [ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [ ] Tests if relevant. - [ ] All breaking changes documented. - [ ] List all breaking changes in the above "Breaking Changes" section. - [ ] Open an issue or PR on any number0 repos that are affected by this breaking change. Give guidance on how the updates should be handled or do the actual updates themselves. The major ones are: - [ ] [`quic-rpc`](https://github.com/n0-computer/quic-rpc) - [ ] [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip) - [ ] [`iroh-blobs`](https://github.com/n0-computer/iroh-blobs) - [ ] [`dumbpipe`](https://github.com/n0-computer/dumbpipe) - [ ] [`sendme`](https://github.com/n0-computer/sendme)
1 parent 280cfc6 commit 25c2d4d

File tree

4 files changed

+36
-65
lines changed

4 files changed

+36
-65
lines changed

iroh/src/discovery.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -909,12 +909,10 @@ mod tests {
909909
let (ep2, _guard2) =
910910
new_endpoint(&mut rng, |ep| disco_shared.create_discovery(ep.id())).await;
911911

912-
let ep1_wrong_addr = EndpointAddr {
913-
id: ep1.id(),
914-
addrs: [TransportAddr::Ip("240.0.0.1:1000".parse().unwrap())]
915-
.into_iter()
916-
.collect(),
917-
};
912+
let ep1_wrong_addr = EndpointAddr::from_parts(
913+
ep1.id(),
914+
[TransportAddr::Ip("240.0.0.1:1000".parse().unwrap())],
915+
);
918916
let _conn = ep2.connect(ep1_wrong_addr, TEST_ALPN).await?;
919917
Ok(())
920918
}
@@ -1059,10 +1057,7 @@ mod test_dns_pkarr {
10591057
.await?;
10601058
println!("resolved {resolved:?}");
10611059

1062-
let expected_addr = EndpointAddr {
1063-
id: endpoint_id,
1064-
addrs: relay_url.into_iter().collect(),
1065-
};
1060+
let expected_addr = EndpointAddr::from_parts(endpoint_id, relay_url);
10661061

10671062
assert_eq!(resolved.to_endpoint_addr(), expected_addr);
10681063
assert_eq!(resolved.user_data(), Some(&user_data));

iroh/src/discovery/static_provider.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,10 @@ mod tests {
248248
.await?;
249249

250250
let key = SecretKey::from_bytes(&[0u8; 32]);
251-
let addr = EndpointAddr {
252-
id: key.public(),
253-
addrs: [TransportAddr::Relay("https://example.com".parse()?)]
254-
.into_iter()
255-
.collect(),
256-
};
251+
let addr = EndpointAddr::from_parts(
252+
key.public(),
253+
[TransportAddr::Relay("https://example.com".parse()?)],
254+
);
257255
let user_data = Some("foobar".parse().unwrap());
258256
let endpoint_info = EndpointInfo::from(addr.clone()).with_user_data(user_data.clone());
259257
discovery.add_endpoint_info(endpoint_info.clone());
@@ -280,12 +278,10 @@ mod tests {
280278
async fn test_provenance() -> Result {
281279
let discovery = StaticProvider::with_provenance("foo");
282280
let key = SecretKey::from_bytes(&[0u8; 32]);
283-
let addr = EndpointAddr {
284-
id: key.public(),
285-
addrs: [TransportAddr::Relay("https://example.com".parse()?)]
286-
.into_iter()
287-
.collect(),
288-
};
281+
let addr = EndpointAddr::from_parts(
282+
key.public(),
283+
[TransportAddr::Relay("https://example.com".parse()?)],
284+
);
289285
discovery.add_endpoint_info(addr);
290286
let mut stream = discovery.resolve(key.public()).unwrap();
291287
let item = stream.next().await.unwrap()?;

iroh/src/magicsock.rs

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,10 +2651,10 @@ mod tests {
26512651
continue;
26522652
}
26532653

2654-
let addr = EndpointAddr {
2655-
id: me.public(),
2656-
addrs: new_addrs.iter().copied().map(TransportAddr::Ip).collect(),
2657-
};
2654+
let addr = EndpointAddr::from_parts(
2655+
me.public(),
2656+
new_addrs.iter().copied().map(TransportAddr::Ip),
2657+
);
26582658
m.endpoint.magic_sock().add_test_addr(addr);
26592659
}
26602660
}
@@ -3216,12 +3216,8 @@ mod tests {
32163216
.ip_addrs()
32173217
.get()
32183218
.into_iter()
3219-
.map(|x| TransportAddr::Ip(x.addr))
3220-
.collect();
3221-
let endpoint_addr_2 = EndpointAddr {
3222-
id: endpoint_id_2,
3223-
addrs,
3224-
};
3219+
.map(|x| TransportAddr::Ip(x.addr));
3220+
let endpoint_addr_2 = EndpointAddr::from_parts(endpoint_id_2, addrs);
32253221
msock_1
32263222
.add_endpoint_addr(
32273223
endpoint_addr_2,
@@ -3292,10 +3288,7 @@ mod tests {
32923288

32933289
// Add an empty entry in the EndpointMap of ep_1
32943290
msock_1.endpoint_map.add_endpoint_addr(
3295-
EndpointAddr {
3296-
id: endpoint_id_2,
3297-
addrs: Default::default(),
3298-
},
3291+
EndpointAddr::from_parts(endpoint_id_2, []),
32993292
Source::NamedApp {
33003293
name: "test".into(),
33013294
},
@@ -3332,13 +3325,9 @@ mod tests {
33323325
.ip_addrs()
33333326
.get()
33343327
.into_iter()
3335-
.map(|x| TransportAddr::Ip(x.addr))
3336-
.collect();
3328+
.map(|x| TransportAddr::Ip(x.addr));
33373329
msock_1.endpoint_map.add_endpoint_addr(
3338-
EndpointAddr {
3339-
id: endpoint_id_2,
3340-
addrs,
3341-
},
3330+
EndpointAddr::from_parts(endpoint_id_2, addrs),
33423331
Source::NamedApp {
33433332
name: "test".into(),
33443333
},
@@ -3393,41 +3382,35 @@ mod tests {
33933382
);
33943383

33953384
// relay url only
3396-
let addr = EndpointAddr {
3397-
id: SecretKey::generate(&mut rng).public(),
3398-
addrs: [TransportAddr::Relay("http://my-relay.com".parse().unwrap())]
3399-
.into_iter()
3400-
.collect(),
3401-
};
3385+
let addr = EndpointAddr::from_parts(
3386+
SecretKey::generate(&mut rng).public(),
3387+
[TransportAddr::Relay("http://my-relay.com".parse().unwrap())],
3388+
);
34023389
stack
34033390
.endpoint
34043391
.magic_sock()
34053392
.add_endpoint_addr(addr, endpoint_map::Source::App)?;
34063393
assert_eq!(stack.endpoint.magic_sock().endpoint_map.endpoint_count(), 1);
34073394

34083395
// addrs only
3409-
let addr = EndpointAddr {
3410-
id: SecretKey::generate(&mut rng).public(),
3411-
addrs: [TransportAddr::Ip("127.0.0.1:1234".parse().unwrap())]
3412-
.into_iter()
3413-
.collect(),
3414-
};
3396+
let addr = EndpointAddr::from_parts(
3397+
SecretKey::generate(&mut rng).public(),
3398+
[TransportAddr::Ip("127.0.0.1:1234".parse().unwrap())],
3399+
);
34153400
stack
34163401
.endpoint
34173402
.magic_sock()
34183403
.add_endpoint_addr(addr, endpoint_map::Source::App)?;
34193404
assert_eq!(stack.endpoint.magic_sock().endpoint_map.endpoint_count(), 2);
34203405

34213406
// both
3422-
let addr = EndpointAddr {
3423-
id: SecretKey::generate(&mut rng).public(),
3424-
addrs: [
3407+
let addr = EndpointAddr::from_parts(
3408+
SecretKey::generate(&mut rng).public(),
3409+
[
34253410
TransportAddr::Relay("http://my-relay.com".parse().unwrap()),
34263411
TransportAddr::Ip("127.0.0.1:1234".parse().unwrap()),
3427-
]
3428-
.into_iter()
3429-
.collect(),
3430-
};
3412+
],
3413+
);
34313414
stack
34323415
.endpoint
34333416
.magic_sock()

iroh/src/magicsock/endpoint_map/endpoint_state.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,10 +1232,7 @@ impl From<RemoteInfo> for EndpointAddr {
12321232
addrs.insert(TransportAddr::Relay(url.into()));
12331233
}
12341234

1235-
EndpointAddr {
1236-
id: info.endpoint_id,
1237-
addrs,
1238-
}
1235+
EndpointAddr::from_parts(info.endpoint_id, addrs)
12391236
}
12401237
}
12411238

0 commit comments

Comments
 (0)