summaryrefslogtreecommitdiffstats
path: root/third_party/rust/maybe-uninit/tests/doesnt_drop.rs
blob: 1d4b30fc462bd704923587f89a9718a60d196a63 (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
extern crate maybe_uninit;
use maybe_uninit::MaybeUninit;

use std::cell::Cell;

struct DecrementOnDrop<'a>(&'a Cell<usize>);

impl<'a> DecrementOnDrop<'a> {
    pub fn new(ref_:&'a Cell<usize>) -> Self {
        ref_.set(1);
        DecrementOnDrop(ref_)
    }
}

impl<'a> Clone for DecrementOnDrop<'a> {
    fn clone(&self) -> Self {
        self.0.set(self.0.get() + 1);

        DecrementOnDrop(self.0)
    }
}

impl<'a> Drop for DecrementOnDrop<'a>{
    fn drop(&mut self) {
        self.0.set(self.0.get() - 1);
    }
}

#[test]
fn doesnt_drop(){
    let count = Cell::new(0);
    let arc = DecrementOnDrop::new(&count);
    let maybe = MaybeUninit::new(arc.clone());
    assert_eq!(count.get(), 2);
    drop(maybe);
    assert_eq!(count.get(), 2);
}