Created
April 24, 2020 16:31
-
-
Save pepyakin/8c11a0f3edcb21206c4019c28faa4753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cell::UnsafeCell; | |
#[test] | |
fn evil() { | |
let array: [u8; 2] = [b'A', b'B']; | |
let array = UnsafeCell::new(array); | |
let (_loaded_a, _loaded_b) = unsafe { | |
let a = &mut *{ | |
let cached_entries = &mut *array.get(); | |
&mut cached_entries[0] | |
}; | |
let b = &mut *{ | |
let cached_entries = &mut *array.get(); | |
&mut cached_entries[1] | |
}; | |
(a, b) | |
}; | |
} | |
#[test] | |
fn saint() { | |
let array: [u8; 2] = [b'A', b'B']; | |
let array = UnsafeCell::new(array); | |
unsafe { | |
let _loaded_a = &mut *{ | |
let cached_entries = &mut *array.get(); | |
&mut cached_entries[0] | |
}; | |
let _loaded_b = &mut *{ | |
let cached_entries = &mut *array.get(); | |
&mut cached_entries[1] | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment