Created
May 6, 2016 17:20
-
-
Save vadixidav/57d76e966ee07d50f927dad3f0550904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::rc::Rc; | |
use std::cell; | |
use std::ops::{Deref, DerefMut}; | |
pub struct SCell<T>(Rc<cell::RefCell<T>>); | |
pub struct Ref<'a, T: 'a>(cell::Ref<'a, T>); | |
pub struct RefMut<'a, T: 'a>(cell::RefMut<'a, T>); | |
impl<T> SCell<T> { | |
fn borrow(&self) -> Ref<T> { | |
Ref(self.0.borrow()) | |
} | |
fn borrow_mut(&self) -> RefMut<T> { | |
RefMut(self.0.borrow_mut()) | |
} | |
} | |
impl<'a, T: ?Sized + 'a> Deref for Ref<'a, T> { | |
type Target = T; | |
fn deref(&self) -> &T { | |
&*self.0 | |
} | |
} | |
impl<'a, T> Deref for RefMut<'a, T> | |
where T: 'a | |
{ | |
type Target = T; | |
fn deref(&self) -> &T { | |
&*self.0 | |
} | |
} | |
impl<'a, T> DerefMut for RefMut<'a, T> | |
where T: 'a | |
{ | |
fn deref_mut(&mut self) -> &mut T { | |
&mut *self.0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment