]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/resource-cycle2.rs
0cfd3a93e53506a1ed6df0a914d61d8462532033
[rust.git] / src / test / run-pass / resource-cycle2.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 // Don't leak the unique pointers
12
13 use std::cast;
14
15 struct U {
16     a: int,
17     b: int,
18     c: *int
19 }
20
21 struct r {
22   v: U,
23 }
24
25 impl Drop for r {
26     fn drop(&self) {
27         unsafe {
28             let _v2: ~int = cast::transmute(self.v.c);
29         }
30     }
31 }
32
33 fn r(v: U) -> r {
34     r {
35         v: v
36     }
37 }
38
39 struct t(Node);
40
41 struct Node {
42     next: Option<@mut t>,
43     r: r
44 }
45
46 pub fn main() {
47     unsafe {
48         let i1 = ~0xA;
49         let i1p = cast::transmute_copy(&i1);
50         cast::forget(i1);
51         let i2 = ~0xA;
52         let i2p = cast::transmute_copy(&i2);
53         cast::forget(i2);
54
55         let u1 = U {a: 0xB, b: 0xC, c: i1p};
56         let u2 = U {a: 0xB, b: 0xC, c: i2p};
57
58         let x1 = @mut t(Node {
59             next: None,
60             r: r(u1)
61         });
62         let x2 = @mut t(Node {
63             next: None,
64             r: r(u2)
65         });
66         x1.next = Some(x2);
67         x2.next = Some(x1);
68     }
69 }