Skip to content

Commit

Permalink
Make the examples testable
Browse files Browse the repository at this point in the history
  • Loading branch information
roxelo committed Jul 2, 2021
1 parent c72d0bc commit de591f5
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/rust-2021/disjoint-capture-in-closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ When running `cargo fix --edition`, Cargo will update the closures in your code
### Wild Card Patterns
Closures now only capture data that needs to be read, which means the following closures will not capture `x`

```rust,ignore
```rust
let x = 10;
let c = || {
let _ = x;
Expand All @@ -55,13 +55,13 @@ let c = || match x {
### Drop Order
Since only a part of a variable might be captured instead of the entire variable, when different fields or elements (in case of tuple) get drop, the drop order might be affected.

```rust,ignore
```rust
{
let t = (vec![0], vec![0]);

{
let c = || {
move(t.0);
let c = move || {
t.0; // t.0 is moved here
};
} // c and t.0 dropped here
} // t.1 dropped here
Expand All @@ -77,16 +77,20 @@ For instance, a common way to allow passing around raw pointers between threads
With disjoint captures, only the specific field mentioned in the closure gets captured, which wasn't originally `Send`/`Sync` defeating the purpose of the wrapper.


```rust,ignore
```rust
use std::thread;

struct Ptr(*mut i32);
unsafe impl Send for Ptr;
unsafe impl Send for Ptr {}


let mut x = 5;
let px = (&mut x as *mut i32);
let px = Ptr(&mut x as *mut i32);

let c = thread::spawn(move || {
*(px.0) += 10;
unsafe {
*(px.0) += 10;
}
}); // Closure captured px.0 which is not Send
```

Expand Down

0 comments on commit de591f5

Please sign in to comment.