@@ -14,50 +14,54 @@ Overview
14
14
--------
15
15
16
16
In this guide, you can learn about the {+driver-short+}'s :ref:`asynchronous <rust-runtimes-configure-async>`
17
- and :ref:`synchronous <rust-runtimes-configure-sync>` APIs.
18
- This guide explains how to enable the available APIs and structure your code to use each.
17
+ and :ref:`synchronous <rust-runtimes-configure-sync>` APIs. This guide explains how to enable the available APIs
18
+ and structure your code to use each.
19
19
20
- The {+driver-short+} supports ``tokio`` and ``async-std``, two popular asynchronous runtime crates.
21
- By default, the driver uses the ``tokio`` asynchronous runtime, but you can select a specific runtime
22
- by adding feature flags to the ``mongodb`` dependency in your ``Cargo.toml`` file.
20
+ The {+driver-short+} supports the ``tokio`` asynchronous runtime crate, which is the default
21
+ runtime.
23
22
24
- The driver also includes a synchronous API for use cases that require blocking, or when parallelism is not necessary.
25
- You can select the synchronous API by adding feature flags to the ``mongodb`` dependency in your ``Cargo.toml`` file.
23
+ .. important::
26
24
27
- .. _rust-runtimes-configure-async:
25
+ Beginning in {+driver-short+} v3.0, the ``tokio`` runtime is the only asynchronous runtime crate that
26
+ the driver supports. Previous versions of the driver also support the ``async-std`` runtime crate.
27
+ If your application uses the ``async-std`` runtime, you can start a ``tokio`` runtime in the same
28
+ application by creating a ``Runtime`` struct instance and wrapping driver operations with the ``Runtime::spawn()``
29
+ method. Ensure that you use the same ``Runtime`` instance for instantiating a ``Client`` and calling
30
+ driver methods on that ``Client`` instance.
31
+
32
+ For an example that instantiates a ``Runtime`` and calls the ``spawn()`` method, see the `tokio documentation
33
+ <https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html#examples-2>`__.
28
34
29
- Configure the Asynchronous Runtime
30
- ----------------------------------
35
+ The driver also includes a synchronous API for use cases that require blocking, or when parallelism
36
+ is not necessary. You can select the synchronous API by adding feature flags to the ``mongodb`` dependency
37
+ in your ``Cargo.toml`` file.
31
38
32
- The driver uses the ``tokio`` runtime by default.
33
- You can explicitly choose a runtime by adding the ``"tokio-runtime"`` or ``"async-std-runtime"``
34
- feature flags to the ``mongodb`` dependency.
39
+ This guide includes the following sections:
35
40
36
- Select from the following tabs to see how to add feature flags for each corresponding crate:
41
+ - :ref:`rust-runtimes-configure-async` shows how to configure your application to use the
42
+ ``tokio`` asynchronous runtime
37
43
38
- .. tabs::
44
+ - :ref:`rust-runtimes-configure-sync` shows how to configure your application to use the
45
+ ``sync`` and ``tokio-sync`` synchronous runtimes
39
46
40
- .. tab:: tokio
41
- :tabid: asynchronous-api-tokio
47
+ - :ref:`rust-runtimes-configure-both` shows how to enable both the asynchronous and synchronous
48
+ runtimes APIs in your application
42
49
43
- .. code-block:: toml
44
-
45
- [dependencies.mongodb]
46
- version = "{+version+}"
47
- features = ["tokio-runtime"]
50
+ - :ref:`rust-addtl-info-runtimes` provides links to resources and API documentation for types
51
+ and methods mentioned in this guide
48
52
49
- .. tab:: async-std
50
- :tabid: asynchronous-api-async-std
53
+ .. _rust-runtimes-configure-async:
51
54
52
- .. code-block:: toml
55
+ Configure the Asynchronous Runtime
56
+ ----------------------------------
53
57
54
- [dependencies.mongodb]
55
- version = "{+version+}"
56
- default-features = false
57
- features = ["async-std"]
58
+ The driver uses the ``tokio`` runtime by default, so you can use this runtime without specifying any
59
+ feature flags in your project's ``Cargo.toml`` file.
58
60
59
- For more information on installing the driver and adding feature flags,
60
- see the :ref:`rust-quick-start-download-and-install` step of the Quick Start.
61
+ You can explicitly choose the ``tokio`` runtime by adding the ``"tokio-runtime"`` feature flag to
62
+ the ``mongodb`` dependency. For more information on installing the driver and adding feature flags,
63
+ including an example that specifies the ``"tokio-runtime"`` feature flag, see the
64
+ :ref:`rust-quick-start-download-and-install` step of the Quick Start.
61
65
62
66
Tokio Runtime Example
63
67
~~~~~~~~~~~~~~~~~~~~~
@@ -113,6 +117,8 @@ When the ``insert_one`` method runs inside the ``for`` loop, the driver waits fo
113
117
:language: rust
114
118
:dedent:
115
119
120
+ .. _rust-runtimes-configure-both:
121
+
116
122
Use Both Asynchronous and Synchronous APIs
117
123
------------------------------------------
118
124
@@ -130,6 +136,8 @@ For example, to enable both ``tokio`` runtimes, you can add the ``tokio`` depend
130
136
version = "{+version+}"
131
137
features = ["tokio-sync"]
132
138
139
+ .. _rust-addtl-info-runtimes:
140
+
133
141
Additional Information
134
142
----------------------
135
143
0 commit comments