blob: 3f7ddf93f0697c821feeccdb47c6a0eccc4da1eb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
// edition:2021
// run-pass
// Test that we can use raw ptrs when using `capture_disjoint_fields`.
#![allow(dead_code)]
#[derive(Debug)]
struct S {
s: String,
t: String,
}
struct T(*const S);
fn unsafe_imm() {
let s = "".into();
let t = "".into();
let my_speed: Box<S> = Box::new(S { s, t });
let p : *const S = Box::into_raw(my_speed);
let t = T(p);
let c = || unsafe {
println!("{:?}", (*t.0).s);
};
c();
}
fn unsafe_mut() {
let s = "".into();
let t = "".into();
let mut my_speed: Box<S> = Box::new(S { s, t });
let p : *mut S = &mut *my_speed;
let c = || {
let x = unsafe { &mut (*p).s };
*x = "s".into();
};
c();
}
fn main() {
unsafe_mut();
unsafe_imm();
}
|