-1

I am using the Rocket web framework and I need to create a function with a generic parameter type parameter:

use rocket_contrib::json::{Json, JsonValue};

fn bluid_succes_response<T>(data: T) ->  JsonValue  { 
    json!(data) 
}

This is the error I have:

the trait `dataStructures::parameters_structures::_::_serde::Serialize` is not implemented for `T`

How can I implement the Serialize trait for a generic type like T or is there another way to solve the problem?

Simon Kissane
  • 4,373
  • 3
  • 34
  • 59

1 Answers1

1

You need to constrain the generic type T to a trait (Serialize in this case):

use rocket_contrib::json::{Json, JsonValue};

fn bluid_succes_response<T: serde::Serialize>(data: T) ->  JsonValue  { 
    json!(data) 
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Netwave
  • 40,134
  • 6
  • 50
  • 93