Skip to content

Commit 676b213

Browse files
authored
Use FromIterator for NameAddr construction (#1199)
If we provide a `FromIterator` implementation for `NameAddr`, we need not have an extra import to construct `NameAddr`s when the type is known. No functional changes.
1 parent 0b0374f commit 676b213

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

linkerd/app/core/src/addr_match.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ipnet::IpNet;
22
use linkerd_addr::Addr;
33
use linkerd_dns::{Name, Suffix};
4-
use std::{fmt, net::IpAddr, sync::Arc};
4+
use std::{fmt, iter::FromIterator, net::IpAddr, sync::Arc};
55

66
#[derive(Clone, Debug, Default)]
77
pub struct AddrMatch {
@@ -23,7 +23,7 @@ impl AddrMatch {
2323
nets: impl IntoIterator<Item = IpNet>,
2424
) -> Self {
2525
Self {
26-
names: NameMatch::new(suffixes),
26+
names: suffixes.into_iter().collect(),
2727
nets: IpMatch::new(nets),
2828
}
2929
}
@@ -54,7 +54,7 @@ impl From<IpMatch> for AddrMatch {
5454
fn from(nets: IpMatch) -> Self {
5555
Self {
5656
nets,
57-
names: NameMatch::new(None),
57+
names: NameMatch::default(),
5858
}
5959
}
6060
}
@@ -74,19 +74,21 @@ impl From<AddrMatch> for IpMatch {
7474
}
7575
}
7676

77+
// === impl NameMatch ===
78+
7779
impl From<AddrMatch> for NameMatch {
7880
fn from(addrs: AddrMatch) -> Self {
7981
addrs.names
8082
}
8183
}
8284

83-
// === impl NameMatch ===
84-
85-
impl NameMatch {
86-
pub fn new(suffixes: impl IntoIterator<Item = Suffix>) -> Self {
87-
Self(Arc::new(suffixes.into_iter().collect()))
85+
impl FromIterator<Suffix> for NameMatch {
86+
fn from_iter<T: IntoIterator<Item = Suffix>>(iter: T) -> Self {
87+
Self(Arc::new(iter.into_iter().collect()))
8888
}
89+
}
8990

91+
impl NameMatch {
9092
#[inline]
9193
pub fn matches(&self, name: &Name) -> bool {
9294
self.0.iter().any(|sfx| sfx.contains(name))

linkerd/app/gateway/src/tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ impl Test {
171171
}
172172

173173
fn with_profile(mut self, profile: profiles::Receiver) -> Self {
174-
let allow = NameMatch::new(Some(dns::Suffix::from_str(self.suffix).unwrap()));
174+
let allow = Some(dns::Suffix::from_str(self.suffix).unwrap())
175+
.into_iter()
176+
.collect::<NameMatch>();
175177
if allow.matches(self.target.name()) {
176178
self.profile = Some(profile);
177179
}

linkerd/app/inbound/src/test_util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use linkerd_app_core::{
99
tap,
1010
},
1111
transport::{Keepalive, ListenAddr},
12-
NameMatch, ProxyRuntime,
12+
ProxyRuntime,
1313
};
1414
pub use linkerd_app_test as support;
1515
use linkerd_server_policy::{Authentication, Authorization, Protocol, ServerPolicy};
@@ -20,7 +20,7 @@ pub fn default_config() -> Config {
2020
.parse::<Suffix>()
2121
.expect("`svc.cluster.local.` suffix is definitely valid");
2222
Config {
23-
allow_discovery: NameMatch::new(Some(cluster_local)),
23+
allow_discovery: Some(cluster_local).into_iter().collect(),
2424
proxy: config::ProxyConfig {
2525
server: config::ServerConfig {
2626
addr: ListenAddr(([0, 0, 0, 0], 0).into()),

linkerd/app/src/env.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::core::{
55
proxy::http::{h1, h2},
66
tls,
77
transport::{Keepalive, ListenAddr},
8-
Addr, AddrMatch, Conditional, IpNet, NameMatch,
8+
Addr, AddrMatch, Conditional, IpNet,
99
};
1010
use crate::{dns, gateway, identity, inbound, oc_collector, outbound};
1111
use inbound::port_policies;
@@ -449,7 +449,7 @@ pub fn parse_config<S: Strings>(strings: &S) -> Result<super::Config, EnvError>
449449
};
450450

451451
let gateway = gateway::Config {
452-
allow_discovery: NameMatch::new(gateway_suffixes?.unwrap_or_default()),
452+
allow_discovery: gateway_suffixes?.into_iter().flatten().collect(),
453453
};
454454

455455
let inbound = {
@@ -570,7 +570,7 @@ pub fn parse_config<S: Strings>(strings: &S) -> Result<super::Config, EnvError>
570570
};
571571

572572
inbound::Config {
573-
allow_discovery: NameMatch::new(dst_profile_suffixes),
573+
allow_discovery: dst_profile_suffixes.into_iter().collect(),
574574
proxy: ProxyConfig {
575575
server,
576576
connect,

0 commit comments

Comments
 (0)