Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ops::{Deref, DerefMut};
- struct DummyContainer(i32);
- struct Proxy<'a>(&'a mut DummyContainer, i32);
- impl DummyContainer {
- fn get_mut(&mut self) -> Proxy {
- Proxy(self, 0)
- }
- }
- impl<'a> Drop for Proxy<'a> {
- fn drop(&mut self) {
- (*self.0).0 = self.1
- }
- }
- impl<'a> Deref for Proxy<'a> {
- type Target = i32;
- fn deref(&self) -> &Self::Target {
- &self.1
- }
- }
- impl<'a> DerefMut for Proxy<'a> {
- fn deref_mut(&mut self) -> &mut Self::Target {
- &mut self.1
- }
- }
- pub fn main() {
- let mut c = DummyContainer(0);
- let mut ptr = c.get_mut();
- *ptr = 3;
- std::mem::drop(ptr);
- println!("{}", c.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement