Attribute conflict with the a lifetime

I have to declare pub because of used Deserialize but it have got conflict with the a lifetime

#[derive(Debug,Deserialize,Serialize)]
pub struct OptionTransaction<'a>{
    pub puts: &'a Option<Transaction>
}

Error compiling:

the trait bound `&'a std::option::Option<Transaction>: Deserialize<'_>` is not satisfied
the following implementations were found:
  <std::option::Option<T> as Deserialize<'de>>rustcE0277
mod.rs(1725, 12): required by a bound in `next_element`

In general, it is not possible to deserialize into a reference – the deserialized data must come from (and be stored/owned) somewhere, and except for very special circumstances, that's not possible unless your data structure owns its contents. Change the type of the puts field to an owning Option<Transaction>, without the reference.


By the way, you don't have to declare fields (or types) as pub in order to derive Deserialize.

1 Like

It is related to the follow post and I can not remove 'a from OptionTransaction
entanglement-2lifetime-problem-pluse-2-short-question

I have to use pub and when I remove it I have got this message

the trait bound `&'a std::option::Option<Transaction>: Deserialize<'_>` is not satisfied
the following implementations were found:
  <std::option::Option<T> as Deserialize<'de>>rustcE0277
mod.rs(1725, 12): required by a bound in `next_element`

Sorry, that is way too long for us to read and understand in one go. Please produce a minimal, self-contained example that reproduces the problem.

Thanks for your response but this code have dependencies that I can not split is

https://stackoverflow.com/questions/65378524/rust-diesel-the-trait-bound-naivedatetime-deserialize-is-not-satisfied
My source code

Can you write a minimal reproduction (10 to 90 lines) in the playground ? General advice, use serde with owned structs.

1 Like