10000 add FFI example · sarvex/rust-memory-model@5219fac · GitHub
[go: up one dir, main page]

Skip to content

Commit 5219fac

Browse files
committed
add FFI example
1 parent 5e87de9 commit 5219fac
Copy full SHA for 5219fac

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
### Example
2+
3+
```rust
4+
fn caller(x: &mut i32) {
5+
callee(x, x);
6+
}
7+
8+
fn callee(x: *mut i32, y: *mut i32) {
9+
}
10+
11+
fn main() { }
12+
```
13+
14+
### Explanation
15+
16+
In the call to `callee`, there is an implicit reborrow of `x` and
17+
coercion of the resulting reborrow to a `*mut`. These reborrows have a
18+
very short lifetime, so here we can coerce `x` twice into a `*mut`.
19+
20+
Since this pattern occurs everywhere in FFI libraries, this suggests
21+
that if we coerce an `&'a mut` (resp. `&'a`) to a `*mut/*const`
22+
(resp. `*const`), we should not say that the resulting unsafe pointer
23+
is only usable during `'a`, because `'a` may be something unreasonably
24+
short.
25+
26+
### Source
27+
28+
https://github.com/rust-lang/rust/issues/30424#issuecomment-171344268

0 commit comments

Comments
 (0)
0