SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn get_mut<'a, 'b, T>(pointer: &'a T) -> &'b mut T {
  2.     unsafe { std::mem::transmute::<*mut T, &mut T>(pointer as *const T as *mut T) }
  3. }
  4.  
  5. fn write_idx<T: std::fmt::Debug>(vec: &Vec<T>, idx: usize, val: T) {
  6.     unsafe {
  7.     let ptr = get_mut(vec).as_mut_ptr().offset((idx*std::mem::size_of_val(&val)) as isize);
  8.     println!("PTR: {:?}", ptr);
  9.     std::ptr::write(ptr, val);
  10.     }
  11. }
  12.  
  13. #[derive(Debug)]
  14. struct shit {
  15.     wtf: u64,
  16. }
  17.  
  18. impl Drop for shit {
  19.     fn drop(&mut self) {
  20.         println!("Dropping shit! {:?}", self);
  21.     }
  22. }
  23.  
  24. fn blah() {
  25.     let v: Vec<Option<Vec<shit>>> = Vec::with_capacity(1);
  26.     {
  27.         unsafe { get_mut(&v).set_len(1); }
  28.         println!("set_len(&v)");
  29.         let inner_vec: Vec<shit> = Vec::with_capacity(1);
  30.         println!("inner_vec init");
  31.         unsafe { get_mut(&inner_vec).set_len(1); }
  32.         println!("set_len(&inner_vec)");
  33.         write_idx(&inner_vec, 0, shit { wtf: 123 });
  34.         println!("inner_vec[0 = shit");
  35.         write_idx(&v, 0, Some(inner_vec));
  36.         println!("v[0] = inner_vec");
  37.         println!("Added inner vec");
  38.     }
  39.     println!("end of nested block");
  40.     //write_idx(&v, 0, None);
  41.     get_mut(&v)[0] = None;
  42.     println!("V: {:?}", v);
  43. }
  44.  
  45. fn main() {
  46.     println!("START");
  47.     blah();
  48.     println!("END");
  49.  
  50. }
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. OK, I Understand
 
Top