2012-07-31 12:27:51 -05:00
|
|
|
// Test cyclic detector when using trait instances.
|
2012-01-10 21:04:09 -06:00
|
|
|
|
2012-01-19 18:10:31 -06:00
|
|
|
enum Tree = TreeR;
|
2012-01-10 21:04:09 -06:00
|
|
|
type TreeR = @{
|
2012-03-26 20:35:18 -05:00
|
|
|
mut left: option<Tree>,
|
|
|
|
mut right: option<Tree>,
|
2012-01-10 21:04:09 -06:00
|
|
|
val: to_str
|
|
|
|
};
|
|
|
|
|
2012-07-31 12:27:51 -05:00
|
|
|
trait to_str {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str;
|
2012-01-10 21:04:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl <T: to_str> of to_str for option<T> {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str {
|
2012-01-10 21:04:09 -06:00
|
|
|
alt self {
|
2012-07-14 00:57:48 -05:00
|
|
|
none { ~"none" }
|
|
|
|
some(t) { ~"some(" + t.to_str() + ~")" }
|
2012-01-10 21:04:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_str for int {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { int::str(self) }
|
2012-01-10 21:04:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_str for Tree {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str {
|
2012-06-04 12:44:19 -05:00
|
|
|
let l = self.left, r = self.right;
|
2012-07-30 18:01:07 -05:00
|
|
|
fmt!{"[%s, %s, %s]", self.val.to_str(),
|
|
|
|
l.to_str(), r.to_str()}
|
2012-01-10 21:04:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn foo<T: to_str>(x: T) -> ~str { x.to_str() }
|
2012-01-12 09:57:30 -06:00
|
|
|
|
2012-01-10 21:04:09 -06:00
|
|
|
fn main() {
|
2012-03-26 20:35:18 -05:00
|
|
|
let t1 = Tree(@{mut left: none,
|
|
|
|
mut right: none,
|
2012-01-10 21:04:09 -06:00
|
|
|
val: 1 as to_str });
|
2012-03-26 20:35:18 -05:00
|
|
|
let t2 = Tree(@{mut left: some(t1),
|
|
|
|
mut right: some(t1),
|
2012-01-10 21:04:09 -06: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])]";
|
2012-01-12 09:57:30 -06:00
|
|
|
assert t2.to_str() == expected;
|
|
|
|
assert foo(t2 as to_str) == expected;
|
2012-01-10 21:04:09 -06:00
|
|
|
t1.left = some(t2); // create cycle
|
|
|
|
}
|