I'm trying to write a generic function to send some data, wrapped into a parent-struct. The data should then be encoded as JSON and sent with a socket.
extern crate serialize;
use serialize::json;
use serialize::serialize::{Encodable, Encoder};
use std::io::{TcpStream, IoResult};
#[deriving(Decodable, Encodable)]
struct RemoteRequest<T> {
id: String,
auth: RequestAuth,
data: T,
}
fn send_request<'a, T:Encodable<Encoder<'a>>>(mut socket: TcpStream, id: &str, data: &'a T) -> IoResult<()> {
let encoded = json::encode(&RemoteRequest {
id: id.to_string(),
auth: RequestAuth {
id: "ForgeDevName".to_string(),
pass: "password".to_string(),
},
data: data,
});
return socket.write((encoded + "\n\n\n").as_bytes())
}
But I'm getting the following compiler error:
error: wrong number of lifetime parameters: expected 0, found 1 [E0107]
fn send_request<'a, T:Encodable<Encoder<'a>>>(mut socket: TcpStream, id: &str, data: &'a T) -> IoResult<()> {
^~~~~~~~~~~
No matter what I do, I was not able to find the correct generic parameters for <T>