rust/src/test/auxiliary/static-methods-crate.rs

34 lines
595 B
Rust
Raw Normal View History

#[link(name = "static_methods_crate",
vers = "0.1")];
#[crate_type = "lib"];
export read, readMaybe;
trait read {
static fn readMaybe(s: ~str) -> option<self>;
}
2012-08-07 20:10:06 -05:00
impl int: read {
static fn readMaybe(s: ~str) -> option<int> {
int::from_str(s)
}
}
2012-08-07 20:10:06 -05:00
impl bool: read {
static fn readMaybe(s: ~str) -> option<bool> {
match s {
~"true" => some(true),
~"false" => some(false),
_ => none
}
}
}
fn read<T: read copy>(s: ~str) -> T {
match readMaybe(s) {
some(x) => x,
_ => fail ~"read failed!"
}
}