Created
September 28, 2012 17:23
-
-
Save erickt/3801125 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use io::Writer; | |
use io::WriterUtil; | |
trait Serializer { | |
fn emit_uint(&self, v: uint); | |
} | |
trait Serializable<S: Serializer> { | |
fn serialize(&self, s: &S); | |
} | |
impl<S: Serializer> uint: Serializable<S> { | |
fn serialize(&self, s: &S) { s.emit_uint(*self) } | |
} | |
pub struct UglySerializer { | |
wr: io::Writer, | |
} | |
pub fn UglySerializer(wr: io::Writer) -> UglySerializer { | |
UglySerializer { wr: wr } | |
} | |
pub impl UglySerializer: Serializer { | |
fn emit_uint(&self, v: uint) { | |
self.wr.write_str(fmt!("ugly %?u", v)); | |
} | |
} | |
pub struct PrettySerializer { | |
wr: io::Writer, | |
} | |
pub fn PrettySerializer(wr: io::Writer) -> PrettySerializer { | |
PrettySerializer { wr: wr } | |
} | |
pub impl PrettySerializer: Serializer { | |
fn emit_uint(&self, v: uint) { | |
self.wr.write_str(fmt!("pretty %?u", v)); | |
} | |
} | |
fn print2< | |
S1: Serializer, | |
S2: Serializer, | |
T: Serializable<S1> Serializable<S2> | |
>(t: T, s1: &S1, s2: &S2) { | |
t.serialize(s1); | |
io::println(""); | |
t.serialize(s2); | |
io::println(""); | |
} | |
fn main() { | |
print2(5u, &UglySerializer(io::stdout()), &PrettySerializer(io::stdout())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment