@@ -101,6 +101,8 @@ lazy_static! {
101
101
102
102
/// A hostname:port address pair.
103
103
#[ derive( Clone , Debug , Eq ) ]
104
+ #[ deprecated = "This type has been renamed to `ServerAddress` and will be removed in the 2.0.0 \
105
+ stable release"]
104
106
pub struct StreamAddress {
105
107
/// The hostname of the address.
106
108
pub hostname : String ,
@@ -111,7 +113,44 @@ pub struct StreamAddress {
111
113
pub port : Option < u16 > ,
112
114
}
113
115
114
- impl < ' de > Deserialize < ' de > for StreamAddress {
116
+ #[ allow( deprecated) ]
117
+ impl PartialEq for StreamAddress {
118
+ fn eq ( & self , other : & Self ) -> bool {
119
+ self . hostname == other. hostname && self . port . unwrap_or ( 27017 ) == other. port . unwrap_or ( 27017 )
120
+ }
121
+ }
122
+
123
+ #[ allow( deprecated) ]
124
+ impl Hash for StreamAddress {
125
+ fn hash < H > ( & self , state : & mut H )
126
+ where
127
+ H : Hasher ,
128
+ {
129
+ self . hostname . hash ( state) ;
130
+ self . port . unwrap_or ( 27017 ) . hash ( state) ;
131
+ }
132
+ }
133
+
134
+ /// An enum representing the address of a MongoDB server.
135
+ ///
136
+ /// Currently this just supports addresses that can be connected to over TCP, but alternative
137
+ /// address types may be supported in the future (e.g. Unix Domain Socket paths).
138
+ #[ derive( Clone , Debug , Eq ) ]
139
+ #[ non_exhaustive]
140
+ pub enum ServerAddress {
141
+ /// A TCP/IP host and port combination.
142
+ Tcp {
143
+ /// The hostname or IP address where the MongoDB server can be found.
144
+ host : String ,
145
+
146
+ /// The TCP port that the MongoDB server is listening on.
147
+ ///
148
+ /// The default is 27017.
149
+ port : Option < u16 > ,
150
+ } ,
151
+ }
152
+
153
+ impl < ' de > Deserialize < ' de > for ServerAddress {
115
154
fn deserialize < D > ( deserializer : D ) -> std:: result:: Result < Self , D :: Error >
116
155
where
117
156
D : Deserializer < ' de > ,
@@ -121,32 +160,54 @@ impl<'de> Deserialize<'de> for StreamAddress {
121
160
}
122
161
}
123
162
124
- impl Default for StreamAddress {
163
+ impl Default for ServerAddress {
125
164
fn default ( ) -> Self {
126
- Self {
127
- hostname : "localhost" . into ( ) ,
165
+ Self :: Tcp {
166
+ host : "localhost" . into ( ) ,
128
167
port : None ,
129
168
}
130
169
}
131
170
}
132
171
133
- impl PartialEq for StreamAddress {
172
+ impl PartialEq for ServerAddress {
134
173
fn eq ( & self , other : & Self ) -> bool {
135
- self . hostname == other. hostname && self . port . unwrap_or ( 27017 ) == other. port . unwrap_or ( 27017 )
174
+ match ( self , other) {
175
+ (
176
+ Self :: Tcp { host, port } ,
177
+ Self :: Tcp {
178
+ host : other_host,
179
+ port : other_port,
180
+ } ,
181
+ ) => host == other_host && port. unwrap_or ( 27017 ) == other_port. unwrap_or ( 27017 ) ,
182
+ }
136
183
}
137
184
}
138
185
139
- impl Hash for StreamAddress {
186
+ impl Hash for ServerAddress {
140
187
fn hash < H > ( & self , state : & mut H )
141
188
where
142
189
H : Hasher ,
143
190
{
144
- self . hostname . hash ( state) ;
145
- self . port . unwrap_or ( 27017 ) . hash ( state) ;
191
+ match self {
192
+ Self :: Tcp { host, port } => {
193
+ host. hash ( state) ;
194
+ port. unwrap_or ( 27017 ) . hash ( state) ;
195
+ }
196
+ }
146
197
}
147
198
}
148
199
149
- impl StreamAddress {
200
+ #[ allow( deprecated) ]
201
+ impl From < StreamAddress > for ServerAddress {
202
+ fn from ( sa : StreamAddress ) -> Self {
203
+ Self :: Tcp {
204
+ host : sa. hostname ,
205
+ port : sa. port ,
206
+ }
207
+ }
208
+ }
209
+
210
+ impl ServerAddress {
150
211
/// Parses an address string into a `StreamAddress`.
151
212
pub fn parse ( address : impl AsRef < str > ) -> Result < Self > {
152
213
let address = address. as_ref ( ) ;
@@ -186,36 +247,44 @@ impl StreamAddress {
186
247
None => None ,
187
248
} ;
188
249
189
- Ok ( StreamAddress {
190
- hostname : hostname. to_string ( ) ,
250
+ Ok ( ServerAddress :: Tcp {
251
+ host : hostname. to_string ( ) ,
191
252
port,
192
253
} )
193
254
}
194
255
195
256
#[ cfg( all( test, not( feature = "sync" ) ) ) ]
196
- pub ( crate ) fn into_document ( mut self ) -> Document {
197
- let mut doc = Document :: new ( ) ;
198
-
199
- doc. insert ( "host" , & self . hostname ) ;
257
+ pub ( crate ) fn into_document ( self ) -> Document {
258
+ match self {
259
+ Self :: Tcp { host, port } => {
260
+ doc ! {
261
+ "host" : host,
262
+ "port" : port. map( |i| Bson :: Int32 ( i. into( ) ) ) . unwrap_or( Bson :: Null )
263
+ }
264
+ }
265
+ }
266
+ }
200
267
201
- if let Some ( i) = self . port . take ( ) {
202
- doc. insert ( "port" , i32:: from ( i) ) ;
203
- } else {
204
- doc. insert ( "port" , Bson :: Null ) ;
268
+ pub ( crate ) fn host ( & self ) -> & str {
269
+ match self {
270
+ Self :: Tcp { host, .. } => host. as_str ( ) ,
205
271
}
272
+ }
206
273
207
- doc
274
+ pub ( crate ) fn port ( & self ) -> Option < u16 > {
275
+ match self {
276
+ Self :: Tcp { port, .. } => * port,
277
+ }
208
278
}
209
279
}
210
280
211
- impl fmt:: Display for StreamAddress {
281
+ impl fmt:: Display for ServerAddress {
212
282
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
213
- write ! (
214
- fmt,
215
- "{}:{}" ,
216
- self . hostname,
217
- self . port. unwrap_or( DEFAULT_PORT )
218
- )
283
+ match self {
284
+ Self :: Tcp { host, port } => {
285
+ write ! ( fmt, "{}:{}" , host, port. unwrap_or( DEFAULT_PORT ) )
286
+ }
287
+ }
219
288
}
220
289
}
221
290
@@ -291,12 +360,12 @@ pub struct ClientOptions {
291
360
/// Note that by default, the driver will autodiscover other nodes in the cluster. To connect
292
361
/// directly to a single server (rather than autodiscovering the rest of the cluster), set the
293
362
/// `direct` field to `true`.
294
- #[ builder( default_code = "vec![ StreamAddress {
295
- hostname : \" localhost\" .to_string(),
363
+ #[ builder( default_code = "vec![ServerAddress::Tcp {
364
+ host : \" localhost\" .to_string(),
296
365
port: Some(27017),
297
366
}]" ) ]
298
367
#[ serde( default = "default_hosts" ) ]
299
- pub hosts : Vec < StreamAddress > ,
368
+ pub hosts : Vec < ServerAddress > ,
300
369
301
370
/// The application name that the Client will send to the server as part of the handshake. This
302
371
/// can be used in combination with the server logs to determine which Client is connected to a
@@ -472,11 +541,8 @@ pub struct ClientOptions {
472
541
pub ( crate ) heartbeat_freq_test : Option < Duration > ,
473
542
}
474
543
475
- fn default_hosts ( ) -> Vec < StreamAddress > {
476
- vec ! [ StreamAddress {
477
- hostname: "localhost" . to_string( ) ,
478
- port: Some ( 27017 ) ,
479
- } ]
544
+ fn default_hosts ( ) -> Vec < ServerAddress > {
545
+ vec ! [ ServerAddress :: default ( ) ]
480
546
}
481
547
482
548
impl Default for ClientOptions {
@@ -487,7 +553,7 @@ impl Default for ClientOptions {
487
553
488
554
#[ derive( Debug , Default , PartialEq ) ]
489
555
struct ClientOptionsParser {
490
- pub hosts : Vec < StreamAddress > ,
556
+ pub hosts : Vec < ServerAddress > ,
491
557
pub srv : bool ,
492
558
pub app_name : Option < String > ,
493
559
pub tls : Option < Tls > ,
@@ -825,11 +891,11 @@ impl ClientOptions {
825
891
if srv {
826
892
let mut resolver = SrvResolver :: new ( resolver_config. map ( |config| config. inner ) ) . await ?;
827
893
let mut config = resolver
828
- . resolve_client_options ( & options. hosts [ 0 ] . hostname )
894
+ . resolve_client_options ( & options. hosts [ 0 ] . host ( ) )
829
895
. await ?;
830
896
831
897
// Save the original SRV hostname to allow mongos polling.
832
- options. original_srv_hostname = Some ( options. hosts [ 0 ] . hostname . clone ( ) ) ;
898
+ options. original_srv_hostname = Some ( options. hosts [ 0 ] . host ( ) . to_string ( ) ) ;
833
899
834
900
// Set the ClientOptions hosts to those found during the SRV lookup.
835
901
options. hosts = config. hosts ;
@@ -1123,8 +1189,8 @@ impl ClientOptionsParser {
1123
1189
Some ( p)
1124
1190
} ;
1125
1191
1126
- Ok ( StreamAddress {
1127
- hostname : hostname. to_lowercase ( ) ,
1192
+ Ok ( ServerAddress :: Tcp {
1193
+ host : hostname. to_lowercase ( ) ,
1128
1194
port,
1129
1195
} )
1130
1196
} )
@@ -1140,7 +1206,7 @@ impl ClientOptionsParser {
1140
1206
. into ( ) ) ;
1141
1207
}
1142
1208
1143
- if hosts[ 0 ] . port . is_some ( ) {
1209
+ if hosts[ 0 ] . port ( ) . is_some ( ) {
1144
1210
return Err ( ErrorKind :: InvalidArgument {
1145
1211
message : "a port cannot be specified with 'mongodb+srv'" . into ( ) ,
1146
1212
}
@@ -1722,7 +1788,7 @@ mod tests {
1722
1788
1723
1789
use pretty_assertions:: assert_eq;
1724
1790
1725
- use super :: { ClientOptions , StreamAddress } ;
1791
+ use super :: { ClientOptions , ServerAddress } ;
1726
1792
use crate :: {
1727
1793
concern:: { Acknowledgment , ReadConcernLevel , WriteConcern } ,
1728
1794
selection_criteria:: { ReadPreference , ReadPreferenceOptions } ,
@@ -1745,9 +1811,9 @@ mod tests {
1745
1811
}
1746
1812
}
1747
1813
1748
- fn host_without_port ( hostname : & str ) -> StreamAddress {
1749
- StreamAddress {
1750
- hostname : hostname. to_string ( ) ,
1814
+ fn host_without_port ( hostname : & str ) -> ServerAddress {
1815
+ ServerAddress :: Tcp {
1816
+ host : hostname. to_string ( ) ,
1751
1817
port : None ,
1752
1818
}
1753
1819
}
@@ -1822,8 +1888,8 @@ mod tests {
1822
1888
assert_eq ! (
1823
1889
ClientOptions :: parse( uri) . await . unwrap( ) ,
1824
1890
ClientOptions {
1825
- hosts: vec![ StreamAddress {
1826
- hostname : "localhost" . to_string( ) ,
1891
+ hosts: vec![ ServerAddress :: Tcp {
1892
+ host : "localhost" . to_string( ) ,
1827
1893
port: Some ( 27017 ) ,
1828
1894
} ] ,
1829
1895
original_uri: Some ( uri. into( ) ) ,
@@ -1840,8 +1906,8 @@ mod tests {
1840
1906
assert_eq ! (
1841
1907
ClientOptions :: parse( uri) . await . unwrap( ) ,
1842
1908
ClientOptions {
1843
- hosts: vec![ StreamAddress {
1844
- hostname : "localhost" . to_string( ) ,
1909
+ hosts: vec![ ServerAddress :: Tcp {
1910
+ host : "localhost" . to_string( ) ,
1845
1911
port: Some ( 27017 ) ,
1846
1912
} ] ,
1847
1913
original_uri: Some ( uri. into( ) ) ,
@@ -1858,8 +1924,8 @@ mod tests {
1858
1924
assert_eq ! (
1859
1925
ClientOptions :: parse( uri) . await . unwrap( ) ,
1860
1926
ClientOptions {
1861
- hosts: vec![ StreamAddress {
1862
- hostname : "localhost" . to_string( ) ,
1927
+ hosts: vec![ ServerAddress :: Tcp {
1928
+ host : "localhost" . to_string( ) ,
1863
1929
port: Some ( 27017 ) ,
1864
1930
} ] ,
1865
1931
read_concern: Some ( ReadConcernLevel :: Custom ( "foo" . to_string( ) ) . into( ) ) ,
@@ -1886,8 +1952,8 @@ mod tests {
1886
1952
assert_eq ! (
1887
1953
ClientOptions :: parse( uri) . await . unwrap( ) ,
1888
1954
ClientOptions {
1889
- hosts: vec![ StreamAddress {
1890
- hostname : "localhost" . to_string( ) ,
1955
+ hosts: vec![ ServerAddress :: Tcp {
1956
+ host : "localhost" . to_string( ) ,
1891
1957
port: Some ( 27017 ) ,
1892
1958
} ] ,
1893
1959
write_concern: Some ( write_concern) ,
@@ -1908,8 +1974,8 @@ mod tests {
1908
1974
assert_eq ! (
1909
1975
ClientOptions :: parse( uri) . await . unwrap( ) ,
1910
1976
ClientOptions {
1911
- hosts: vec![ StreamAddress {
1912
- hostname : "localhost" . to_string( ) ,
1977
+ hosts: vec![ ServerAddress :: Tcp {
1978
+ host : "localhost" . to_string( ) ,
1913
1979
port: Some ( 27017 ) ,
1914
1980
} ] ,
1915
1981
write_concern: Some ( write_concern) ,
@@ -1938,8 +2004,8 @@ mod tests {
1938
2004
assert_eq ! (
1939
2005
ClientOptions :: parse( uri) . await . unwrap( ) ,
1940
2006
ClientOptions {
1941
- hosts: vec![ StreamAddress {
1942
- hostname : "localhost" . to_string( ) ,
2007
+ hosts: vec![ ServerAddress :: Tcp {
2008
+ host : "localhost" . to_string( ) ,
1943
2009
port: Some ( 27017 ) ,
1944
2010
} ] ,
1945
2011
write_concern: Some ( write_concern) ,
@@ -1980,8 +2046,8 @@ mod tests {
1980
2046
assert_eq ! (
1981
2047
ClientOptions :: parse( uri) . await . unwrap( ) ,
1982
2048
ClientOptions {
1983
- hosts: vec![ StreamAddress {
1984
- hostname : "localhost" . to_string( ) ,
2049
+ hosts: vec![ ServerAddress :: Tcp {
2050
+ host : "localhost" . to_string( ) ,
1985
2051
port: Some ( 27017 ) ,
1986
2052
} ] ,
1987
2053
write_concern: Some ( write_concern) ,
@@ -2004,8 +2070,8 @@ mod tests {
2004
2070
assert_eq ! (
2005
2071
ClientOptions :: parse( uri) . await . unwrap( ) ,
2006
2072
ClientOptions {
2007
- hosts: vec![ StreamAddress {
2008
- hostname : "localhost" . to_string( ) ,
2073
+ hosts: vec![ ServerAddress :: Tcp {
2074
+ host : "localhost" . to_string( ) ,
2009
2075
port: Some ( 27017 ) ,
2010
2076
} ] ,
2011
2077
write_concern: Some ( write_concern) ,
@@ -2037,12 +2103,12 @@ mod tests {
2037
2103
ClientOptions :: parse( uri) . await . unwrap( ) ,
2038
2104
ClientOptions {
2039
2105
hosts: vec![
2040
- StreamAddress {
2041
- hostname : "localhost" . to_string( ) ,
2106
+ ServerAddress :: Tcp {
2107
+ host : "localhost" . to_string( ) ,
2042
2108
port: None ,
2043
2109
} ,
2044
- StreamAddress {
2045
- hostname : "localhost" . to_string( ) ,
2110
+ ServerAddress :: Tcp {
2111
+ host : "localhost" . to_string( ) ,
2046
2112
port: Some ( 27018 ) ,
2047
2113
} ,
2048
2114
] ,
0 commit comments