Lifetime error of serde’s impl-deserializer example


#1

I am learning serde and reading its document of implementing a custom deserializer. All goes well, until I added a definition of a wrapper struct in the test_struct function.
See line 735 in the code.

Then rustc complained about the lifetime comflicting in the process of deriving Deserialize for struct Wrapper<‘a>( Test<‘a> ).

Why and how?


#2

You need to indicate that you’re borrowing the data for that field from the input:

struct Wrapper<'a>(#[serde(borrow)] Test<'a> );

https://serde.rs/lifetimes.html#borrowing-data-in-a-derived-impl has more info.


#3

Thanks! I did glanced over that chapter before you mentioned, but didn’t realized it was the answer to my question.