SHARE
TWEET

Untitled




Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- fn get_mut<'a, 'b, T>(pointer: &'a T) -> &'b mut T {
- unsafe { std::mem::transmute::<*mut T, &mut T>(pointer as *const T as *mut T) }
- }
- fn write_idx<T: std::fmt::Debug>(vec: &Vec<T>, idx: usize, val: T) {
- unsafe {
- let ptr = get_mut(vec).as_mut_ptr().offset((idx*std::mem::size_of_val(&val)) as isize);
- println!("PTR: {:?}", ptr);
- std::ptr::write(ptr, val);
- }
- }
- #[derive(Debug)]
- struct shit {
- wtf: u64,
- }
- impl Drop for shit {
- fn drop(&mut self) {
- println!("Dropping shit! {:?}", self);
- }
- }
- fn blah() {
- let v: Vec<Option<Vec<shit>>> = Vec::with_capacity(1);
- {
- unsafe { get_mut(&v).set_len(1); }
- println!("set_len(&v)");
- let inner_vec: Vec<shit> = Vec::with_capacity(1);
- println!("inner_vec init");
- unsafe { get_mut(&inner_vec).set_len(1); }
- println!("set_len(&inner_vec)");
- write_idx(&inner_vec, 0, shit { wtf: 123 });
- println!("inner_vec[0 = shit");
- write_idx(&v, 0, Some(inner_vec));
- println!("v[0] = inner_vec");
- println!("Added inner vec");
- }
- println!("end of nested block");
- //write_idx(&v, 0, None);
- get_mut(&v)[0] = None;
- println!("V: {:?}", v);
- }
- fn main() {
- println!("START");
- blah();
- println!("END");
- }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.