2012-01-10 21:04:09 -06:00
|
|
|
// Test cyclic detector when using iface instances.
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
iface to_str {
|
|
|
|
fn to_str() -> str;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <T: to_str> of to_str for option<T> {
|
|
|
|
fn to_str() -> str {
|
|
|
|
alt self {
|
2012-01-19 00:37:22 -06:00
|
|
|
none { "none" }
|
2012-01-10 21:04:09 -06:00
|
|
|
some(t) { "some(" + t.to_str() + ")" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_str for int {
|
|
|
|
fn to_str() -> str { int::str(self) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_str for Tree {
|
|
|
|
fn to_str() -> str {
|
|
|
|
#fmt["[%s, %s, %s]",
|
|
|
|
self.val.to_str(),
|
|
|
|
self.left.to_str(),
|
|
|
|
self.right.to_str()]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-12 09:57:30 -06:00
|
|
|
fn foo<T: to_str>(x: T) -> str { x.to_str() }
|
|
|
|
|
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-01-12 09:57:30 -06:00
|
|
|
let expected = "[2, some([1, none, none]), some([1, none, none])]";
|
|
|
|
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
|
|
|
|
}
|