Skip to content

Commit 8c62064

Browse files
authored
RUST-1208 Future-proof features (#1062)
1 parent 719beab commit 8c62064

File tree

17 files changed

+140
-79
lines changed

17 files changed

+140
-79
lines changed

Cargo.toml

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,37 @@ exclude = [
2727
]
2828

2929
[features]
30-
default = []
30+
default = ["compat-3-0-0", "rustls-tls", "dns-resolver"]
31+
compat-3-0-0 = []
3132
sync = []
32-
openssl-tls = ["openssl", "openssl-probe", "tokio-openssl"]
33-
34-
# Enable support for v0.4 of the chrono crate in the public API of the BSON library.
35-
bson-chrono-0_4 = ["bson/chrono-0_4"]
36-
37-
# Enable support for the serde_with crate in the BSON library.
38-
bson-serde_with = ["bson/serde_with"]
39-
40-
# Enable support for v0.8 of the uuid crate in the public API of the BSON library.
41-
bson-uuid-0_8 = ["bson/uuid-0_8"]
42-
43-
# Enable support for v1.x of the uuid crate in the public API of the BSON library.
44-
bson-uuid-1 = ["bson/uuid-1"]
33+
rustls-tls = ["dep:rustls", "dep:rustls-pemfile", "dep:tokio-rustls"]
34+
openssl-tls = ["dep:openssl", "dep:openssl-probe", "dep:tokio-openssl"]
35+
dns-resolver = ["dep:trust-dns-resolver", "dep:trust-dns-proto"]
4536

4637
# Enable support for MONGODB-AWS authentication.
4738
# This can only be used with the tokio-runtime feature flag.
48-
aws-auth = ["reqwest"]
39+
aws-auth = ["dep:reqwest"]
4940

5041
# Enable support for on-demand Azure KMS credentials.
5142
# This can only be used with the tokio-runtime feature flag.
52-
azure-kms = ["reqwest"]
43+
azure-kms = ["dep:reqwest"]
5344

5445
# Enable support for on-demand GCP KMS credentials.
5546
# This can only be used with the tokio-runtime feature flag.
56-
gcp-kms = ["reqwest"]
47+
gcp-kms = ["dep:reqwest"]
5748

58-
zstd-compression = ["zstd"]
59-
zlib-compression = ["flate2"]
60-
snappy-compression = ["snap"]
49+
zstd-compression = ["dep:zstd"]
50+
zlib-compression = ["dep:flate2"]
51+
snappy-compression = ["dep:snap"]
6152

6253
# Enables support for client-side field level encryption and queryable encryption.
6354
# The In Use Encryption API is unstable and may have backwards-incompatible changes in minor version updates.
64-
in-use-encryption-unstable = ["mongocrypt", "rayon", "num_cpus"]
55+
in-use-encryption-unstable = ["dep:mongocrypt", "dep:rayon", "dep:num_cpus"]
6556

6657
# Enables support for emitting tracing events.
6758
# The tracing API is unstable and may have backwards-incompatible changes in minor version updates.
6859
# TODO: pending https://github.com/tokio-rs/tracing/issues/2036 stop depending directly on log.
69-
tracing-unstable = ["tracing", "log"]
60+
tracing-unstable = ["dep:tracing", "dep:log"]
7061

7162
[dependencies]
7263
action_macro = { path = "action_macro" }
@@ -95,7 +86,7 @@ percent-encoding = "2.0.0"
9586
rand = { version = "0.8.3", features = ["small_rng"] }
9687
rayon = { version = "1.5.3", optional = true }
9788
rustc_version_runtime = "0.2.1"
98-
rustls-pemfile = "1.0.1"
89+
rustls-pemfile = { version = "1.0.1", optional = true }
9990
serde_with = "1.3.1"
10091
sha-1 = "0.10.0"
10192
sha2 = "0.10.2"
@@ -107,8 +98,8 @@ take_mut = "0.2.2"
10798
thiserror = "1.0.24"
10899
tokio-openssl = { version = "0.6.3", optional = true }
109100
tracing = { version = "0.1.36", optional = true }
110-
trust-dns-proto = "0.21.2"
111-
trust-dns-resolver = "0.21.2"
101+
trust-dns-proto = { version = "0.21.2", optional = true }
102+
trust-dns-resolver = { version = "0.21.2", optional = true }
112103
typed-builder = "0.10.0"
113104
webpki-roots = "0.25.2"
114105
zstd = { version = "0.11.2", optional = true }
@@ -125,6 +116,7 @@ features = ["json", "rustls-tls"]
125116

126117
[dependencies.rustls]
127118
version = "0.21.6"
119+
optional = true
128120
features = ["dangerous_configuration"]
129121

130122
[dependencies.serde]
@@ -140,6 +132,7 @@ features = ["io-util", "sync", "macros", "net", "process", "rt", "time"]
140132

141133
[dependencies.tokio-rustls]
142134
version = "0.24.1"
135+
optional = true
143136
features = ["dangerous_configuration"]
144137

145138
[dependencies.tokio-util]

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ features = ["sync"]
6464
|:---------------------|:--------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------|:--------|
6565
| `sync` | Expose the synchronous API (`mongodb::sync`). | n/a | no |
6666
| `aws-auth` | Enable support for the MONGODB-AWS authentication mechanism. | `reqwest` | no |
67-
| `bson-uuid-0_8` | Enable support for v0.8 of the [`uuid`](docs.rs/uuid/0.8) crate in the public API of the re-exported `bson` crate. | n/a | no |
68-
| `bson-uuid-1` | Enable support for v1.x of the [`uuid`](docs.rs/uuid/1.0) crate in the public API of the re-exported `bson` crate. | n/a | no |
69-
| `bson-chrono-0_4` | Enable support for v0.4 of the [`chrono`](docs.rs/chrono/0.4) crate in the public API of the re-exported `bson` crate. | n/a | no |
70-
| `bson-serde_with` | Enable support for the [`serde_with`](docs.rs/serde_with/latest) crate in the public API of the re-exported `bson` crate. | `serde_with` | no |
7167
| `zlib-compression` | Enable support for compressing messages with [`zlib`](https://zlib.net/) | `flate2` | no |
7268
| `zstd-compression` | Enable support for compressing messages with [`zstd`](http://facebook.github.io/zstd/). | `zstd` | no |
7369
| `snappy-compression` | Enable support for compressing messages with [`snappy`](http://google.github.io/snappy/) | `snap` | no |

manual/src/installation_features.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ features = ["sync"]
2222
|:---------------------|:--------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------|:--------|
2323
| `sync` | Expose the synchronous API (`mongodb::sync`). | n/a | no |
2424
| `aws-auth` | Enable support for the MONGODB-AWS authentication mechanism. | `reqwest` 0.11 | no |
25-
| `bson-uuid-0_8` | Enable support for v0.8 of the [`uuid`](docs.rs/uuid/0.8) crate in the public API of the re-exported `bson` crate. | n/a | no |
26-
| `bson-uuid-1` | Enable support for v1.x of the [`uuid`](docs.rs/uuid/1.0) crate in the public API of the re-exported `bson` crate. | n/a | no |
27-
| `bson-chrono-0_4` | Enable support for v0.4 of the [`chrono`](docs.rs/chrono/0.4) crate in the public API of the re-exported `bson` crate. | n/a | no |
28-
| `bson-serde_with` | Enable support for the [`serde_with`](docs.rs/serde_with/latest) crate in the public API of the re-exported `bson` crate. | `serde_with` 1.0 | no |
2925
| `zlib-compression` | Enable support for compressing messages with [`zlib`](https://zlib.net/) | `flate2` 1.0 | no |
3026
| `zstd-compression` | Enable support for compressing messages with [`zstd`](http://facebook.github.io/zstd/). This flag requires Rust version 1.54. | `zstd` 0.9.0 | no |
3127
| `snappy-compression` | Enable support for compressing messages with [`snappy`](http://google.github.io/snappy/) | `snap` 1.0.5 | no |

src/action/client_options.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub struct ParseConnectionString {
9595
impl ParseConnectionString {
9696
/// In the case that "mongodb+srv" is used, SRV and TXT record lookups will be done using the
9797
/// provided `ResolverConfig` as part of this method.
98+
#[cfg(feature = "dns-resolver")]
9899
pub fn resolver_config(mut self, value: ResolverConfig) -> Self {
99100
self.resolver_config = Some(value);
100101
self

src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl Client {
478478
}
479479
}
480480

481-
#[cfg(test)]
481+
#[cfg(all(test, feature = "dns-resolver"))]
482482
pub(crate) fn get_hosts(&self) -> Vec<String> {
483483
let watcher = self.inner.topology.watch();
484484
let state = watcher.peek_latest();

src/client/options.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ mod parse;
55
mod resolver_config;
66

77
use std::{
8-
borrow::Cow,
98
cmp::Ordering,
109
collections::HashSet,
1110
convert::TryFrom,
@@ -45,7 +44,10 @@ use crate::{
4544
srv::{OriginalSrvInfo, SrvResolver},
4645
};
4746

47+
#[cfg(feature = "dns-resolver")]
4848
pub use resolver_config::ResolverConfig;
49+
#[cfg(not(feature = "dns-resolver"))]
50+
pub(crate) use resolver_config::ResolverConfig;
4951

5052
pub(crate) const DEFAULT_PORT: u16 = 27017;
5153

@@ -253,14 +255,16 @@ impl ServerAddress {
253255
})
254256
}
255257

256-
pub(crate) fn host(&self) -> Cow<'_, str> {
258+
#[cfg(feature = "dns-resolver")]
259+
pub(crate) fn host(&self) -> std::borrow::Cow<'_, str> {
257260
match self {
258-
Self::Tcp { host, .. } => Cow::Borrowed(host.as_str()),
261+
Self::Tcp { host, .. } => std::borrow::Cow::Borrowed(host.as_str()),
259262
#[cfg(unix)]
260263
Self::Unix { path } => path.to_string_lossy(),
261264
}
262265
}
263266

267+
#[cfg(feature = "dns-resolver")]
264268
pub(crate) fn port(&self) -> Option<u16> {
265269
match self {
266270
Self::Tcp { port, .. } => *port,
@@ -597,6 +601,7 @@ pub struct ClientOptions {
597601
#[builder(default, setter(skip))]
598602
#[serde(skip)]
599603
#[derivative(Debug = "ignore")]
604+
#[cfg(feature = "dns-resolver")]
600605
pub(crate) resolver_config: Option<ResolverConfig>,
601606

602607
/// Control test behavior of the client.
@@ -932,8 +937,7 @@ impl HostInfo {
932937
Ok(match self {
933938
Self::HostIdentifiers(hosts) => ResolvedHostInfo::HostIdentifiers(hosts),
934939
Self::DnsRecord(hostname) => {
935-
let mut resolver =
936-
SrvResolver::new(resolver_config.clone().map(|config| config.inner)).await?;
940+
let mut resolver = SrvResolver::new(resolver_config.clone()).await?;
937941
let config = resolver.resolve_client_options(&hostname).await?;
938942
ResolvedHostInfo::DnsRecord { hostname, config }
939943
}
@@ -1264,6 +1268,17 @@ impl ClientOptions {
12641268
MIN_HEARTBEAT_FREQUENCY
12651269
}
12661270
}
1271+
1272+
pub(crate) fn resolver_config(&self) -> Option<&ResolverConfig> {
1273+
#[cfg(feature = "dns-resolver")]
1274+
{
1275+
self.resolver_config.as_ref()
1276+
}
1277+
#[cfg(not(feature = "dns-resolver"))]
1278+
{
1279+
None
1280+
}
1281+
}
12671282
}
12681283

12691284
/// Splits a string into a section before a given index and a section exclusively after the index.
@@ -1359,6 +1374,13 @@ impl ConnectionString {
13591374
.into())
13601375
}
13611376
};
1377+
#[cfg(not(feature = "dns-resolver"))]
1378+
if srv {
1379+
return Err(Error::invalid_argument(
1380+
"mongodb+srv connection strings cannot be used when the 'dns-resolver' feature is \
1381+
disabled",
1382+
));
1383+
}
13621384

13631385
let after_scheme = &s[end_of_scheme + 3..];
13641386

src/client/options/parse.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ impl Action for ParseConnectionString {
1919
.is_some();
2020
let host_info = std::mem::take(&mut conn_str.host_info);
2121
let mut options = ClientOptions::from_connection_string(conn_str);
22-
options.resolver_config = self.resolver_config.clone();
22+
#[cfg(feature = "dns-resolver")]
23+
{
24+
options.resolver_config = self.resolver_config.clone();
25+
}
2326

2427
let resolved = host_info.resolve(self.resolver_config).await?;
2528
options.hosts = match resolved {
@@ -146,6 +149,7 @@ impl ClientOptions {
146149
original_srv_info: None,
147150
#[cfg(test)]
148151
original_uri: Some(conn_str.original_uri),
152+
#[cfg(feature = "dns-resolver")]
149153
resolver_config: None,
150154
server_api: None,
151155
load_balanced: conn_str.load_balanced,

src/client/options/resolver_config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(feature = "dns-resolver")]
12
use trust_dns_resolver::config::ResolverConfig as TrustDnsResolverConfig;
23

34
/// Configuration for the upstream nameservers to use for resolution.
@@ -6,9 +7,11 @@ use trust_dns_resolver::config::ResolverConfig as TrustDnsResolverConfig;
67
/// API stability.
78
#[derive(Clone, Debug, PartialEq)]
89
pub struct ResolverConfig {
10+
#[cfg(feature = "dns-resolver")]
911
pub(crate) inner: TrustDnsResolverConfig,
1012
}
1113

14+
#[cfg(feature = "dns-resolver")]
1215
impl ResolverConfig {
1316
/// Creates a default configuration, using 1.1.1.1, 1.0.0.1 and 2606:4700:4700::1111,
1417
/// 2606:4700:4700::1001 (thank you, Cloudflare).

src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ impl Error {
267267
self.labels.insert(label);
268268
}
269269

270+
#[cfg(feature = "dns-resolver")]
270271
pub(crate) fn from_resolve_error(error: trust_dns_resolver::error::ResolveError) -> Self {
271272
ErrorKind::DnsResolve {
272273
message: error.to_string(),

src/lib.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@
3535
//! |:-----------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------|
3636
//! | `sync` | Expose the synchronous API (`mongodb::sync`). | no |
3737
//! | `aws-auth` | Enable support for the MONGODB-AWS authentication mechanism. | no |
38-
//! | `bson-uuid-0_8` | Enable support for v0.8 of the [`uuid`](docs.rs/uuid/0.8) crate in the public API of the re-exported `bson` crate. | no |
39-
//! | `bson-uuid-1` | Enable support for v1.x of the [`uuid`](docs.rs/uuid/1.0) crate in the public API of the re-exported `bson` crate. | no |
40-
//! | `bson-chrono-0_4` | Enable support for v0.4 of the [`chrono`](docs.rs/chrono/0.4) crate in the public API of the re-exported `bson` crate. | no |
41-
//! | `bson-serde_with` | Enable support for the [`serde_with`](docs.rs/serde_with/latest) crate in the public API of the re-exported `bson` crate. | no |
4238
//! | `zlib-compression` | Enable support for compressing messages with [`zlib`](https://zlib.net/). | no |
4339
//! | `zstd-compression` | Enable support for compressing messages with [`zstd`](http://facebook.github.io/zstd/). | no |
4440
//! | `snappy-compression` | Enable support for compressing messages with [`snappy`](http://google.github.io/snappy/). | no |
@@ -142,9 +138,9 @@
142138
//! ```
143139
//!
144140
//! ### Finding documents in a collection
145-
//! Results from queries are generally returned via [`Cursor`](struct.Cursor.html), a struct which streams
146-
//! the results back from the server as requested. The [`Cursor`](struct.Cursor.html) type implements the
147-
//! [`Stream`](https://docs.rs/futures/latest/futures/stream/trait.Stream.html) trait from
141+
//! Results from queries are generally returned via [`Cursor`](struct.Cursor.html), a struct which
142+
//! streams the results back from the server as requested. The [`Cursor`](struct.Cursor.html) type
143+
//! implements the [`Stream`](https://docs.rs/futures/latest/futures/stream/trait.Stream.html) trait from
148144
//! the [`futures`](https://crates.io/crates/futures) crate, and in order to access its streaming
149145
//! functionality you need to import at least one of the
150146
//! [`StreamExt`](https://docs.rs/futures/latest/futures/stream/trait.StreamExt.html) or
@@ -236,14 +232,14 @@
236232
//! In async Rust, it is common to implement cancellation and timeouts by dropping a future after a
237233
//! certain period of time instead of polling it to completion. This is how
238234
//! [`tokio::time::timeout`](https://docs.rs/tokio/1.10.1/tokio/time/fn.timeout.html) works, for
239-
//! example. However, doing this with futures returned by the driver can leave the driver's internals in
240-
//! an inconsistent state, which may lead to unpredictable or incorrect behavior (see RUST-937 for more
241-
//! details). As such, it is **_highly_** recommended to poll all futures returned from the driver to
242-
//! completion. In order to still use timeout mechanisms like `tokio::time::timeout` with the driver,
243-
//! one option is to spawn tasks and time out on their
235+
//! example. However, doing this with futures returned by the driver can leave the driver's
236+
//! internals in an inconsistent state, which may lead to unpredictable or incorrect behavior (see
237+
//! RUST-937 for more details). As such, it is **_highly_** recommended to poll all futures returned
238+
//! from the driver to completion. In order to still use timeout mechanisms like
239+
//! `tokio::time::timeout` with the driver, one option is to spawn tasks and time out on their
244240
//! [`JoinHandle`](https://docs.rs/tokio/1.10.1/tokio/task/struct.JoinHandle.html) futures instead of on
245-
//! the driver's futures directly. This will ensure the driver's futures will always be completely polled
246-
//! while also allowing the application to continue in the event of a timeout.
241+
//! the driver's futures directly. This will ensure the driver's futures will always be completely
242+
//! polled while also allowing the application to continue in the event of a timeout.
247243
//!
248244
//! e.g.
249245
//! ``` rust
@@ -268,8 +264,8 @@
268264
//!
269265
//! ## Minimum supported Rust version (MSRV)
270266
//!
271-
//! The MSRV for this crate is currently 1.64.0. This will be rarely be increased, and if it ever is,
272-
//! it will only happen in a minor or major version release.
267+
//! The MSRV for this crate is currently 1.64.0. This will be rarely be increased, and if it ever
268+
//! is, it will only happen in a minor or major version release.
273269
274270
#![warn(missing_docs)]
275271
#![warn(rustdoc::missing_crate_level_docs)]
@@ -289,7 +285,6 @@
289285
#![cfg_attr(test, type_length_limit = "80000000")]
290286
#![doc(html_root_url = "https://docs.rs/mongodb/2.8.0")]
291287

292-
293288
#[macro_use]
294289
pub mod options;
295290

@@ -318,8 +313,8 @@ mod index;
318313
mod operation;
319314
pub mod results;
320315
pub(crate) mod runtime;
321-
mod search_index;
322316
mod sdam;
317+
mod search_index;
323318
mod selection_criteria;
324319
mod serde_util;
325320
mod srv;
@@ -344,7 +339,17 @@ pub use crate::{
344339
gridfs::{GridFsBucket, GridFsDownloadStream, GridFsUploadStream},
345340
};
346341

347-
pub use {client::session::ClusterTime, coll::Namespace, index::IndexModel, sdam::public::*, search_index::SearchIndexModel};
342+
pub use client::session::ClusterTime;
343+
pub use coll::Namespace;
344+
pub use index::IndexModel;
345+
pub use sdam::public::*;
346+
pub use search_index::SearchIndexModel;
348347

349348
/// A boxed future.
350-
pub type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
349+
pub type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
350+
351+
#[cfg(not(feature = "compat-3-0-0"))]
352+
compile_error!(
353+
"The feature 'compat-3-0-0' must be enabled to ensure forward compatibility with future \
354+
versions of this crate."
355+
);

0 commit comments

Comments
 (0)