-
-
Save JustAPerson/a96dddcde35633ec9d27 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
extern crate ser = "serialize"; | |
use serialize = self::ser; // necessary for deriving(Encodable) | |
use ser::{Encodable, Encoder}; | |
use ser::json; | |
use ser::ebml::writer; | |
use std::io::MemWriter; | |
use std::str::from_utf8_owned; | |
#[deriving(Encodable)] | |
struct Foo { | |
baz: bool, | |
} | |
#[deriving(Encodable)] | |
struct Bar { | |
froboz: uint | |
} | |
enum WireProtocol { | |
JSON, | |
EBML, | |
// ... | |
} | |
fn encode_json<'a, T: Encodable<json::Encoder<'a>, std::io::IoError>>(val: &T, wr: &'a mut MemWriter) { | |
let mut encoder = json::Encoder::new(wr); | |
val.encode(&mut encoder); | |
} | |
fn encode_ebml<'a, T: Encodable<writer::Encoder<'a, MemWriter>, std::io::IoError>>(val: &T, wr: &'a mut MemWriter) { | |
let mut encoder = writer::Encoder(wr); | |
val.encode(&mut encoder); | |
} | |
pub fn main() { | |
let target = Foo { baz: false }; | |
let mut wr = MemWriter::new(); | |
let proto = JSON; | |
match proto { | |
JSON => encode_json(&target, &mut wr), | |
EBML => encode_ebml(&target, &mut wr) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment