Skip to content

Instantly share code, notes, and snippets.

@Kimundi
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kimundi/81b9944a896052b5407f to your computer and use it in GitHub Desktop.
Save Kimundi/81b9944a896052b5407f to your computer and use it in GitHub Desktop.
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