aboutsummaryrefslogtreecommitdiffstats
path: root/tests/compile-fail/handler.rs
blob: 64ab9618dd5ed166d8afc3b2b23e85306f012d94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
extern crate clint;

#[macro_use]
extern crate lazy_static;

use clint::Handler;

lazy_static! {
    static ref HANDLER: Handler<'static> = Handler::new();
}

fn main() {
    need_move();
    borrow_error();
    no_borrow_needed();
}

fn need_move() {
    let x = vec![1, 2, 3];
    let mut c = || {
        println!("x(h-c): {:?}", x); //~ ERROR does not live long enough
    };
    unsafe {
        HANDLER.replace(&mut c);
        HANDLER.call();
        HANDLER.call();
    }
    println!("x(h-o): {:?}", x);
}

fn borrow_error() {
    let x = vec![1, 2, 3];
    let mut c = move || {
        println!("x(h-c): {:?}", x);
    };
    unsafe {
        HANDLER.replace(&mut c);
        HANDLER.call();
        HANDLER.call();
    }
    println!("x(h-o): {:?}", x); //~ ERROR borrow of moved value
}

fn no_borrow_needed() {
    let x = vec![1, 2, 3];
    let mut c = || {
        println!("x(h-c): hi!");
    };
    unsafe {
        HANDLER.replace(&mut c);
        HANDLER.call();
        HANDLER.call();
    }
    println!("x(h-o): {:?}", x);
}