@@ -95,13 +95,13 @@ pub enum DnsEntry {
95
95
96
96
/// The resolved hostnames of a `DnsEntry`.
97
97
#[ derive( Debug , Clone ) ]
98
- pub struct ResolvedHostnames < ' a > ( pub ( super ) & ' a [ String ] ) ;
98
+ pub struct ResolvedHostnames < ' a > ( pub ( super ) std :: slice :: Iter < ' a , String > ) ;
99
99
100
100
impl < ' a > Iterator for ResolvedHostnames < ' a > {
101
101
type Item = & ' a str ;
102
102
103
103
fn next ( & mut self ) -> Option < Self :: Item > {
104
- self . 0 . iter ( ) . next ( ) . map ( String :: as_str)
104
+ self . 0 . next ( ) . map ( String :: as_str)
105
105
}
106
106
}
107
107
@@ -111,10 +111,12 @@ impl DnsEntry {
111
111
pub fn hostnames ( & self ) -> ResolvedHostnames < ' _ > {
112
112
match self {
113
113
Self :: Resolved ( Resolved :: WithAsInfo ( _, hosts, _) | Resolved :: Normal ( _, hosts) ) => {
114
- ResolvedHostnames ( hosts)
114
+ ResolvedHostnames ( hosts. iter ( ) )
115
115
}
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 ( ) )
118
120
}
119
121
}
120
122
}
@@ -185,3 +187,24 @@ impl Display for DnsEntry {
185
187
}
186
188
}
187
189
}
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