Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. use std::ops::{Deref, DerefMut};
  2. struct DummyContainer(i32);
  3.  
  4. struct Proxy<'a>(&'a mut DummyContainer, i32);
  5.  
  6. impl DummyContainer {
  7. fn get_mut(&mut self) -> Proxy {
  8. Proxy(self, 0)
  9. }
  10. }
  11.  
  12. impl<'a> Drop for Proxy<'a> {
  13. fn drop(&mut self) {
  14. (*self.0).0 = self.1
  15. }
  16. }
  17.  
  18. impl<'a> Deref for Proxy<'a> {
  19. type Target = i32;
  20. fn deref(&self) -> &Self::Target {
  21. &self.1
  22. }
  23. }
  24.  
  25.  
  26. impl<'a> DerefMut for Proxy<'a> {
  27. fn deref_mut(&mut self) -> &mut Self::Target {
  28. &mut self.1
  29. }
  30. }
  31.  
  32.  
  33. pub fn main() {
  34. let mut c = DummyContainer(0);
  35. let mut ptr = c.get_mut();
  36. *ptr = 3;
  37. std::mem::drop(ptr);
  38. println!("{}", c.0);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement