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