Last active
August 29, 2015 14:08
-
-
Save Kimundi/81b9944a896052b5407f 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::cell::{Cell, RefCell}; | |
use std::kinds::marker::{ContravariantLifetime, NoCopy}; | |
struct Unmovable<'a, T> { | |
data: RefCell<Option<T>>, | |
lock: Cell<ContravariantLifetime<'a>>, | |
marker: NoCopy, | |
} | |
impl<'a, T> Unmovable<'a, T> { | |
fn new() -> Unmovable<'a, T> { | |
Unmovable { | |
data: RefCell::new(None), | |
lock: Cell::new(ContravariantLifetime), | |
marker: NoCopy, | |
} | |
} | |
fn get(&'a self) -> &'a RefCell<Option<T>> { | |
self.lock.set(ContravariantLifetime); | |
&self.data | |
} | |
} | |
fn main(){ | |
let y = Unmovable::new(); | |
*y.get().borrow_mut() = Some(5u); | |
*y.get().borrow_mut() = Some(6u); | |
// error: cannot move out of `y` because it is borrowed | |
// let z = y; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment