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