]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/resource-cycle.rs
clean up warnings
[rust.git] / src / test / run-pass / resource-cycle.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 r {
16   v: *int,
17 }
18
19 impl Drop for r {
20     fn drop(&self) {
21         unsafe {
22             info!("r's dtor: self = %x, self.v = %x, self.v's value = %x",
23               cast::transmute::<*r, uint>(self),
24               cast::transmute::<**int, uint>(&(self.v)),
25               cast::transmute::<*int, uint>(self.v));
26             let _v2: ~int = cast::transmute(self.v);
27         }
28     }
29 }
30
31 fn r(v: *int) -> r {
32     r {
33         v: v
34     }
35 }
36
37 struct t(Node);
38
39 struct Node {
40     next: Option<@mut t>,
41     r: r
42 }
43
44 pub fn main() {
45     unsafe {
46         let i1 = ~0;
47         let i1p = cast::transmute_copy(&i1);
48         cast::forget(i1);
49         let i2 = ~0;
50         let i2p = cast::transmute_copy(&i2);
51         cast::forget(i2);
52
53         let x1 = @mut t(Node{
54             next: None,
55               r: {
56               let rs = r(i1p);
57               info!("r = %x", cast::transmute::<*r, uint>(&rs));
58               rs }
59         });
60
61         info!("x1 = %x, x1.r = %x",
62                cast::transmute::<@mut t, uint>(x1),
63                cast::transmute::<*r, uint>(&x1.r));
64
65         let x2 = @mut t(Node{
66             next: None,
67               r: {
68               let rs = r(i2p);
69               info!("r2 = %x", cast::transmute::<*r, uint>(&rs));
70               rs
71                 }
72         });
73
74         info!("x2 = %x, x2.r = %x",
75                cast::transmute::<@mut t, uint>(x2),
76                cast::transmute::<*r, uint>(&(x2.r)));
77
78         x1.next = Some(x2);
79         x2.next = Some(x1);
80     }
81 }