Skip to content

Instantly share code, notes, and snippets.

@daxhuiberts
Last active August 29, 2015 14:12
Show Gist options
  • Save daxhuiberts/6527eea491da5a9d70b3 to your computer and use it in GitHub Desktop.
Save daxhuiberts/6527eea491da5a9d70b3 to your computer and use it in GitHub Desktop.
what to do with the 'impl <T> Payload<T>'?
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