Skip to content

Instantly share code, notes, and snippets.

@erickt
Created June 12, 2013 18:11
Show Gist options
  • Save erickt/5767698 to your computer and use it in GitHub Desktop.
Save erickt/5767698 to your computer and use it in GitHub Desktop.
use std::io;
use std::str;
trait Eq2<RHS> {
fn eq2(&self, rhs: RHS) -> bool;
}
////
trait IntEq2 {
fn eq2_to_int(&self, lhs: int) -> bool;
}
impl<RHS: IntEq2> Eq2<RHS> for int {
fn eq2(&self, rhs: RHS) -> bool { rhs.eq2_to_int(*self) }
}
impl IntEq2 for i8 {
fn eq2_to_int(&self, lhs: int) -> bool { lhs == *self as int }
}
impl IntEq2 for i16 {
fn eq2_to_int(&self, lhs: int) -> bool { lhs == *self as int }
}
////
trait StrEq2 {
fn eq2_to_str(&self, lhs: &str) -> bool;
}
impl<'self, RHS: StrEq2> Eq2<RHS> for &'self str {
fn eq2(&self, rhs: RHS) -> bool { rhs.eq2_to_str(*self) }
}
impl StrEq2 for ~str {
fn eq2_to_str(&self, lhs: &str) -> bool { str::eq_slice(lhs, *self) }
}
impl<'self> StrEq2 for &'self str {
fn eq2_to_str(&self, lhs: &str) -> bool { str::eq_slice(lhs, *self) }
}
////
fn main() {
io::println(fmt!("%?", (5).eq2(5i8)));
io::println(fmt!("%?", (5).eq2(5i16)));
io::println(fmt!("%?", (~"abc").eq2("abc")));
io::println(fmt!("%?", ("abc").eq2(~"abc")));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment