Skip to content

Commit e02e1aa

Browse files
committed
fix(async-book): use owned self in Worker trait Send example (ch10)
The original example used &self in async fn run(&self), then claimed also prevents spawning because tokio::spawn requires 'static — a future borrowing &self can never satisfy that bound. Changed to of failure, and added a note explaining the 'static requirement.
1 parent d5dbc04 commit e02e1aa

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

async-book/src/ch10-async-traits.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,26 @@ trait DynDataStore {
9292

9393
```rust
9494
trait Worker {
95-
async fn run(&self); // Future might or might not be Send
95+
async fn run(self); // Future might or might not be Send
9696
}
9797

9898
struct MyWorker;
9999

100100
impl Worker for MyWorker {
101-
async fn run(&self) {
101+
async fn run(self) {
102102
// If this uses !Send types, the future is !Send
103103
let rc = std::rc::Rc::new(42);
104104
some_work().await;
105105
println!("{rc}");
106106
}
107107
}
108108

109-
// ❌ This fails if the future isn't Send:
109+
// ❌ This fails because the future is !Send (Rc is !Send):
110110
// tokio::spawn(worker.run()); // Requires Send + 'static
111+
//
112+
// Note: We use `self` (owned) here because tokio::spawn also
113+
// requires 'static — a future borrowing &self can't be 'static.
114+
// Even without Rc, `async fn run(&self)` wouldn't be spawnable.
111115
```
112116

113117
### The trait_variant Crate

0 commit comments

Comments
 (0)