我最近try 使用format_serde_error,这是一个格式化插入错误消息的 crate .我发现该示例似乎不能编译.以下代码片段:
use format_serde_error::SerdeError;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Foo {
x: u64,
}
fn main() {
let content = "x: 3";
let foo = serde_yaml::from_str(content)
.map_err(|err| SerdeError::new(content.to_string(), err))
.unwrap();
println!("Read in {:?}", foo);
}
连同随附的Cargo.toml
个:
[package]
name = "ser-test"
version = "0.1.0"
edition = "2021"
[dependencies]
format_serde_error = { version = "0.3.0", features = ["serde_yaml"] }
serde = { version = "1.0.183", features = ["derive"] }
serde_yaml = "0.9.25"
产生令人费解的:
error[E0277]: the trait bound `ErrorTypes: From<serde_yaml::Error>` is not satisfied
--> src/main.rs:12:61
|
12 | .map_err(|err| SerdeError::new(content.to_string(), err))
| --------------- ^^^ the trait `From<serde_yaml::Error>` is not implemented for `ErrorTypes`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `From<T>`:
<ErrorTypes as From<serde_json::error::Error>>
<ErrorTypes as From<serde_yaml::error::Error>>
<ErrorTypes as From<(Box<(dyn StdError + 'static)>, std::option::Option<usize>, std::option::Option<usize>)>>
= note: required for `serde_yaml::Error` to implement `Into<ErrorTypes>`
note: required by a bound in `SerdeError::new`
--> /home/c/.cargo/registry/src/index.crates.io-6f17d22bba15001f/format_serde_error-0.3.0/src/lib.rs:278:41
|
278 | pub fn new(input: String, err: impl Into<ErrorTypes>) -> SerdeError {
| ^^^^^^^^^^^^^^^^ required by this bound in `SerdeError::new`
我之所以认为这是inscrutable,是因为在编译器输出中提到的code on github和lib.ars文件中,Impl中的这个‘Missing’被定义为:
#[cfg(feature = "serde_yaml")]
impl From<serde_yaml::Error> for ErrorTypes {
fn from(err: serde_yaml::Error) -> Self {
Self::Yaml(err)
}
}
我不明白为什么编译器没有看到这From
个Iml.我已经更新了铁 rust (运行1.73.0-nightly (f3623871c 2023-08-06)
)和cargo clean
版,没有任何变化.如果我使用默认功能标志或指定serde_yaml
,则此行为不会更改.
更令人困惑的是,我的代码完全(意外地)抄袭了format_serde_error
提供的an example个内容.
对于那些想要准确重现该问题的人,以下是在我的机器上重现该问题的一系列shell 命令:
cargo new ser-test
cd ser-test
cargo add serde --features derive
cargo add serde_yaml
cargo add format_serde_error
echo Copy the snippet into main.rs here
cargo run