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.
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.
11 #![feature(managed_boxes)]
13 // Test cyclic detector when using trait instances.
15 use std::cell::RefCell;
17 struct Tree(@RefCell<TreeR>);
25 fn to_str_(&self) -> ~str;
28 impl<T:to_str> to_str for Option<T> {
29 fn to_str_(&self) -> ~str {
31 None => { "none".to_owned() }
32 Some(ref t) => { "some(".to_owned() + t.to_str_() + ")".to_owned() }
38 fn to_str_(&self) -> ~str { self.to_str() }
41 impl to_str for Tree {
42 fn to_str_(&self) -> ~str {
44 let this = t.borrow();
45 let (l, r) = (this.left, this.right);
47 format!("[{}, {}, {}]", val.to_str_(), l.to_str_(), r.to_str_())
51 fn foo<T:to_str>(x: T) -> ~str { x.to_str_() }
54 let t1 = Tree(@RefCell::new(TreeR{left: None,
56 val: ~1 as ~to_str:Send}));
57 let t2 = Tree(@RefCell::new(TreeR{left: Some(t1),
59 val: ~2 as ~to_str:Send}));
60 let expected = "[2, some([1, none, none]), some([1, none, none])]".to_owned();
61 assert!(t2.to_str_() == expected);
62 assert!(foo(t2) == expected);
66 let mut t1 = t1_.borrow_mut();
67 t1.left = Some(t2); // create cycle