]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-16492.rs
Rollup merge of #91804 - woppopo:const_clone, r=oli-obk
[rust.git] / src / test / ui / issues / issue-16492.rs
1 // run-pass
2 #![allow(non_snake_case)]
3
4 use std::rc::Rc;
5 use std::cell::Cell;
6
7 struct Field {
8     number: usize,
9     state: Rc<Cell<usize>>
10 }
11
12 impl Field {
13     fn new(number: usize, state: Rc<Cell<usize>>) -> Field {
14         Field {
15             number: number,
16             state: state
17         }
18     }
19 }
20
21 impl Drop for Field {
22     fn drop(&mut self) {
23         println!("Dropping field {}", self.number);
24         assert_eq!(self.state.get(), self.number);
25         self.state.set(self.state.get()+1);
26     }
27 }
28
29 struct NoDropImpl {
30     _one: Field,
31     _two: Field,
32     _three: Field
33 }
34
35 struct HasDropImpl {
36     _one: Field,
37     _two: Field,
38     _three: Field
39 }
40
41 impl Drop for HasDropImpl {
42     fn drop(&mut self) {
43         println!("HasDropImpl.drop()");
44         assert_eq!(self._one.state.get(), 0);
45         self._one.state.set(1);
46     }
47 }
48
49 pub fn main() {
50     let state = Rc::new(Cell::new(1));
51     let noImpl = NoDropImpl {
52         _one: Field::new(1, state.clone()),
53         _two: Field::new(2, state.clone()),
54         _three: Field::new(3, state.clone())
55     };
56     drop(noImpl);
57     assert_eq!(state.get(), 4);
58
59     state.set(0);
60     let hasImpl = HasDropImpl {
61         _one: Field::new(1, state.clone()),
62         _two: Field::new(2, state.clone()),
63         _three: Field::new(3, state.clone())
64     };
65     drop(hasImpl);
66     assert_eq!(state.get(), 4);
67 }