我试着为一个包含对Path
的引用的结构体派生serde :: Deserialize
,这会产生一个错误信息,如果将&'a Path
替换为&'a str
则不会发生。是什么导致了#[derive(Deserialize)]
的不同行为?
#!/bin/cargo script
//! ```cargo
//! [dependencies]
//! serde_derive="1.0"
//! serde="1.0"
//! ```
extern crate serde_derive;
use serde_derive::*;
#[derive(Deserialize)]
struct A<'a> {
a: &'a std::path::Path,
//a: &'a str,
}
fn main() {}
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
--> src/main.rs:7:5
|
7 | a: &'a std::path::Path,
| ^
|
note: first, the lifetime cannot outlive the lifetime 'de as defined on the impl at 5:10...
--> src/main.rs:5:10
|
5 | #[derive(Deserialize)]
| ^^^^^^^^^^^
= note: ...so that the types are compatible:
expected _IMPL_DESERIALIZE_FOR_A::_serde::de::SeqAccess<'_>
found _IMPL_DESERIALIZE_FOR_A::_serde::de::SeqAccess<'de>
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 6:10...
--> src/main.rs:6:10
|
6 | struct A<'a> {
| ^^
= note: ...so that the types are compatible:
expected _IMPL_DESERIALIZE_FOR_A::_serde::Deserialize<'_>
found _IMPL_DESERIALIZE_FOR_A::_serde::Deserialize<'_>
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
--> src/main.rs:7:5
|
7 | a: &'a std::path::Path,
| ^
|
note: first, the lifetime cannot outlive the lifetime 'de as defined on the impl at 5:10...
--> src/main.rs:5:10
|
5 | #[derive(Deserialize)]
| ^^^^^^^^^^^
= note: ...so that the types are compatible:
expected _IMPL_DESERIALIZE_FOR_A::_serde::de::MapAccess<'_>
found _IMPL_DESERIALIZE_FOR_A::_serde::de::MapAccess<'de>
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 6:10...
--> src/main.rs:6:10
|
6 | struct A<'a> {
| ^^
= note: ...so that the types are compatible:
expected _IMPL_DESERIALIZE_FOR_A::_serde::Deserialize<'_>
found _IMPL_DESERIALIZE_FOR_A::_serde::Deserialize<'_>
奇怪的是,如果结构体同时包含字段_a: &'a Path
和_b: &'a str
,代码仍能编译通过... 我认为这是一个bug。
a: PathBuf
- Boiethios&'a str
和&'a Path
的Deserialize
的impl
,两种情况下所有内容看起来都一样,除了通过AsRef
进行的类型转换,但这在PathVisitor
的主体中,并且当出现此错误时应该已经进行了长时间的类型检查。看起来这甚至可能是生命周期推断中的一个错误。 - Jan Hudec