Skip to content

Commit 824ca82

Browse files
committed
fix(dns): hostname iterator only returns the first item
1 parent 818eed2 commit 824ca82

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

crates/trippy-dns/src/resolver.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ pub enum DnsEntry {
9595

9696
/// The resolved hostnames of a `DnsEntry`.
9797
#[derive(Debug, Clone)]
98-
pub struct ResolvedHostnames<'a>(pub(super) &'a [String]);
98+
pub struct ResolvedHostnames<'a>(pub(super) std::slice::Iter<'a, String>);
9999

100100
impl<'a> Iterator for ResolvedHostnames<'a> {
101101
type Item = &'a str;
102102

103103
fn next(&mut self) -> Option<Self::Item> {
104-
self.0.iter().next().map(String::as_str)
104+
self.0.next().map(String::as_str)
105105
}
106106
}
107107

@@ -111,10 +111,12 @@ impl DnsEntry {
111111
pub fn hostnames(&self) -> ResolvedHostnames<'_> {
112112
match self {
113113
Self::Resolved(Resolved::WithAsInfo(_, hosts, _) | Resolved::Normal(_, hosts)) => {
114-
ResolvedHostnames(hosts)
114+
ResolvedHostnames(hosts.iter())
115115
}
116-
Self::Pending(_) | Self::Timeout(_) | Self::NotFound(_) | Self::Failed(_) => {
117-
ResolvedHostnames(&[])
116+
Self::Pending(_) | Self::Timeout(_) | Self::NotFound(_) | Self::Failed(_) =>
117+
{
118+
#[expect(clippy::iter_on_empty_collections)]
119+
ResolvedHostnames([].iter())
118120
}
119121
}
120122
}
@@ -185,3 +187,24 @@ impl Display for DnsEntry {
185187
}
186188
}
187189
}
190+
191+
#[cfg(test)]
192+
mod tests {
193+
use super::*;
194+
use std::net::IpAddr;
195+
use std::str::FromStr;
196+
197+
#[test]
198+
fn test_iterator_returns_each_hostname_once() {
199+
let entry = DnsEntry::Resolved(Resolved::Normal(
200+
IpAddr::from_str("1.1.1.1").unwrap(),
201+
vec!["one".to_string(), "two".to_string(), "three".to_string()],
202+
));
203+
204+
let mut iter = entry.hostnames();
205+
assert_eq!(iter.next(), Some("one"));
206+
assert_eq!(iter.next(), Some("two"));
207+
assert_eq!(iter.next(), Some("three"));
208+
assert_eq!(iter.next(), None);
209+
}
210+
}

0 commit comments

Comments
 (0)