Hello,
I'm trying to use nested struct references in serde.
But I'm struggling with lifetime implementation.
I would like to avoid String, Clone .. or any costly operation as much as possible.
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Foo<'a> {
tst: &'a str
}
#[derive(Serialize, Deserialize, Debug)]
struct Bar<'a> {
#[serde(bound(deserialize = "&'a Foo<'a>: Deserialize<'a>"))]
foo: &'a Foo<'a>
}
fn to_fix<'a, T, Y>(tmp: T) -> Y
where
Y: Serialize,
T: Deserialize<'a>
{
todo!();
}
fn main() {
let foo = Foo {
tst: "hello world",
};
let bar = Bar{
foo: &foo
};
let tmp: Bar = to_fix(bar);
}
but I'm getting the following compilation error:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `&Foo<'_>: Deserialize<'_>` is not satisfied
--> src/main.rs:29:27
|
29 | let tmp: Bar = to_fix(bar);
| ------ ^^^ the trait `Deserialize<'_>` is not implemented for `&Foo<'_>`
| |
| required by a bound introduced by this call
|
= help: the trait `Deserialize<'de>` is implemented for `Foo<'a>`
note: required for `Bar<'_>` to implement `Deserialize<'_>`
--> src/main.rs:8:21
|
8 | #[derive(Serialize, Deserialize, Debug)]
| ^^^^^^^^^^^
9 | struct Bar<'a> {
| ^^^^^^^
10 | #[serde(bound(deserialize = "&'a Foo<'a>: Deserialize<'a>"))]
| ------------------------------ unsatisfied trait bound introduced in this `derive` macro
note: required by a bound in `to_fix`
--> src/main.rs:17:8
|
14 | fn to_fix<'a, T, Y>(tmp: T) -> Y
| ------ required by a bound in this
...
17 | T: Deserialize<'a>
| ^^^^^^^^^^^^^^^ required by this bound in `to_fix`
= note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error
Could you please help me to identify what's wrong with the to_fix function?