2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-10-23 03:49:18 -05:00
|
|
|
#[feature(managed_boxes)];
|
|
|
|
|
2012-07-31 12:27:51 -05:00
|
|
|
// Test cyclic detector when using trait instances.
|
2012-01-10 21:04:09 -06:00
|
|
|
|
2013-03-07 18:36:30 -06:00
|
|
|
struct Tree(@mut TreeR);
|
2013-01-26 00:46:32 -06:00
|
|
|
struct TreeR {
|
2013-02-22 18:08:16 -06:00
|
|
|
left: Option<Tree>,
|
|
|
|
right: Option<Tree>,
|
2013-06-22 13:43:15 -05:00
|
|
|
val: ~to_str
|
2013-01-26 00:46:32 -06:00
|
|
|
}
|
2012-01-10 21:04:09 -06:00
|
|
|
|
2012-07-31 12:27:51 -05:00
|
|
|
trait to_str {
|
2013-06-22 13:43:15 -05:00
|
|
|
fn to_str_(&self) -> ~str;
|
2012-01-10 21:04:09 -06:00
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<T:to_str> to_str for Option<T> {
|
2013-06-22 13:43:15 -05:00
|
|
|
fn to_str_(&self) -> ~str {
|
2013-02-22 18:08:16 -06:00
|
|
|
match *self {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => { ~"none" }
|
2013-06-22 13:43:15 -05:00
|
|
|
Some(ref t) => { ~"some(" + t.to_str_() + ~")" }
|
2012-01-10 21:04:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl to_str for int {
|
2013-06-22 13:43:15 -05:00
|
|
|
fn to_str_(&self) -> ~str { self.to_str() }
|
2012-01-10 21:04:09 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl to_str for Tree {
|
2013-06-22 13:43:15 -05:00
|
|
|
fn to_str_(&self) -> ~str {
|
|
|
|
let (l, r) = (self.left, self.right);
|
2013-02-22 18:08:16 -06:00
|
|
|
let val = &self.val;
|
2013-09-29 21:23:57 -05:00
|
|
|
format!("[{}, {}, {}]", val.to_str_(), l.to_str_(), r.to_str_())
|
2012-01-10 21:04:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 13:43:15 -05:00
|
|
|
fn foo<T:to_str>(x: T) -> ~str { x.to_str_() }
|
2012-01-12 09:57:30 -06:00
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {
|
2013-02-22 18:08:16 -06:00
|
|
|
let t1 = Tree(@mut TreeR{left: None,
|
|
|
|
right: None,
|
2013-06-22 13:43:15 -05:00
|
|
|
val: ~1 as ~to_str });
|
2013-02-22 18:08:16 -06:00
|
|
|
let t2 = Tree(@mut TreeR{left: Some(t1),
|
|
|
|
right: Some(t1),
|
2013-06-22 13:43:15 -05:00
|
|
|
val: ~2 as ~to_str });
|
2012-07-14 00:57:48 -05:00
|
|
|
let expected = ~"[2, some([1, none, none]), some([1, none, none])]";
|
2013-06-22 13:43:15 -05:00
|
|
|
assert!(t2.to_str_() == expected);
|
|
|
|
assert!(foo(t2) == expected);
|
2012-08-20 14:23:37 -05:00
|
|
|
t1.left = Some(t2); // create cycle
|
2012-01-10 21:04:09 -06:00
|
|
|
}
|