|
| 1 | +#![allow(unused_assignments)] |
| 2 | +#![allow(unused_variables)] |
| 3 | +#![allow(dead_code)] |
| 4 | + |
| 5 | +/* unsafe superpower 1: dereferencing pointers. */ |
| 6 | +fn superpower_1() { |
| 7 | + let x = 42; |
| 8 | + |
| 9 | + // Implicit &T -> *const T conversion. |
| 10 | + let raw_ptr: *const i32 = &x; |
| 11 | + |
| 12 | + // An old way to directly create a pointer. |
| 13 | + let raw_ptr: *const i32 = std::ptr::addr_of!(x); |
| 14 | + |
| 15 | + // The new way to directly create a pointer. |
| 16 | + let raw_ptr: *const i32 = &raw const x; |
| 17 | + |
| 18 | + // Dereferencing a raw pointer requires an `unsafe` block. |
| 19 | + println!("Value: {}", unsafe { *raw_ptr }); |
| 20 | +} |
| 21 | + |
| 22 | +/* unsafe superpower 2: calling an unsafe function. */ |
| 23 | +unsafe fn unsafe_function() { |
| 24 | + println!("This is an unsafe function!"); |
| 25 | +} |
| 26 | + |
| 27 | +fn superpower_2() { |
| 28 | + unsafe { |
| 29 | + // Calling an unsafe function. |
| 30 | + unsafe_function(); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/* unsafe superpower 3: Accessing or modifying mutable static variable. |
| 35 | + * It is unsafe because it can lead to data races if accessed concurrently. |
| 36 | + * */ |
| 37 | + |
| 38 | +static mut COUNTER: i32 = 0; |
| 39 | + |
| 40 | +fn increment_counter() { |
| 41 | + unsafe { |
| 42 | + // Accessing and modifying a mutable static variable |
| 43 | + COUNTER += 1; |
| 44 | + println!("Counter: {}", COUNTER); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +fn superpower_3() { |
| 49 | + // This would cause UB: a data race. |
| 50 | + // std::thread::spawn(increment_counter); |
| 51 | + increment_counter(); |
| 52 | +} |
| 53 | + |
| 54 | +/* unsafe superpower 4: Implementing unsafe traits. |
| 55 | + * It is unsafe because safe code is permitted to cause UB if an unsafe trait |
| 56 | + * is implemented for a type that should not implement it (think Send/Sync). |
| 57 | + * */ |
| 58 | + |
| 59 | +unsafe trait CanBeAtomic { |
| 60 | + fn safe_method_of_unsafe_trait(&self); |
| 61 | +} |
| 62 | + |
| 63 | +struct MyStruct { |
| 64 | + i: i32, |
| 65 | +} |
| 66 | + |
| 67 | +unsafe impl UnsafeTrait for MyStruct { |
| 68 | + fn safe_method_of_unsafe_trait(&self) { |
| 69 | + println!("Method called!"); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +fn superpower_4() { |
| 74 | + let my_struct = MyStruct { i: 42 }; |
| 75 | + |
| 76 | + // Calling a safe method from an unsafe trait |
| 77 | + my_struct.safe_method_of_unsafe_trait(); |
| 78 | +} |
| 79 | + |
| 80 | +/* unsafe superpower 5: Accessing fields of a union. |
| 81 | + * It is unsafe because union can contain a different variant that we try to read, |
| 82 | + * so we could read some rubbish value. |
| 83 | + * */ |
| 84 | + |
| 85 | +union MyUnion { |
| 86 | + int_value: i32, |
| 87 | + bool_value: bool, |
| 88 | +} |
| 89 | + |
| 90 | +fn main() { |
| 91 | + let u = MyUnion { int_value: 42 }; |
| 92 | + |
| 93 | + unsafe { |
| 94 | + // Accessing a field of a union |
| 95 | + println!("Union value as int: {}", u.int_value); |
| 96 | + |
| 97 | + // Would result in UB, as the compiler may assume that bool is either 0 or 1 underneath. |
| 98 | + // println!("Union value as bool: {}", u.bool_value); |
| 99 | + } |
| 100 | +} |
0 commit comments