Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. #![feature(maybe_uninit_ref)]
  2.  
  3. use std::mem::MaybeUninit;
  4.  
  5. struct MyRef<'a> {
  6. r: &'a mut i32
  7. }
  8.  
  9. impl<'a> Drop for MyRef<'a> {
  10. fn drop(&mut self) {
  11. panic!("We shouldn't be dropping!");
  12. }
  13. }
  14.  
  15. impl<'a> MyRef<'a> {
  16. fn destruct(self) -> &'a mut i32 {
  17. let mut maybe_uninit = MaybeUninit::new(self);
  18. unsafe {
  19. std::ptr::read(&maybe_uninit.get_mut().r)
  20. }
  21. }
  22. }
  23.  
  24. fn main() {
  25. let mut x = 3;
  26. let my_ref = MyRef {
  27. r: &mut x
  28. };
  29. let x_ref = my_ref.destruct();
  30. println!("{}", *x_ref);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement