MongoDB Rust和BSON驱动程序使rustc编译器声明错误E0277



我得到了以下场景,使用:

Rust version: 1.63
Ubuntu 20.04

以及MongoDB Rust和BSON驱动程序,如下所示:

  • https://github.com/rust-lang/crates.io-index#mongodb@2.3.0

  • https://github.com/rust-lang/crates.io-index#bson@2.4.0

我使用的是MongoDB 5.0.4,具有副本集拓扑。

描述错误:

在编译时,出现错误E0277,并显示以下消息:特性绑定的ValueBag<'_>:From不满意。该代码假装获得MongoDB实例中托管的数据库列表并打印。我是Rust的新手,也是MongoDB生态系统的忠实消费者,通过mongosh,通过其他API,如Python、Java、Scala等。我也很确定我使用Rust 1.57运行这段代码。我测试了其他拓扑,比如最简单的拓扑:localhost、默认端口、独立模式,但场景保持不变,在编译时触发E0277。

要复制代码,只需键入其中一个示例:

use mongodb::{Client, options::ClientOptions};
async fn main() -> mongodb::error::Result<()>{
let mut client_options = ClientOptions::parse(
"mongodb://<a_member_of_ReplSet_IP>,<another_member_of_ReplSet_IP/?replicaSet=myReplSet").await?;
client_options.app_name = Some("My Copier app".to_string());
let client = Client::with_options(client_options)?;
for db_name in client.list_database_names(None, None).await? {
println!("{}", db_name);
}
Ok(())
}

错误发生在编译时:

Compiling log v0.4.14
error[E0277]: the trait bound `ValueBag<'_>: From<u128>` is not satisfied
--> /home/jolmedo/.cargo/registry/src/github.com-1ecc6299db9ec823/log-0.4.14/src/kv/value.rs:364:21
|
364 |                       Value::from_value_bag(value)
|                       ^^^^^^^^^^^^^^^^^^^^^ the trait `From<u128>` is not implemented for `ValueBag<'_>`
...
384 | / impl_to_value_primitive![
385 | |     usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64, char, bool,
386 | | ];
| |_- in this macro invocation
|
= help: the following other types implement trait `From<T>`:
<ValueBag<'v> as From<&'a ()>>
<ValueBag<'v> as From<&'a bool>>
<ValueBag<'v> as From<&'a char>>
<ValueBag<'v> as From<&'a f32>>
<ValueBag<'v> as From<&'a f64>>
<ValueBag<'v> as From<&'a i16>>
<ValueBag<'v> as From<&'a i32>>
<ValueBag<'v> as From<&'a i64>>
and 25 others
= note: required because of the requirements on the impl of `Into<ValueBag<'_>>` for `u128`
note: required by a bound in `Value::<'v>::from_value_bag`
--> /home/jolmedo/.cargo/registry/src/github.com-1ecc6299db9ec823/log-0.4.14/src/kv/value.rs:250:12
|
248 |     fn from_value_bag<T>(value: T) -> Self
|        -------------- required by a bound in this
249 |     where
250 |         T: Into<ValueBag<'v>>,
|            ^^^^^^^^^^^^^^^^^^ required by this bound in `Value::<'v>::from_value_bag`
= note: this error originates in the macro `impl_to_value_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `ValueBag<'_>: From<i128>` is not satisfied
--> /home/jolmedo/.cargo/registry/src/github.com-1ecc6299db9ec823/log-0.4.14/src/kv/value.rs:364:21
|
364 |                       Value::from_value_bag(value)
|                       ^^^^^^^^^^^^^^^^^^^^^ the trait `From<i128>` is not implemented for `ValueBag<'_>`
...
384 | / impl_to_value_primitive![
385 | |     usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64, char, bool,
386 | | ];
| |_- in this macro invocation
|
= help: the following other types implement trait `From<T>`:
<ValueBag<'v> as From<&'a ()>>
<ValueBag<'v> as From<&'a bool>>
<ValueBag<'v> as From<&'a char>>
<ValueBag<'v> as From<&'a f32>>
<ValueBag<'v> as From<&'a f64>>
<ValueBag<'v> as From<&'a i16>>
<ValueBag<'v> as From<&'a i32>>
<ValueBag<'v> as From<&'a i64>>
and 25 others
= note: required because of the requirements on the impl of `Into<ValueBag<'_>>` for `i128`
note: required by a bound in `Value::<'v>::from_value_bag`
--> /home/jolmedo/.cargo/registry/src/github.com-1ecc6299db9ec823/log-0.4.14/src/kv/value.rs:250:12
|
248 |     fn from_value_bag<T>(value: T) -> Self
|        -------------- required by a bound in this
249 |     where
250 |         T: Into<ValueBag<'v>>,
|            ^^^^^^^^^^^^^^^^^^ required by this bound in `Value::<'v>::from_value_bag`
= note: this error originates in the macro `impl_to_value_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0277`.
error: could not compile `log` due to 2 previous errors
Process finished with exit code 101

提前感谢您的时间和关心。顺致敬意,

首先,这个错误不是您的错。log机箱的维护人员发布的版本在某些系统上无法编译。他们发布的代码看起来很好,在大多数CPU上都能工作,但在其他CPU上遇到了问题。这个问题本不应该发生,但已经得到了修补。

幸运的是,您可以在项目中添加新版本的日志来修复它。版本0.4.14存在此问题,但已修复(最新版本为0.4.16(。如果你在项目中添加了一个补丁版本号更高的版本,Cargo会发现它是兼容的,并将依赖项使用的版本切换为使用它。这是因为x.y.ax.y.b版本的板条箱应该具有兼容的API,所以Cargo在编译项目时会对两个板条箱使用两者中较高的一个。

因此,修复方法是将log = "0.4.16"添加到Cargo.toml中的依赖项中。

最新更新