Last active
August 29, 2015 14:12
-
-
Save daxhuiberts/6527eea491da5a9d70b3 to your computer and use it in GitHub Desktop.
what to do with the 'impl <T> Payload<T>'?
This file contains 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 "rustc-serialize" as rustc_serialize; | |
use rustc_serialize::json; | |
use rustc_serialize::Encodable; | |
use rustc_serialize::json::Encoder; | |
use std::io::IoError; | |
#[deriving(RustcEncodable)] | |
struct Data { | |
str: &'static str, | |
int: i32, | |
} | |
#[deriving(RustcEncodable)] | |
struct Payload<T> { | |
timestamp: u32, | |
data: T, | |
} | |
impl <'a, T: Encodable<Encoder<'a>, IoError>> Payload<T> { | |
fn json_encode(&self) -> String { | |
json::encode(self) | |
} | |
} | |
fn main() { | |
let data = Data { str: "hello world", int: 42 }; | |
let payload = Payload { timestamp: 0, data: data }; | |
let json = payload.json_encode(); | |
println!("{}", json); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment