about summary refs log tree commit diff
path: root/tests/ui/drop/drop-scope-exit.rs
blob: 4d003fec7125e46b003babd6c3d96d4d76896a65 (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
//! Check that the `Drop` implementation is called when a value goes out of scope.

//@ run-pass

#![allow(non_camel_case_types)]
use std::cell::Cell;

struct shrinky_pointer<'a> {
    i: &'a Cell<isize>,
}

impl<'a> Drop for shrinky_pointer<'a> {
    fn drop(&mut self) {
        println!("Hello!");
        self.i.set(self.i.get() - 1);
    }
}

impl<'a> shrinky_pointer<'a> {
    pub fn look_at(&self) -> isize {
        return self.i.get();
    }
}

fn shrinky_pointer(i: &Cell<isize>) -> shrinky_pointer<'_> {
    shrinky_pointer { i }
}

pub fn main() {
    let my_total = &Cell::new(10);
    {
        let pt = shrinky_pointer(my_total);
        assert_eq!(pt.look_at(), 10);
    }
    println!("my_total = {}", my_total.get());
    assert_eq!(my_total.get(), 9);
}