Hi,
I am trying to creatue a structure with a geneics that I want to Serialize and Deserialize with Serde.
Here is how is define my struct:
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
pub struct Request<T>
where
T: Serialize + Deserialize
{
pub id: String,
pub message: T,
}
impl<T> Request<T>
where
T: Serialize + Deserialize,
{
pub fn new(id: impl ToString, message: T) -> Request<T> {
Request {
id: id.to_string(),
message,
}
}
pub fn to_serialized_string(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(&self)
}
pub fn serialize_to_bytes(&self) -> Result<Vec<u8>, serde_json::Error> {
serde_json::to_vec(&self)
}
}
I want to use such structure with generic type like String
or even some enum
defined like this :
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Serialize, Deserialize)]
pub enum DomainApiMessageAnswer {
Starting,
Stoping,
Restarting,
ReloadConfiguration,
Error
}
And I have the following error :
error[E0106]: missing lifetime specifier
--> unixsocket/src/message.rs:16:20
|
16 | T: Serialize + Deserialize,
| ^^^^^^^^^^^ expected named lifetime parameter
|
help: consider making the bound lifetime-generic with a new `'a` lifetime
|
16 | T: Serialize + for<'a> Deserialize<'a>,
| +++++++ ++++
help: consider making the bound lifetime-generic with a new `'a` lifetime
|
16 | for<'a> T: Serialize + Deserialize<'a>,
| +++++++ ++++
help: consider introducing a named lifetime parameter
|
14 ~ impl<'a, T> Request<T>
15 | where
16 ~ T: Serialize + Deserialize<'a>,
|
At first I though that not putting the where
clause on the generics would suffice because the enum
can be Deserialized but with no chances.
How can I solve that?