]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-16492.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / issue-16492.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-pretty
12
13 use std::rc::Rc;
14 use std::cell::Cell;
15
16 struct Field {
17     number: usize,
18     state: Rc<Cell<usize>>
19 }
20
21 impl Field {
22     fn new(number: usize, state: Rc<Cell<usize>>) -> Field {
23         Field {
24             number: number,
25             state: state
26         }
27     }
28 }
29
30 impl Drop for Field {
31     fn drop(&mut self) {
32         println!("Dropping field {}", self.number);
33         assert_eq!(self.state.get(), self.number);
34         self.state.set(self.state.get()+1);
35     }
36 }
37
38 struct NoDropImpl {
39     _one: Field,
40     _two: Field,
41     _three: Field
42 }
43
44 struct HasDropImpl {
45     _one: Field,
46     _two: Field,
47     _three: Field
48 }
49
50 impl Drop for HasDropImpl {
51     fn drop(&mut self) {
52         println!("HasDropImpl.drop()");
53         assert_eq!(self._one.state.get(), 0);
54         self._one.state.set(1);
55     }
56 }
57
58 pub fn main() {
59     let state = Rc::new(Cell::new(1));
60     let noImpl = NoDropImpl {
61         _one: Field::new(1, state.clone()),
62         _two: Field::new(2, state.clone()),
63         _three: Field::new(3, state.clone())
64     };
65     drop(noImpl);
66     assert_eq!(state.get(), 4);
67
68     state.set(0);
69     let hasImpl = HasDropImpl {
70         _one: Field::new(1, state.clone()),
71         _two: Field::new(2, state.clone()),
72         _three: Field::new(3, state.clone())
73     };
74     drop(hasImpl);
75     assert_eq!(state.get(), 4);
76 }