I'm trying to implement a struct that holds a field which implements two traits:
use meilisearch_sdk::document::Document;
use serde::{Serialize,Deserialize};
trait DeserializableDocument<'a>: Deserialize<'a> + Document{}
#[derive(Serialize, Deserialize, Debug)]
pub struct KafkaMessage<'a, DeserializableDocument>{
Data: &'a DeserializableDocument,
}
Where the following struct would satisfy Data
:
#[derive(Serialize, Deserialize, Debug)]
pub struct User {
ID: String,
Firstname: String,
Lastname: String,
}
impl Document for User {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType { &self.ID }
}
However upon trying to define the empty trait object DeserializableDocument
I get the following error:
type annotations needed
cannot infer type for type parameter `Self`
note: cannot satisfy `Self: <my-project>::documents::_::_serde::Deserialize<'a>`
[dependencies]
serde = { version="1.0", features = ["derive"] }
meilisearch-sdk = "0.15"
What is the correct way to approach this?
Edit 1:
When restructured according to @ChayimFriedman answer and @SebastianRedl comment:
#[derive(Serialize, Deserialize, Debug)]
pub struct KafkaMessage<D> where D: Document {
Data: D,
}
I get the following compiler error:
type annotations needed for `std::option::Option<D>`
consider giving `__field1` the explicit type `std::option::Option<D>`, where the type parameter `D` is specified
Ofcourse, specifying D: Option<D>
doesn't resolve it either.