Reputation: 11
I am trying to serialize a stuct with a generic member I get the error 'equired because of the requirements on the impl of sns_pub::_IMPL_DESERIALIZE_FOR_Message::_serde::Serialize
for sns_pub::Message<T>
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(bound(
serialize = "T: Serialize",
deserialize = "T: Deserialize<'de>",
))]
struct Message<T> {
topic: String,
message_id: String,
created_date: DateTime<Utc>,
message: T,
subject: String
}
let msg = String::from("TEST_MESSAGE");
let message = Message {
topic: self.topic.clone(),
message_id: id.to_string(),
created_date: now,
subject: self.subject.clone(),
message: msg
};
let filename = format!("{}/{}_{}", self.folder_path, prefix, id);
let mut file = File::create(filename.clone())?;
file.write_all(serde_yaml::to_string(&message)?.as_str())?;
error[E0277]: the trait bound `T: sns_pub::_IMPL_DESERIALIZE_FOR_Message::_serde::Serialize` is not satisfied
--> src/sns_pub.rs:57:46
|
43 | impl<T> SnsPub<T> for DiskSnsPub {
| - help: consider restricting this bound: `T: sns_pub::_IMPL_DESERIALIZE_FOR_Message::_serde::Serialize` ...
57 | file.write_all(serde_yaml::to_string(&message)?.as_str())?;
| ^^^^^^^^ the trait `sns_pub::_IMPL_DESERIALIZE_FOR_Message::_serde::Serialize` is not implemented for `T`
| ::: /home/moharaza/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.11/src/ser.rs:421:8
|
421 | T: ser::Serialize,
| -------------- required by this bound in `serde_yaml::ser::to_string`
|
= note: required because of the requirements on the impl of `sns_pub::_IMPL_DESERIALIZE_FOR_Message::_serde::Serialize` for `sns_pub::Message<T>`
error[E0308]: mismatched types
Upvotes: 0
Views: 388
Reputation: 58735
The provided code is missing a lot of information, and the error is actually caused by code that you didn't provide. This is why it's important to provide a [reprex].
However, I can guess some of the code is actually from a method of Message
, since it uses self
. What you have missed is to but bounds on T
for the impl block that defines the method:
impl<T> Message<T>
where
T: Serialize + for<'de> Deserialize<'de>,
{
/// methods go here
{
I know this because the error message already tells you pretty much this exactly.
Upvotes: 0