]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/init-res-into-things.rs
0b5e58526f6343e576dd6a9364cc5462c8d67059
[rust.git] / src / test / run-pass / init-res-into-things.rs
1 // Copyright 2012 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 // Resources can't be copied, but storing into data structures counts
12 // as a move unless the stored thing is used afterwards.
13
14 struct r {
15   i: @mut int,
16 }
17
18 struct Box { x: r }
19
20 #[unsafe_destructor]
21 impl Drop for r {
22     fn drop(&self) {
23         *(self.i) = *(self.i) + 1;
24     }
25 }
26
27 fn r(i: @mut int) -> r {
28     r {
29         i: i
30     }
31 }
32
33 fn test_box() {
34     let i = @mut 0;
35     {
36         let _a = @r(i);
37     }
38     assert_eq!(*i, 1);
39 }
40
41 fn test_rec() {
42     let i = @mut 0;
43     {
44         let _a = Box {x: r(i)};
45     }
46     assert_eq!(*i, 1);
47 }
48
49 fn test_tag() {
50     enum t {
51         t0(r),
52     }
53
54     let i = @mut 0;
55     {
56         let _a = t0(r(i));
57     }
58     assert_eq!(*i, 1);
59 }
60
61 fn test_tup() {
62     let i = @mut 0;
63     {
64         let _a = (r(i), 0);
65     }
66     assert_eq!(*i, 1);
67 }
68
69 fn test_unique() {
70     let i = @mut 0;
71     {
72         let _a = ~r(i);
73     }
74     assert_eq!(*i, 1);
75 }
76
77 fn test_box_rec() {
78     let i = @mut 0;
79     {
80         let _a = @Box {
81             x: r(i)
82         };
83     }
84     assert_eq!(*i, 1);
85 }
86
87 pub fn main() {
88     test_box();
89     test_rec();
90     test_tag();
91     test_tup();
92     test_unique();
93     test_box_rec();
94 }