Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- use std::cell::{Ref, RefCell};
- use std::ops::Deref;
- use std::ops::DerefMut;
- use std::marker::PhantomData;
- struct A {
- b: u32,
- }
- struct _PhantomStruct;
- struct EzShare<T> {
- value: Rc<RefCell<T>>,
- }
- impl<T> EzShare<T> {
- fn new(v: T) -> Self {
- Self {
- value: Rc::new(RefCell::new(v)),
- }
- }
- }
- impl<T> Deref for EzShare<T> {
- type Target = T;
- fn deref(&self) -> &T {
- Box::leak(Box::new(self.value.borrow()))
- }
- }
- impl<T> DerefMut for EzShare<T> {
- fn deref_mut(&mut self) -> &mut T {
- unsafe { std::mem::transmute(Box::leak(Box::new(self.value.borrow()))) }
- }
- }
- fn main() {
- let mut thing = EzShare::new(A { b: 0u32 });
- thing.b = 5;
- println!("{}", thing.b);
- }
Add Comment
Please, Sign In to add comment