【发布时间】:2021-08-31 23:06:12
【问题描述】:
这是代码链接。我希望 Bar 能够包装实现 Serialize、Derialize for json 的任意类型。
有人可以分享如何编译吗?
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct Foo {
i: u32,
}
#[derive(Serialize, Deserialize)]
struct Bar<'a, K> where
K: Serialize + Deserialize<'a>
{
f: K,
}
fn main() {
let bf = Bar::<'_, Foo> {
f: Foo { i: 9 },
};
}
Compiling playground v0.0.1 (/playground)
error[E0392]: parameter `'a` is never used
--> src/lib.rs:9:12
|
9 | struct Bar<'a, K> where
| ^^ unused parameter
|
= help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData`
error[E0283]: type annotations needed
--> src/lib.rs:10:20
|
10 | K: Serialize + Deserialize<'a>
| ^^^^^^^^^^^^^^^ cannot infer type for type parameter `K`
|
::: /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.127/src/de/mod.rs:530:1
|
530 | pub trait Deserialize<'de>: Sized {
| --------------------------------- required by this bound in `Deserialize`
|
= note: cannot satisfy `K: Deserialize<'a>`
error[E0283]: type annotations needed
--> src/lib.rs:8:21
|
8 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^ cannot infer type for type parameter `K`
9 | struct Bar<'a, K> where
| --- required by a bound in this
10 | K: Serialize + Deserialize<'a>
| --------------- required by this bound in `Bar`
|
= note: cannot satisfy `K: Deserialize<'a>`
= note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider specifying the type arguments in the function call
|
8 | #[derive(Serialize, Deserialize::<'a, K>)]
| ^^^^^^^^^
error[E0283]: type annotations needed
--> src/lib.rs:8:21
|
8 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^ cannot infer type for type parameter `K`
|
= note: cannot satisfy `K: Deserialize<'_>`
note: required because of the requirements on the impl of `Visitor<'de>` for `_::<impl Deserialize<'de> for Bar<'a, K>>::deserialize::__Visitor<'de, 'a, K>`
--> src/lib.rs:8:21
|
8 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^
= note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0283, E0392.
For more information about an error, try `rustc --explain E0283`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
【问题讨论】:
-
在 Rust 操场上,我得到了一个完全不同的错误,通过在
f: Foo { i: 9 },之后添加_a: std::marker::PhantomData,来解决。希望对您有所帮助。