You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rust-cookbook/src/concurrency/custom_future/custom-future.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,12 +8,16 @@ instead of relying on `async`/`await`.
8
8
9
9
Every custom future must handle three concepts:
10
10
11
+
<divclass="comparison">
12
+
11
13
| Concept | Why it matters |
12
14
|---------|----------------|
13
15
|**[`Pin<&mut Self>`]**| Guarantees the future won't move in memory after the first poll. This is critical for self-referential futures (e.g., those holding borrows across `.await` points). If your struct contains only ordinary fields (no self-references), the compiler auto-implements `Unpin` and pinning is effectively a no-op. |
14
16
|**[`Poll::Pending`] / [`Poll::Ready`]**|`Pending` tells the executor "not done yet," while `Ready(value)` completes the future. |
15
17
|**[`cx.waker()`]**| A handle the executor gives you. You *must* call `wake()` at some point after returning `Pending`, or the executor will never poll the future again and it will hang. |
16
18
19
+
</div>
20
+
17
21
The example below builds a simple `Delay` future that resolves after a
18
22
deadline. It has no self-referential fields, so it is `Unpin` automatically—
19
23
pinning costs nothing. A production timer would register with a reactor;
0 commit comments