Skip to content

Instantly share code, notes, and snippets.

@pepyakin
Created April 24, 2020 16:31
Show Gist options
  • Save pepyakin/8c11a0f3edcb21206c4019c28faa4753 to your computer and use it in GitHub Desktop.
Save pepyakin/8c11a0f3edcb21206c4019c28faa4753 to your computer and use it in GitHub Desktop.
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