Skip to content

Commit ebea653

Browse files
committed
use cargo add when adding dependencies
This picks specific latest x.y.z version available, which is generally what most users should be doing when writing new applications rather than choosing a general version. This builds upon the tokio website dependencies being automatically updated by dependabot. If these fail to build in a particular version, then these cargo add commands should be temporarily changed to pin specific versions. However, this should be rare.
1 parent 3c1522c commit ebea653

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

content/tokio/topics/tracing-next-steps.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ performance issues during the development process.
1212
For instance, to use tokio-console in [the mini-redis project](https://github.com/tokio-rs/mini-redis),
1313
you need to enable the `tracing` feature for the Tokio package:
1414

15-
```toml
16-
# Update the tokio import in your Cargo.toml
17-
tokio = { version = "1", features = ["full", "tracing"] }
15+
```bash
16+
cargo add tokio --features full,tracing
1817
```
1918

2019
Note: The `full` feature doesn't enable `tracing`.
@@ -23,9 +22,8 @@ You'll also need to add a dependency on the `console-subscriber` package. This
2322
crate provides a `Subscriber` implementation that will replace the one currently
2423
used by mini-redis:
2524

26-
```toml
27-
# Add this to the dependencies section of your Cargo.toml
28-
console-subscriber = "0.1.5"
25+
```bash
26+
cargo add console-subscriber
2927
```
3028

3129
Finally, in `src/bin/server.rs`, replace the call to `tracing_subscriber` with
@@ -128,16 +126,16 @@ It will look like this:
128126

129127
We'll come back to this page once we have some trace data generated and sent.
130128

131-
To set up mini-redis, we'll first need to add a few dependencies. Update your
132-
`Cargo.toml` with the following:
129+
To set up mini-redis, we'll first need to add a few dependencies. Add the
130+
following dependencies to your `Cargo.toml` file:
133131

134-
```toml
132+
```bash
135133
# Implements the types defined in the Otel spec
136-
opentelemetry = "0.17.0"
134+
cargo add opentelemetry
137135
# Integration between the tracing crate and the opentelemetry crate
138-
tracing-opentelemetry = "0.17.2"
136+
cargo add tracing-opentelemetry
139137
# Allows you to export data to Jaeger
140-
opentelemetry-jaeger = "0.16.0"
138+
cargo add opentelemetry-jaeger
141139
```
142140

143141
Now, in `src/bin/server.rs`, add the following imports:

content/tokio/tutorial/async.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,10 @@ implementors, but requires a bunch of unsafe boilerplate code. Instead of using
499499
provided by the [`futures`] crate. This allows us to implement a simple trait to
500500
expose our `Task` struct as a waker.
501501

502-
Add the following dependency to your `Cargo.toml` to pull in `futures`.
502+
Add the `futures` dependency to your `Cargo.toml` file:
503503

504-
```toml
505-
futures = "0.3"
504+
```bash
505+
cargo add futures
506506
```
507507

508508
Then implement [`futures::task::ArcWake`][`ArcWake`].

content/tokio/tutorial/hello-tokio.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ then read back the key. This will be done using the Mini-Redis client library.
1313
Let's start by generating a new Rust app:
1414

1515
```bash
16-
$ cargo new my-redis
17-
$ cd my-redis
16+
cargo new my-redis
17+
cd my-redis
1818
```
1919

2020
## Add dependencies
2121

22-
Next, open `Cargo.toml` and add the following right below `[dependencies]`:
22+
Add the following dependencies to your `Cargo.toml` file:
2323

24-
```toml
25-
tokio = { version = "1", features = ["full"] }
26-
mini-redis = "0.4"
24+
```bash
25+
cargo add tokio --features full
26+
cargo add mini-redis
2727
```
2828

2929
## Write the code
@@ -55,13 +55,13 @@ async fn main() -> Result<()> {
5555
Make sure the Mini-Redis server is running. In a separate terminal window, run:
5656

5757
```bash
58-
$ mini-redis-server
58+
mini-redis-server
5959
```
6060

6161
If you have not already installed mini-redis, you can do so with
6262

6363
```bash
64-
$ cargo install mini-redis
64+
cargo install mini-redis
6565
```
6666

6767
Now, run the `my-redis` application:

content/tokio/tutorial/shared-state.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ the underlying data. Instead, a `Bytes` instance is a reference-counted handle
3030
to some underlying data. The `Bytes` type is roughly an `Arc<Vec<u8>>` but with
3131
some added capabilities.
3232

33-
To depend on `bytes`, add the following to your `Cargo.toml` in the
34-
`[dependencies]` section:
33+
Add the `bytes` dependency to your `Cargo.toml` file:
3534

36-
```toml
37-
bytes = "1"
35+
```bash
36+
cargo add bytes
3837
```
3938

4039
[`bytes`]: https://docs.rs/bytes/1/bytes/struct.Bytes.html
@@ -154,6 +153,7 @@ async fn process(socket: TcpStream, db: Db) {
154153
# Holding a `MutexGuard` across an `.await`
155154

156155
You might write code that looks like this:
156+
157157
```rust
158158
use std::sync::{Mutex, MutexGuard};
159159

@@ -165,8 +165,10 @@ async fn increment_and_do_stuff(mutex: &Mutex<i32>) {
165165
} // lock goes out of scope here
166166
# async fn do_something_async() {}
167167
```
168+
168169
When you try to spawn something that calls this function, you will encounter the
169170
following error message:
171+
170172
```text
171173
error: future cannot be sent between threads safely
172174
--> src/lib.rs:13:5
@@ -191,11 +193,13 @@ note: future is not `Send` as this value is used across an await
191193
8 | }
192194
| - `mut lock` is later dropped here
193195
```
196+
194197
This happens because the `std::sync::MutexGuard` type is **not** `Send`. This
195198
means that you can't send a mutex lock to another thread, and the error happens
196199
because the Tokio runtime can move a task between threads at every `.await`.
197200
To avoid this, you should restructure your code such that the mutex lock's
198201
destructor runs before the `.await`.
202+
199203
```rust
200204
# use std::sync::{Mutex, MutexGuard};
201205
// This works!
@@ -209,7 +213,9 @@ async fn increment_and_do_stuff(mutex: &Mutex<i32>) {
209213
}
210214
# async fn do_something_async() {}
211215
```
216+
212217
Note that this does not work:
218+
213219
```rust
214220
use std::sync::{Mutex, MutexGuard};
215221

@@ -223,6 +229,7 @@ async fn increment_and_do_stuff(mutex: &Mutex<i32>) {
223229
}
224230
# async fn do_something_async() {}
225231
```
232+
226233
This is because the compiler currently calculates whether a future is `Send`
227234
based on scope information only. The compiler will hopefully be updated to
228235
support explicitly dropping it in the future, but for now, you must explicitly
@@ -250,6 +257,7 @@ We will discuss some approaches to avoid these issues below:
250257

251258
The safest way to handle a mutex is to wrap it in a struct, and lock the mutex
252259
only inside non-async methods on that struct.
260+
253261
```rust
254262
use std::sync::Mutex;
255263

@@ -270,6 +278,7 @@ async fn increment_and_do_stuff(can_incr: &CanIncrement) {
270278
}
271279
# async fn do_something_async() {}
272280
```
281+
273282
This pattern guarantees that you won't run into the `Send` error, because the
274283
mutex guard does not appear anywhere in an async function. It also protects you
275284
from deadlocks, when using crates whose `MutexGuard` implements `Send`.
@@ -288,6 +297,7 @@ The [`tokio::sync::Mutex`] type provided by Tokio can also be used. The primary
288297
feature of the Tokio mutex is that it can be held across an `.await` without any
289298
issues. That said, an asynchronous mutex is more expensive than an ordinary
290299
mutex, and it is typically better to use one of the two other approaches.
300+
291301
```rust
292302
use tokio::sync::Mutex; // note! This uses the Tokio mutex
293303

0 commit comments

Comments
 (0)