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