Skip to content

Commit 555fbfa

Browse files
committed
fix: dma example
1 parent 7ad628a commit 555fbfa

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

text/0000-forget-marker-trait.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,19 @@ fn spawn<'a>(fut: impl IntoFuture + 'a) -> TaskHandler<'a> {
182182

183183
DMA stands for Direct Memory Access, which is used to transfer data between two memory locations in parallel to the operation of the core processor. For the purposes of this example, it can be thought of as `memcpy` in parallel to any other code.
184184

185-
Let's say that `Serial::read_exact` triggers a DMA transfer and returns a future that will resolve on completion. It would be safe if we were to block on this future (basically passing control flow to the future itself), but we may instead trigger undefined behavior with `forget`:
185+
Let's say that the poll of `Serial::read_exact` triggers a DMA transfer. It would be safe if we were to block on this future (basically passing control flow to the future itself), but we may instead trigger undefined behavior with `forget`:
186186

187187
```rust
188188
fn start(serial: &mut Serial) {
189189
let mut buf = [0; 16];
190190

191-
mem::forget(serial.read_exact(&mut buf));
191+
let mut fut = Box::pin(serial.read_exact(&mut buf));
192+
193+
fut.poll(cx); // start dma transfer
194+
195+
// Memory of the future itself is still valid (inside the allocation),
196+
// but the buffer lives outside of it and is not protected.
197+
core::mem::forget(fut); // or `Box::leak`
192198
}
193199

194200
fn corrupted() {
@@ -204,10 +210,6 @@ start(&mut serial);
204210
corrupted();
205211
```
206212

207-
See [blog.japaric.io/safe-dma] for more.
208-
209-
[blog.japaric.io/safe-dma]: https://blog.japaric.io/safe-dma/
210-
211213
### GPU
212214
[example-async-cuda]: #example-async-cuda
213215

0 commit comments

Comments
 (0)