Skip to content

Commit 073b9ec

Browse files
committed
fix: dma example
1 parent 7ad628a commit 073b9ec

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

text/0000-forget-marker-trait.md

+8-2
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 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() {

0 commit comments

Comments
 (0)