Skip to content

Commit a77eea9

Browse files
RUST-995 Add tokio-sync feature flag (#578)
1 parent 8be820f commit a77eea9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+482
-543
lines changed

.evergreen/check-clippy.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ cargo clippy --all-targets -p mongodb -- -D warnings
1010
cargo clippy --all-targets -p mongodb --features zstd-compression,snappy-compression,zlib-compression -- -D warnings
1111
cargo clippy --all-targets --no-default-features --features async-std-runtime -p mongodb -- -D warnings
1212
cargo clippy --all-targets --no-default-features --features sync -p mongodb -- -D warnings
13+
cargo clippy --all-targets --features tokio-sync -p mongodb -- -D warnings

.evergreen/check-rustdoc.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ set -o errexit
66
cargo +nightly rustdoc -- -D warnings --cfg docsrs
77
cargo +nightly rustdoc --no-default-features --features async-std-runtime -- -D warnings --cfg docsrs
88
cargo +nightly rustdoc --no-default-features --features sync -- -D warnings --cfg docsrs
9+
cargo +nightly rustdoc --features tokio-sync -- -D warnings --cfg docsrs

.evergreen/compile-only-tokio.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ if [[ $RUST_VERSION == "nightly" ]]; then
1313
fi
1414

1515
rustup run $RUST_VERSION cargo build --features $FEATURE_FLAGS
16+
rustup run $RUST_VERSION cargo build --features tokio-sync,$FEATURE_FLAGS

.evergreen/run-tokio-tests.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ FEATURE_FLAGS="zstd-compression,snappy-compression,zlib-compression"
1515
echo "cargo test options: --features $FEATURE_FLAGS ${OPTIONS}"
1616

1717
RUST_BACKTRACE=1 cargo test --features $FEATURE_FLAGS $OPTIONS | tee results.json
18-
cat results.json | cargo2junit > results.xml
18+
cat results.json | cargo2junit > async-tests.xml
19+
RUST_BACKTRACE=1 cargo test sync --features tokio-sync,$FEATURE_FLAGS $OPTIONS | tee sync-tests.json
20+
cat sync-tests.json | cargo2junit > sync-tests.xml
21+
RUST_BACKTRACE=1 cargo test --doc sync --features tokio-sync,$FEATURE_FLAGS $OPTIONS | tee sync-doc-tests.json
22+
cat sync-doc-tests.json | cargo2junit > sync-doc-tests.xml
23+
24+
junit-report-merger results.xml async-tests.xml sync-tests.xml sync-doc-tests.xml

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ default = ["tokio-runtime"]
2424
tokio-runtime = ["tokio/macros", "tokio/net", "tokio/rt", "tokio/time", "serde_bytes"]
2525
async-std-runtime = ["async-std", "async-std/attributes", "async-std-resolver", "tokio-util/compat"]
2626
sync = ["async-std-runtime"]
27+
tokio-sync = ["tokio-runtime"]
2728

2829
# Enable support for v0.4 of the chrono crate in the public API of the BSON library.
2930
bson-chrono-0_4 = ["bson/chrono-0_4"]

src/change_stream/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::{
4545
/// A `ChangeStream` can be iterated like any other [`Stream`]:
4646
///
4747
/// ```
48-
/// # #[cfg(not(feature = "sync"))]
48+
/// # #[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
4949
/// # use futures::stream::StreamExt;
5050
/// # use mongodb::{Client, error::Result, bson::doc,
5151
/// # change_stream::event::ChangeStreamEvent};

src/client/auth/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ pub struct Credential {
352352
}
353353

354354
impl Credential {
355-
#[cfg(all(test, not(feature = "sync")))]
355+
#[cfg(all(test, not(feature = "sync"), not(feature = "tokio-sync")))]
356356
pub(crate) fn into_document(mut self) -> Document {
357357
use crate::bson::Bson;
358358

src/client/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ const DEFAULT_SERVER_SELECTION_TIMEOUT: Duration = Duration::from_secs(30);
5252
/// so it can safely be shared across threads or async tasks. For example:
5353
///
5454
/// ```rust
55-
/// # #[cfg(not(feature = "sync"))]
55+
/// # #[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
5656
/// # use mongodb::{bson::Document, Client, error::Result};
5757
/// # #[cfg(feature = "async-std-runtime")]
5858
/// # use async_std::task;
5959
/// # #[cfg(feature = "tokio-runtime")]
6060
/// # use tokio::task;
6161
/// #
62-
/// # #[cfg(not(feature = "sync"))]
62+
/// # #[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
6363
/// # async fn start_workers() -> Result<()> {
6464
/// let client = Client::with_uri_str("mongodb://example.com").await?;
6565
///
@@ -431,7 +431,7 @@ impl Client {
431431
}
432432
}
433433

434-
#[cfg(all(test, not(feature = "sync")))]
434+
#[cfg(all(test, not(feature = "sync"), not(feature = "tokio-sync")))]
435435
pub(crate) async fn get_hosts(&self) -> Vec<String> {
436436
let servers = self.inner.topology.servers().await;
437437
servers

src/client/options/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(all(test, not(feature = "sync")))]
1+
#[cfg(all(test, not(feature = "sync"), not(feature = "tokio-sync")))]
22
mod test;
33

44
mod resolver_config;
@@ -54,6 +54,9 @@ use crate::{
5454
srv::{OriginalSrvInfo, SrvResolver},
5555
};
5656

57+
#[cfg(any(feature = "sync", feature = "tokio-sync"))]
58+
use crate::runtime;
59+
5760
pub use resolver_config::ResolverConfig;
5861

5962
const DEFAULT_PORT: u16 = 27017;
@@ -244,7 +247,7 @@ impl ServerAddress {
244247
})
245248
}
246249

247-
#[cfg(all(test, not(feature = "sync")))]
250+
#[cfg(all(test, not(feature = "sync"), not(feature = "tokio-sync")))]
248251
pub(crate) fn into_document(self) -> Document {
249252
match self {
250253
Self::Tcp { host, port } => {
@@ -1038,17 +1041,17 @@ impl ClientOptions {
10381041
///
10391042
/// Note: if the `sync` feature is enabled, then this method will be replaced with [the sync
10401043
/// version](#method.parse-1).
1041-
#[cfg(not(feature = "sync"))]
1044+
#[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
10421045
pub async fn parse(s: impl AsRef<str>) -> Result<Self> {
10431046
Self::parse_uri(s, None).await
10441047
}
10451048

10461049
/// This method will be present if the `sync` feature is enabled. It's otherwise identical to
10471050
/// [the async version](#method.parse)
1048-
#[cfg(any(feature = "sync", docsrs))]
1049-
#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
1051+
#[cfg(any(feature = "sync", feature = "tokio-sync", docsrs))]
1052+
#[cfg_attr(docsrs, doc(cfg(any(feature = "sync", feature = "tokio-sync"))))]
10501053
pub fn parse(s: impl AsRef<str>) -> Result<Self> {
1051-
crate::RUNTIME.block_on(Self::parse_uri(s.as_ref(), None))
1054+
runtime::block_on(Self::parse_uri(s.as_ref(), None))
10521055
}
10531056

10541057
/// Parses a MongoDB connection string into a `ClientOptions` struct.
@@ -1065,7 +1068,7 @@ impl ClientOptions {
10651068
///
10661069
/// Note: if the `sync` feature is enabled, then this method will be replaced with [the sync
10671070
/// version](#method.parse_with_resolver_config-1).
1068-
#[cfg(not(feature = "sync"))]
1071+
#[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
10691072
pub async fn parse_with_resolver_config(
10701073
uri: impl AsRef<str>,
10711074
resolver_config: ResolverConfig,
@@ -1075,10 +1078,10 @@ impl ClientOptions {
10751078

10761079
/// This method will be present if the `sync` feature is enabled. It's otherwise identical to
10771080
/// [the async version](#method.parse_with_resolver_config)
1078-
#[cfg(any(feature = "sync", docsrs))]
1079-
#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
1081+
#[cfg(any(feature = "sync", feature = "tokio-sync", docsrs))]
1082+
#[cfg_attr(docsrs, doc(cfg(any(feature = "sync", feature = "tokio-sync"))))]
10801083
pub fn parse_with_resolver_config(uri: &str, resolver_config: ResolverConfig) -> Result<Self> {
1081-
crate::RUNTIME.block_on(Self::parse_uri(uri, Some(resolver_config)))
1084+
runtime::block_on(Self::parse_uri(uri, Some(resolver_config)))
10821085
}
10831086

10841087
/// Populate this `ClientOptions` from the given URI, optionally using the resolver config for
@@ -2015,7 +2018,7 @@ impl ClientOptionsParser {
20152018
}
20162019
}
20172020

2018-
#[cfg(all(test, not(feature = "sync")))]
2021+
#[cfg(all(test, not(feature = "sync"), not(feature = "tokio-sync")))]
20192022
mod tests {
20202023
use std::time::Duration;
20212024

src/client/session/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ use crate::{
1818
error::{ErrorKind, Result},
1919
operation::{AbortTransaction, CommitTransaction, Operation},
2020
options::{SessionOptions, TransactionOptions},
21+
runtime,
2122
sdam::{ServerInfo, TransactionSupportStatus},
2223
selection_criteria::SelectionCriteria,
2324
Client,
24-
RUNTIME,
2525
};
2626
pub use cluster_time::ClusterTime;
2727
pub(super) use pool::ServerSessionPool;
@@ -607,14 +607,14 @@ impl Drop for ClientSession {
607607
snapshot_time: self.snapshot_time,
608608
operation_time: self.operation_time,
609609
};
610-
RUNTIME.execute(async move {
610+
runtime::execute(async move {
611611
let mut session: ClientSession = dropped_session.into();
612612
let _result = session.abort_transaction().await;
613613
});
614614
} else {
615615
let client = self.client.clone();
616616
let server_session = self.server_session.clone();
617-
RUNTIME.execute(async move {
617+
runtime::execute(async move {
618618
client.check_in_server_session(server_session).await;
619619
});
620620
}

0 commit comments

Comments
 (0)