]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-11881.rs
auto merge of #19648 : mquandalle/rust/patch-1, r=alexcrichton
[rust.git] / src / test / run-pass / issue-11881.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 extern crate rbml;
12 extern crate serialize;
13
14 use std::io;
15 use std::io::{IoError, IoResult, SeekStyle};
16 use std::slice;
17
18 use serialize::{Encodable, Encoder};
19 use serialize::json;
20
21 use rbml::writer;
22 use rbml::io::SeekableMemWriter;
23
24 #[deriving(Encodable)]
25 struct Foo {
26     baz: bool,
27 }
28
29 #[deriving(Encodable)]
30 struct Bar {
31     froboz: uint,
32 }
33
34 enum WireProtocol {
35     JSON,
36     RBML,
37     // ...
38 }
39
40 fn encode_json<'a,
41                T: Encodable<json::Encoder<'a>,
42                             std::io::IoError>>(val: &T,
43                                                wr: &'a mut SeekableMemWriter) {
44     let mut encoder = json::Encoder::new(wr);
45     val.encode(&mut encoder);
46 }
47 fn encode_rbml<'a,
48                T: Encodable<writer::Encoder<'a, SeekableMemWriter>,
49                             std::io::IoError>>(val: &T,
50                                                wr: &'a mut SeekableMemWriter) {
51     let mut encoder = writer::Encoder::new(wr);
52     val.encode(&mut encoder);
53 }
54
55 pub fn main() {
56     let target = Foo{baz: false,};
57     let mut wr = SeekableMemWriter::new();
58     let proto = WireProtocol::JSON;
59     match proto {
60         WireProtocol::JSON => encode_json(&target, &mut wr),
61         WireProtocol::RBML => encode_rbml(&target, &mut wr)
62     }
63 }