<分区>
如何为内部具有不同或相同生命周期的对象的结构派生反序列化
?
#[derive(Default, Debug, serde::Deserialize, serde::Serialize)]
struct B<'a> {
b: &'a str,
}
#[derive(Default, Debug, serde::Deserialize, serde::Serialize)]
struct C<'a> {
c: &'a str,
}
#[derive(Default, Debug, serde::Deserialize, serde::Serialize)]
struct A<'a> {
b: B<'a>,
c: C<'a>,
}
fn main() {
}
Rustc 说这是不可能的:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
--> src/main.rs:13:5
|
13 | b: B<'a>,
| ^
|
note: first, the lifetime cannot outlive the lifetime 'de as defined on the impl at 11:26...
--> src/main.rs:11:26
|
11 | #[derive(Default, Debug, serde::Deserialize, serde::Serialize)]
| ^^^^^^^^^^^^^^^^^^
= note: ...so that the types are compatible:
expected _IMPL_SERIALIZE_FOR_B::_serde::de::SeqAccess<'_>
found _IMPL_SERIALIZE_FOR_B::_serde::de::SeqAccess<'de>
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 12:10...
--> src/main.rs:12:10
|
12 | struct A<'a> {
| ^^
= note: ...so that the types are compatible:
expected _IMPL_SERIALIZE_FOR_B::_serde::Deserialize<'_>
found _IMPL_SERIALIZE_FOR_B::_serde::Deserialize<'_>
我不明白导致此问题的原因以及如何解决它。 有 a similar question但它的答案不包括这种情况。