Rust 如何在包含&;的结构上实现serde::反序列化';静态str?
我试图在Rust 如何在包含&;的结构上实现serde::反序列化';静态str?,rust,deserialization,borrow-checker,serde,Rust,Deserialization,Borrow Checker,Serde,我试图在SourceConfigstruct上实现serde::反序列化,它包装了一个包含&静态str的结构,并拥有自己的一些数据 使用serde::{序列化,反序列化}; #[派生(序列化、反序列化)] 发布结构配置{ 酒吧名称:&'static str, } #[派生(序列化、反序列化)] 结构源配置{ config:config, id:u32, } 但这给了我一个终生的错误: Compiling playground v0.0.1 (/playground) error[E04
SourceConfig
struct上实现serde::反序列化
,它包装了一个包含&静态str
的结构,并拥有自己的一些数据
使用serde::{序列化,反序列化};
#[派生(序列化、反序列化)]
发布结构配置{
酒吧名称:&'static str,
}
#[派生(序列化、反序列化)]
结构源配置{
config:config,
id:u32,
}
但这给了我一个终生的错误:
Compiling playground v0.0.1 (/playground)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'de` as defined on the impl at 8:21...
--> src/lib.rs:8:21
|
8 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^
note: ...so that the types are compatible
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
= note: expected `SeqAccess<'_>`
found `SeqAccess<'de>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
= note: expected `Deserialize<'_>`
found `Deserialize<'static>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'de` as defined on the impl at 8:21...
--> src/lib.rs:8:21
|
8 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^
note: ...so that the types are compatible
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
= note: expected `MapAccess<'_>`
found `MapAccess<'de>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
--> src/lib.rs:10:5
|
10 | config: Config,
| ^^^^^^
= note: expected `Deserialize<'_>`
found `Deserialize<'static>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
编译操场v0.0.1(/playerd)
错误[E0495]:由于需求冲突,无法为生存期参数“de”推断适当的生存期
-->src/lib.rs:10:5
|
10 |配置:配置,
| ^^^^^^
|
注意:首先,生命周期不能超过impl在8:21定义的生命周期“de”。。。
-->src/lib.rs:8:21
|
8 |#[派生(序列化、反序列化)]
| ^^^^^^^^^^^
注意:…以便类型兼容
-->src/lib.rs:10:5
|
10 |配置:配置,
| ^^^^^^
=注:应为'SeqAccess'`
=注意:但是,生存期必须对静态生存期有效。。。
注意:…以便类型兼容
-->src/lib.rs:10:5
|
10 |配置:配置,
| ^^^^^^
=注意:应为“反序列化”`
=注意:此错误源于派生宏(在夜间构建中,使用-Z宏反向跟踪运行以获取更多信息)
错误[E0495]:由于需求冲突,无法为生存期参数“de”推断适当的生存期
-->src/lib.rs:10:5
|
10 |配置:配置,
| ^^^^^^
|
注意:首先,生命周期不能超过impl在8:21定义的生命周期“de”。。。
-->src/lib.rs:8:21
|
8 |#[派生(序列化、反序列化)]
| ^^^^^^^^^^^
注意:…以便类型兼容
-->src/lib.rs:10:5
|
10 |配置:配置,
| ^^^^^^
=注意:应为“MapAccess”`
=注意:但是,生存期必须对静态生存期有效。。。
注意:…以便类型兼容
-->src/lib.rs:10:5
|
10 |配置:配置,
| ^^^^^^
=注意:应为“反序列化”`
=注意:此错误源于派生宏(在夜间构建中,使用-Z宏反向跟踪运行以获取更多信息)
错误:由于之前的两个错误而中止
我尝试将#[serde(借用)]
添加到SourceConfig
中的config
,但这不起作用,因为config
不是借用的。将其添加到Config
中的name
中也没有帮助。如何在源配置
上正确实现反序列化
?您可以在源配置
结构上添加一个,以便'de
生存期等于静态
生存期
#[派生(调试,serde::反序列化)]
发布结构配置{
酒吧名称:&'static str,
}
#[派生(调试,serde::反序列化)]
#[serde(绑定(反序列化=“'de:'static”)]
结构源配置{
config:config,
id:u32,
}
fn main(){
设j=r#”
{
“id”:123,
“配置”:{
“姓名”:“约翰·史密斯”
}
}
"#;
让sc:SourceConfig=serde_json::from_str(&j).unwrap();
dbg!(sc);
}
但是,您可能不想反序列化一个&'static str
,因为这要求您有一个&'static str
可以借用。这将您限制为字符串文本,或者您需要泄漏内存来获取&'static str
。
您可能需要一个字符串
,或者,如果您想支持字符串文本,可以在源配置
结构上添加一个'de
生存期等于静态
生存期
#[派生(调试,serde::反序列化)]
发布结构配置{
酒吧名称:&'static str,
}
#[派生(调试,serde::反序列化)]
#[serde(绑定(反序列化=“'de:'static”)]
结构源配置{
config:config,
id:u32,
}
fn main(){
设j=r#”
{
“id”:123,
“配置”:{
“姓名”:“约翰·史密斯”
}
}
"#;
让sc:SourceConfig=serde_json::from_str(&j).unwrap();
dbg!(sc);
}
但是,您可能不想反序列化一个&'static str
,因为这要求您有一个&'static str
可以借用。这将您限制为字符串文本,或者您需要泄漏内存来获取&'static str
。
您可能需要一个字符串
,或者,如果您想支持字符串文字,则需要一个Cow