Skip to content

Instantly share code, notes, and snippets.

@Kimundi
Last active August 29, 2015 14:08
Show Gist options
  • Save Kimundi/d846f1da490ef4e9b0d9 to your computer and use it in GitHub Desktop.
Save Kimundi/d846f1da490ef4e9b0d9 to your computer and use it in GitHub Desktop.
use std::cell::Cell;
use std::kinds::marker;
struct Unmovable<'a> {
lock: Cell<marker::ContravariantLifetime<'a>>,
marker: marker::NoCopy
}
impl<'a> Unmovable<'a> {
fn new() -> Unmovable<'a> {
Unmovable {
lock: Cell::new(marker::ContravariantLifetime),
marker: marker::NoCopy
}
}
fn lock(&'a self) {
self.lock.set(marker::ContravariantLifetime);
}
fn new_in(self_: &'a mut Option<Unmovable<'a>>) {
*self_ = Some(Unmovable::new());
self_.as_ref().unwrap().lock();
}
}
fn main(){
let x = Unmovable::new();
x.lock();
// error: cannot move out of `x` because it is borrowed
// let z = x;
let mut y = None;
Unmovable::new_in(&mut y);
// error: cannot move out of `y` because it is borrowed
// let z = y;
assert_eq!(std::mem::size_of::<Unmovable>(), 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment