Guest User

Untitled

a guest
May 21st, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. use std::rc::Rc;
  2. use std::cell::{Ref, RefCell};
  3. use std::ops::Deref;
  4. use std::ops::DerefMut;
  5. use std::marker::PhantomData;
  6.  
  7. struct A {
  8. b: u32,
  9. }
  10.  
  11. struct _PhantomStruct;
  12.  
  13. struct EzShare<T> {
  14. value: Rc<RefCell<T>>,
  15. }
  16.  
  17. impl<T> EzShare<T> {
  18. fn new(v: T) -> Self {
  19. Self {
  20. value: Rc::new(RefCell::new(v)),
  21. }
  22. }
  23. }
  24.  
  25. impl<T> Deref for EzShare<T> {
  26. type Target = T;
  27.  
  28. fn deref(&self) -> &T {
  29. Box::leak(Box::new(self.value.borrow()))
  30. }
  31. }
  32.  
  33. impl<T> DerefMut for EzShare<T> {
  34. fn deref_mut(&mut self) -> &mut T {
  35. unsafe { std::mem::transmute(Box::leak(Box::new(self.value.borrow()))) }
  36. }
  37. }
  38.  
  39. fn main() {
  40. let mut thing = EzShare::new(A { b: 0u32 });
  41. thing.b = 5;
  42. println!("{}", thing.b);
  43.  
  44. }
Add Comment
Please, Sign In to add comment